Skip to content

Commit

Permalink
[chore] Move MetricsLevel check to MeterProvider creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mx-psi committed Jan 20, 2025
1 parent 52d1414 commit 9de9b3f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
14 changes: 2 additions & 12 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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...)

Expand Down Expand Up @@ -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{}
}
2 changes: 1 addition & 1 deletion config/confighttp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions otelcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions otelcol/otelcoltest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions service/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9de9b3f

Please sign in to comment.