From 2396bd4633b2ff40717c6c65dd99aa958f8567ab Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:29:06 -0400 Subject: [PATCH] chore: fix config tests; make noop global meter --- internal/cmd/grpc.go | 4 ++-- internal/config/config.go | 5 +++++ internal/config/config_test.go | 9 +++++++++ internal/config/metrics.go | 11 ++++------- internal/config/testdata/marshal/yaml/default.yml | 3 +++ internal/config/testdata/metrics/disabled.yml | 3 +++ internal/metrics/metrics.go | 10 ++++++++++ 7 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 internal/config/testdata/metrics/disabled.yml diff --git a/internal/cmd/grpc.go b/internal/cmd/grpc.go index c45a3d3d35..30dcf83033 100644 --- a/internal/cmd/grpc.go +++ b/internal/cmd/grpc.go @@ -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" @@ -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 { diff --git a/internal/config/config.go b/internal/config/config.go index 9d97097bfa..e5f6054c64 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -556,6 +556,11 @@ func Default() *Config { GRPCPort: 9000, }, + Metrics: MetricsConfig{ + Enabled: true, + Exporter: MetricsPrometheus, + }, + Tracing: TracingConfig{ Enabled: false, Exporter: TracingJaeger, diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 3ca3fc9307..0b8b41553e 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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", diff --git a/internal/config/metrics.go b/internal/config/metrics.go index db4825b33f..ba7dce92ff 100644 --- a/internal/config/metrics.go +++ b/internal/config/metrics.go @@ -16,12 +16,12 @@ 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"` } @@ -29,9 +29,6 @@ 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 diff --git a/internal/config/testdata/marshal/yaml/default.yml b/internal/config/testdata/marshal/yaml/default.yml index 88663b26ec..864384d9a9 100644 --- a/internal/config/testdata/marshal/yaml/default.yml +++ b/internal/config/testdata/marshal/yaml/default.yml @@ -24,6 +24,9 @@ server: http_port: 8080 https_port: 443 grpc_port: 9000 +metrics: + enabled: true + exporter: prometheus storage: type: database diagnostics: diff --git a/internal/config/testdata/metrics/disabled.yml b/internal/config/testdata/metrics/disabled.yml new file mode 100644 index 0000000000..7d85a4708e --- /dev/null +++ b/internal/config/testdata/metrics/disabled.yml @@ -0,0 +1,3 @@ +metrics: + enabled: false + exporter: prometheus diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 5f972e406a..e4e4263b82 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -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.