Skip to content

Commit

Permalink
[chore][confighttp] Move MetricsLevel check to MeterProvider creation (
Browse files Browse the repository at this point in the history
…#12126)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Drops `otelhttp` metrics depending of level with a view instead of
passing a noop `MeterProvider`

~~I am not sure if this would have a performance impact or not.~~
Checked with Damien, this should not have any visible impact.

<!-- Issue number if applicable -->
#### Link to tracking issue
Updates #11061
  • Loading branch information
mx-psi authored Jan 22, 2025
1 parent 70f9fe9 commit 45a8024
Show file tree
Hide file tree
Showing 6 changed files with 17 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.118.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0
go.opentelemetry.io/otel v1.34.0
go.opentelemetry.io/otel/metric v1.34.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 (
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/extension v0.118.0 // indirect
go.opentelemetry.io/collector/pdata v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.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 @@ -87,6 +88,7 @@ require (
go.opentelemetry.io/collector/receiver/xreceiver v0.118.0 // indirect
go.opentelemetry.io/collector/semconv v0.118.0 // indirect
go.opentelemetry.io/contrib/bridges/otelzap v0.9.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.34.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 @@ -86,6 +87,7 @@ require (
go.opentelemetry.io/collector/semconv v0.118.0 // indirect
go.opentelemetry.io/contrib/bridges/otelzap v0.9.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.34.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.118.0
go.opentelemetry.io/contrib/bridges/otelzap v0.9.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.34.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.34.0
Expand Down Expand Up @@ -106,7 +107,6 @@ require (
go.opentelemetry.io/collector/config/configtls v1.24.0 // indirect
go.opentelemetry.io/collector/consumer/consumererror v0.118.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.118.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
9 changes: 9 additions & 0 deletions service/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
Expand Down Expand Up @@ -66,6 +67,14 @@ 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, dropViewOption(sdkmetric.Instrument{
Scope: instrumentation.Scope{Name: otelhttp.ScopeName},
},
))
}

// otel-arrow library metrics
// See https://github.com/open-telemetry/otel-arrow/blob/c39257/pkg/otel/arrow_record/consumer.go#L174-L176
if set.cfg.Level < configtelemetry.LevelNormal {
Expand Down

0 comments on commit 45a8024

Please sign in to comment.