Skip to content

Commit

Permalink
chore: fix config tests; make noop global meter
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Phelps <[email protected]>
  • Loading branch information
markphelps committed Apr 24, 2024
1 parent 66f1aa3 commit 47f9ddd
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
metric "go.opentelemetry.io/otel/metric"
metricsnoop "go.opentelemetry.io/otel/metric/noop"
metricnoop "go.opentelemetry.io/otel/metric/noop"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap"
Expand Down Expand Up @@ -155,7 +155,7 @@ func NewGRPCServer(
logger.Debug("store enabled", zap.Stringer("store", store))

// Default to a no-op OTEL meter provider
var meterProvider metric.MeterProvider = metricsnoop.NewMeterProvider()
var meterProvider metric.MeterProvider = metricnoop.NewMeterProvider()

// Initialize metrics exporter if enabled
if cfg.Metrics.Enabled {
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ func Default() *Config {
GRPCPort: 9000,
},

Metrics: MetricsConfig{
Enabled: true,
Exporter: MetricsPrometheus,
},

Tracing: TracingConfig{
Enabled: false,
Exporter: TracingJaeger,
Expand Down
9 changes: 9 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
name: "metrics disabled",
path: "./testdata/metrics/disabled.yml",
expected: func() *Config {
cfg := Default()
cfg.Metrics.Enabled = false
return cfg
},
},
{
name: "tracing zipkin",
path: "./testdata/tracing/zipkin.yml",
Expand Down
11 changes: 4 additions & 7 deletions internal/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@ const (
)

type MetricsConfig struct {
Enabled bool `json:"enabled" mapstructure:"enabled" yaml:"enabled"`
Exporter MetricsExporter `json:"exporter,omitempty" mapstructure:"exporter" yaml:"exporter,omitempty"`
OTLP *OTLPConfig `json:"otlp,omitempty" mapstructure:"otlp" yaml:"otlp,omitempty"`
Enabled bool `json:"enabled" mapstructure:"enabled" yaml:"enabled"`
Exporter MetricsExporter `json:"exporter,omitempty" mapstructure:"exporter" yaml:"exporter,omitempty"`
OTLP *OTLPMetricsConfig `json:"otlp,omitempty" mapstructure:"otlp" yaml:"otlp,omitempty"`
}

type OTLPConfig struct {
type OTLPMetricsConfig struct {
Endpoint string `json:"endpoint,omitempty" mapstructure:"endpoint" yaml:"endpoint,omitempty"`
}

func (c *MetricsConfig) setDefaults(v *viper.Viper) error {
v.SetDefault("metrics", map[string]interface{}{
"enabled": true,
"exporter": MetricsPrometheus,
"otlp": map[string]interface{}{
"endpoint": "localhost:4317",
},
})

return nil
Expand Down
3 changes: 3 additions & 0 deletions internal/config/testdata/marshal/yaml/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ server:
http_port: 8080
https_port: 443
grpc_port: 9000
metrics:
enabled: true
exporter: prometheus
storage:
type: database
diagnostics:
Expand Down
3 changes: 3 additions & 0 deletions internal/config/testdata/metrics/disabled.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
metrics:
enabled: false
exporter: prometheus
10 changes: 10 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
metricnoop "go.opentelemetry.io/otel/metric/noop"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
)

// Meter is the default Flipt-wide otel metric Meter.
var Meter metric.Meter

func init() {
// If the global Meter is nil, create a new noop Meter.
// Useful for testing.
if Meter == nil {
meterProvider := metricnoop.NewMeterProvider()
Meter = meterProvider.Meter("github.com/flipt-io/flipt")
}
}

// MustInt64 returns an instrument provider based on the global Meter.
// The returns provider panics instead of returning an error when it cannot build
// a required counter, upDownCounter or histogram.
Expand Down

0 comments on commit 47f9ddd

Please sign in to comment.