From 9de9b3f1b502674ad7665e260a91f091476fbbcf Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Mon, 20 Jan 2025 18:14:59 +0100 Subject: [PATCH] [chore] Move MetricsLevel check to MeterProvider creation --- config/confighttp/confighttp.go | 14 ++------------ config/confighttp/go.mod | 2 +- otelcol/go.mod | 2 ++ otelcol/otelcoltest/go.mod | 2 ++ service/go.mod | 2 +- service/telemetry/metrics.go | 17 +++++++++++++++++ 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/config/confighttp/confighttp.go b/config/confighttp/confighttp.go index c3a19211c2d..8a74fbfc600 100644 --- a/config/confighttp/confighttp.go +++ b/config/confighttp/confighttp.go @@ -18,8 +18,6 @@ import ( "github.com/rs/cors" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/metric/noop" "golang.org/x/net/http2" "golang.org/x/net/publicsuffix" @@ -28,7 +26,6 @@ import ( "go.opentelemetry.io/collector/config/configcompression" "go.opentelemetry.io/collector/config/confighttp/internal" "go.opentelemetry.io/collector/config/configopaque" - "go.opentelemetry.io/collector/config/configtelemetry" "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/extension/auth" ) @@ -247,7 +244,7 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett otelOpts := []otelhttp.Option{ otelhttp.WithTracerProvider(settings.TracerProvider), otelhttp.WithPropagators(otel.GetTextMapPropagator()), - otelhttp.WithMeterProvider(getLeveledMeterProvider(settings)), + otelhttp.WithMeterProvider(settings.MeterProvider), } // wrapping http transport with otelhttp transport to enable otel instrumentation if settings.TracerProvider != nil && settings.MeterProvider != nil { @@ -478,7 +475,7 @@ func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settin otelhttp.WithSpanNameFormatter(func(_ string, r *http.Request) string { return r.URL.Path }), - otelhttp.WithMeterProvider(getLeveledMeterProvider(settings)), + otelhttp.WithMeterProvider(settings.MeterProvider), }, serverOpts.OtelhttpOpts...) @@ -567,10 +564,3 @@ func maxRequestBodySizeInterceptor(next http.Handler, maxRecvSize int64) http.Ha next.ServeHTTP(w, r) }) } - -func getLeveledMeterProvider(settings component.TelemetrySettings) metric.MeterProvider { - if configtelemetry.LevelDetailed <= settings.MetricsLevel { - return settings.MeterProvider - } - return noop.MeterProvider{} -} diff --git a/config/confighttp/go.mod b/config/confighttp/go.mod index b5dd120619a..4579ebe4329 100644 --- a/config/confighttp/go.mod +++ b/config/confighttp/go.mod @@ -20,7 +20,6 @@ require ( go.opentelemetry.io/collector/extension/auth/authtest v0.117.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 go.opentelemetry.io/otel v1.32.0 - go.opentelemetry.io/otel/metric v1.32.0 go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 golang.org/x/net v0.34.0 @@ -38,6 +37,7 @@ require ( github.com/rogpeppe/go-internal v1.12.0 // indirect go.opentelemetry.io/collector/extension v0.117.0 // indirect go.opentelemetry.io/collector/pdata v1.23.0 // indirect + go.opentelemetry.io/otel/metric v1.32.0 // indirect go.opentelemetry.io/otel/sdk v1.32.0 // indirect go.opentelemetry.io/otel/sdk/metric v1.32.0 // indirect go.opentelemetry.io/otel/trace v1.32.0 // indirect diff --git a/otelcol/go.mod b/otelcol/go.mod index d830cd184a4..06e22f268d0 100644 --- a/otelcol/go.mod +++ b/otelcol/go.mod @@ -38,6 +38,7 @@ require ( github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/ebitengine/purego v0.8.1 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect @@ -86,6 +87,7 @@ require ( go.opentelemetry.io/collector/receiver/xreceiver v0.117.0 // indirect go.opentelemetry.io/collector/semconv v0.117.0 // indirect go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect go.opentelemetry.io/contrib/propagators/b3 v1.31.0 // indirect go.opentelemetry.io/otel v1.32.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.7.0 // indirect diff --git a/otelcol/otelcoltest/go.mod b/otelcol/otelcoltest/go.mod index 8b3df5ecc74..9d31ca87a13 100644 --- a/otelcol/otelcoltest/go.mod +++ b/otelcol/otelcoltest/go.mod @@ -32,6 +32,7 @@ require ( github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/ebitengine/purego v0.8.1 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect @@ -85,6 +86,7 @@ require ( go.opentelemetry.io/collector/semconv v0.117.0 // indirect go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 // indirect go.opentelemetry.io/contrib/config v0.10.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect go.opentelemetry.io/contrib/propagators/b3 v1.31.0 // indirect go.opentelemetry.io/otel v1.32.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.7.0 // indirect diff --git a/service/go.mod b/service/go.mod index ea86883746b..4d671a986bd 100644 --- a/service/go.mod +++ b/service/go.mod @@ -45,6 +45,7 @@ require ( go.opentelemetry.io/collector/semconv v0.117.0 go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 go.opentelemetry.io/contrib/config v0.10.0 + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 go.opentelemetry.io/contrib/propagators/b3 v1.31.0 go.opentelemetry.io/otel v1.32.0 go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.32.0 @@ -105,7 +106,6 @@ require ( go.opentelemetry.io/collector/config/configtls v1.23.0 // indirect go.opentelemetry.io/collector/consumer/consumererror v0.117.0 // indirect go.opentelemetry.io/collector/extension/auth v0.117.0 // indirect - go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect go.opentelemetry.io/contrib/zpages v0.56.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.7.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.31.0 // indirect diff --git a/service/telemetry/metrics.go b/service/telemetry/metrics.go index 0b2690c0af1..f379e811b52 100644 --- a/service/telemetry/metrics.go +++ b/service/telemetry/metrics.go @@ -8,8 +8,10 @@ import ( "net/http" "sync" + "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/noop" + "go.opentelemetry.io/otel/sdk/instrumentation" sdkmetric "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/resource" "go.uber.org/multierr" @@ -56,6 +58,21 @@ func newMeterProvider(set meterProviderSettings, disableHighCardinality bool) (m opts = append(opts, sdkmetric.WithReader(r)) } + if set.cfg.Level != configtelemetry.LevelDetailed { + // Drop all otelhttp metrics if the level is not detailed. + opts = append(opts, sdkmetric.WithView( + sdkmetric.NewView( + sdkmetric.Instrument{ + Scope: instrumentation.Scope{ + Name: otelhttp.ScopeName, + }, + }, + sdkmetric.Stream{ + Aggregation: sdkmetric.AggregationDrop{}, + }, + ))) + } + var err error mp.MeterProvider, err = otelinit.InitOpenTelemetry(set.res, opts, disableHighCardinality) if err != nil {