Skip to content

Commit

Permalink
[service] deprecate TelemetrySettings.MeterProvider (#10912)
Browse files Browse the repository at this point in the history
This is replaced by a LeveledMeterProvider method on the struct instead.
This
reduces the complexity from the view of component authors, in that
there's no need
to check the level before invoking the meter to record a metric.

Closes
#9510

---------

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com>
  • Loading branch information
codeboten and TylerHelmuth authored Aug 29, 2024
1 parent fa98f7d commit 7da6b61
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 216 deletions.
25 changes: 25 additions & 0 deletions .chloggen/codeboten_level-internal-telemetry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: configtelemetry

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Deprecating `TelemetrySettings.MeterProvider` in favour of `TelemetrySettings.LeveledMeterProvider`"

# One or more tracking issues or pull requests related to the change
issues: [10912]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ type metric struct {

// Attributes is the list of attributes that the metric emits.
Attributes []attributeName `mapstructure:"attributes"`

// Level specifies the minimum `configtelemetry.Level` for which
// the metric will be emitted. This only applies to internal telemetry
// configuration.
Level configtelemetry.Level `mapstructure:"level"`
}

func (m *metric) Unmarshal(parser *confmap.Conf) error {
Expand Down Expand Up @@ -237,6 +242,14 @@ type telemetry struct {
Metrics map[metricName]metric `mapstructure:"metrics"`
}

func (t telemetry) Levels() map[string]interface{} {
levels := map[string]interface{}{}
for _, m := range t.Metrics {
levels[m.Level.String()] = nil
}
return levels
}

type metadata struct {
// Type of the component.
Type string `mapstructure:"type"`
Expand Down
29 changes: 10 additions & 19 deletions cmd/mdatagen/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,18 @@ type TelemetryBuilder struct {
observe{{ $name.Render }} func(context.Context, metric.Observer) error
{{- end }}
{{- end }}
level configtelemetry.Level
meters map[configtelemetry.Level]metric.Meter
}

// telemetryBuilderOption applies changes to default builder.
type telemetryBuilderOption func(*TelemetryBuilder)

// WithLevel sets the current telemetry level for the component.
func WithLevel(lvl configtelemetry.Level) telemetryBuilderOption {
return func(builder *TelemetryBuilder) {
builder.level = lvl
}
}

{{- range $name, $metric := .Telemetry.Metrics }}
{{- if $metric.Optional }}
// Init{{ $name.Render }} configures the {{ $name.Render }} metric.
func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async -}}cb func() {{ $metric.Data.BasicType }}{{- end }}, opts ...metric.ObserveOption) error {
var err error
builder.{{ $name.Render }}, err = builder.meter.{{ $metric.Data.Instrument }}(
builder.{{ $name.Render }}, err = builder.meters[configtelemetry.Level{{ casesTitle $metric.Level.String }}].{{ $metric.Data.Instrument }}(
"otelcol_{{ $name }}",
metric.WithDescription("{{ $metric.Description }}"),
metric.WithUnit("{{ $metric.Unit }}"),
Expand All @@ -75,7 +68,7 @@ func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async
if err != nil {
return err
}
_, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error {
_, err = builder.meters[configtelemetry.Level{{ casesTitle $metric.Level.String }}].RegisterCallback(func(_ context.Context, o metric.Observer) error {
o.Observe{{ casesTitle $metric.Data.BasicType }}(builder.{{ $name.Render }}, cb(), opts...)
return nil
}, builder.{{ $name.Render }})
Expand All @@ -102,20 +95,18 @@ func With{{ $name.Render }}Callback(cb func() {{ $metric.Data.BasicType }}, opts
// NewTelemetryBuilder provides a struct with methods to update all internal telemetry
// for a component
func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) {
builder := TelemetryBuilder{level: configtelemetry.LevelBasic}
builder := TelemetryBuilder{meters: map[configtelemetry.Level]metric.Meter{}}
for _, op := range options {
op(&builder)
}
{{- range $level, $val := .Telemetry.Levels }}
builder.meters[configtelemetry.Level{{ casesTitle $level }}] = LeveledMeter(settings, configtelemetry.Level{{ casesTitle $level }})
{{- end }}
var err, errs error
if builder.level >= configtelemetry.Level{{ casesTitle .Telemetry.Level.String }} {
builder.meter = Meter(settings)
} else {
builder.meter = noop.Meter{}
}


{{- range $name, $metric := .Telemetry.Metrics }}
{{- if not $metric.Optional }}
builder.{{ $name.Render }}, err = builder.meter.{{ $metric.Data.Instrument }}(
builder.{{ $name.Render }}, err = builder.meters[configtelemetry.Level{{ casesTitle $metric.Level.String }}].{{ $metric.Data.Instrument }}(
"otelcol_{{ $name }}",
metric.WithDescription("{{ $metric.Description }}"),
metric.WithUnit("{{ $metric.Unit }}"),
Expand All @@ -125,7 +116,7 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme
)
errs = errors.Join(errs, err)
{{- if $metric.Data.Async }}
_, err = builder.meter.RegisterCallback(builder.observe{{ $name.Render }}, builder.{{ $name.Render }})
_, err = builder.meters[configtelemetry.Level{{ casesTitle $metric.Level.String }}].RegisterCallback(builder.observe{{ $name.Render }}, builder.{{ $name.Render }})
errs = errors.Join(errs, err)
{{- end }}
{{- end }}
Expand Down
6 changes: 5 additions & 1 deletion component/componenttest/nop_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package componenttest // import "go.opentelemetry.io/collector/component/componenttest"

import (
"go.opentelemetry.io/otel/metric"
noopmetric "go.opentelemetry.io/otel/metric/noop"
nooptrace "go.opentelemetry.io/otel/trace/noop"
"go.uber.org/zap"
Expand All @@ -16,7 +17,10 @@ import (
// NewNopTelemetrySettings returns a new nop telemetry settings for Create* functions.
func NewNopTelemetrySettings() component.TelemetrySettings {
return component.TelemetrySettings{
Logger: zap.NewNop(),
Logger: zap.NewNop(),
LeveledMeterProvider: func(_ configtelemetry.Level) metric.MeterProvider {
return noopmetric.NewMeterProvider()
},
TracerProvider: nooptrace.NewTracerProvider(),
MeterProvider: noopmetric.NewMeterProvider(),
MetricsLevel: configtelemetry.LevelNone,
Expand Down
6 changes: 4 additions & 2 deletions component/componenttest/obsreporttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/attribute"
otelprom "go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
Expand Down Expand Up @@ -149,7 +150,6 @@ func SetupTelemetry(id component.ID) (TestTelemetry, error) {
SpanRecorder: sr,
}
settings.ts.TracerProvider = tp
settings.ts.MetricsLevel = configtelemetry.LevelNormal

promRegOtel := prometheus.NewRegistry()

Expand All @@ -162,7 +162,9 @@ func SetupTelemetry(id component.ID) (TestTelemetry, error) {
sdkmetric.WithResource(resource.Empty()),
sdkmetric.WithReader(exp),
)
settings.ts.MeterProvider = settings.meterProvider
settings.ts.LeveledMeterProvider = func(_ configtelemetry.Level) metric.MeterProvider {
return settings.meterProvider
}

settings.prometheusChecker = &prometheusChecker{
otelHandler: promhttp.HandlerFor(promRegOtel, promhttp.HandlerOpts{}),
Expand Down
7 changes: 5 additions & 2 deletions component/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ type TelemetrySettings struct {
TracerProvider trace.TracerProvider

// MeterProvider that the factory can pass to other instrumented third-party libraries.
//
// Deprecated [v0.109.0]: use LeveledMeterProvider instead.
MeterProvider metric.MeterProvider

// LeveledMeterProvider returns a MeterProvider for a Level that the factory can
// pass to other instrumented third-party libraries.
LeveledMeterProvider func(level configtelemetry.Level) metric.MeterProvider

// MetricsLevel controls the level of detail for metrics emitted by the collector.
// Experimental: *NOTE* this field is experimental and may be changed or removed.
// MetricsLevel represents the configuration value set when the collector
// is configured. Components may use this level to decide whether it is
// appropriate to avoid computationally expensive calculations.
MetricsLevel configtelemetry.Level

// Resource contains the resource attributes for the collector's telemetry.
Expand Down
8 changes: 2 additions & 6 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,7 @@ func (gcs *ClientConfig) toDialOptions(ctx context.Context, host component.Host,
otelOpts := []otelgrpc.Option{
otelgrpc.WithTracerProvider(settings.TracerProvider),
otelgrpc.WithPropagators(otel.GetTextMapPropagator()),
}
if settings.MetricsLevel >= configtelemetry.LevelDetailed {
otelOpts = append(otelOpts, otelgrpc.WithMeterProvider(settings.MeterProvider))
otelgrpc.WithMeterProvider(settings.LeveledMeterProvider(configtelemetry.LevelDetailed)),
}

// Enable OpenTelemetry observability plugin.
Expand Down Expand Up @@ -427,9 +425,7 @@ func (gss *ServerConfig) toServerOption(host component.Host, settings component.
otelOpts := []otelgrpc.Option{
otelgrpc.WithTracerProvider(settings.TracerProvider),
otelgrpc.WithPropagators(otel.GetTextMapPropagator()),
}
if settings.MetricsLevel >= configtelemetry.LevelDetailed {
otelOpts = append(otelOpts, otelgrpc.WithMeterProvider(settings.MeterProvider))
otelgrpc.WithMeterProvider(settings.LeveledMeterProvider(configtelemetry.LevelDetailed)),
}

// Enable OpenTelemetry observability plugin.
Expand Down
9 changes: 2 additions & 7 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,8 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
otelOpts := []otelhttp.Option{
otelhttp.WithTracerProvider(settings.TracerProvider),
otelhttp.WithPropagators(otel.GetTextMapPropagator()),
otelhttp.WithMeterProvider(settings.LeveledMeterProvider(configtelemetry.LevelDetailed)),
}
if settings.MetricsLevel >= configtelemetry.LevelDetailed {
otelOpts = append(otelOpts, otelhttp.WithMeterProvider(settings.MeterProvider))
}

// wrapping http transport with otelhttp transport to enable otel instrumentation
if settings.TracerProvider != nil && settings.MeterProvider != nil {
clientTransport = otelhttp.NewTransport(clientTransport, otelOpts...)
Expand Down Expand Up @@ -460,9 +457,7 @@ func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settin
otelhttp.WithSpanNameFormatter(func(_ string, r *http.Request) string {
return r.URL.Path
}),
}
if settings.MetricsLevel >= configtelemetry.LevelDetailed {
otelOpts = append(otelOpts, otelhttp.WithMeterProvider(settings.MeterProvider))
otelhttp.WithMeterProvider(settings.LeveledMeterProvider(configtelemetry.LevelDetailed)),
}

// Enable OpenTelemetry observability plugin.
Expand Down
3 changes: 2 additions & 1 deletion config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"

Expand Down Expand Up @@ -52,7 +53,7 @@ var (
dummyID = component.MustNewID("dummy")
nonExistingID = component.MustNewID("nonexisting")
// Omit TracerProvider and MeterProvider in TelemetrySettings as otelhttp.Transport cannot be introspected
nilProvidersSettings = component.TelemetrySettings{Logger: zap.NewNop(), MetricsLevel: configtelemetry.LevelNone}
nilProvidersSettings = component.TelemetrySettings{Logger: zap.NewNop(), MetricsLevel: configtelemetry.LevelNone, LeveledMeterProvider: func(_ configtelemetry.Level) metric.MeterProvider { return nil }}
)

func TestAllHTTPClientSettings(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion config/confighttp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
go.opentelemetry.io/collector/featuregate v1.14.1
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/metric v1.29.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
golang.org/x/net v0.28.0
Expand Down Expand Up @@ -52,7 +53,6 @@ require (
go.opentelemetry.io/collector/extension v0.108.1 // indirect
go.opentelemetry.io/collector/pdata v1.14.1 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.51.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
Expand Down
Loading

0 comments on commit 7da6b61

Please sign in to comment.