Skip to content

Commit

Permalink
Revert public API changes in #10931, preserve logic (#11415)
Browse files Browse the repository at this point in the history
This PR removes APIs that expose `configtelemetry.Level`. Internal
functionality still uses the Level, but will be soon changed to use the
new Instrument.Enabled.

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Nov 16, 2024
1 parent d59bf0a commit 8e522ad
Show file tree
Hide file tree
Showing 29 changed files with 435 additions and 289 deletions.
25 changes: 25 additions & 0 deletions .chloggen/revert-10931.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: component

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

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

# (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]
4 changes: 2 additions & 2 deletions cmd/mdatagen/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,11 @@ import (
"go.opentelemetry.io/collector/config/configtelemetry"
)
// Deprecated: [v0.108.0] use LeveledMeter instead.
func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("")
}
// Deprecated: [v0.114.0] use Meter instead.
func LeveledMeter(settings component.TelemetrySettings, level configtelemetry.Level) metric.Meter {
return settings.LeveledMeterProvider(level).Meter("")
}
Expand Down Expand Up @@ -642,11 +642,11 @@ import (
"go.opentelemetry.io/collector/config/configtelemetry"
)
// Deprecated: [v0.108.0] use LeveledMeter instead.
func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("")
}
// Deprecated: [v0.114.0] use Meter instead.
func LeveledMeter(settings component.TelemetrySettings, level configtelemetry.Level) metric.Meter {
return settings.LeveledMeterProvider(level).Meter("")
}
Expand Down

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ type componentTestTelemetry struct {
{{- if or isConnector isExporter isExtension isProcessor isReceiver }}
func (tt *componentTestTelemetry) NewSettings() {{ .Status.Class }}.Settings {
set := {{ .Status.Class }}test.NewNopSettings()
set.TelemetrySettings = tt.newTelemetrySettings()
set.ID = component.NewID(component.MustNewType("{{ .Type }}"))
set.TelemetrySettings = tt.newTelemetrySettings()
return set
}
{{- end }}

func (tt *componentTestTelemetry) newTelemetrySettings() component.TelemetrySettings {
set := componenttest.NewNopTelemetrySettings()
set.MeterProvider = tt.meterProvider
set.MetricsLevel = configtelemetry.LevelDetailed
set.LeveledMeterProvider = func(_ configtelemetry.Level) metric.MeterProvider {
return tt.meterProvider
}
Expand Down
24 changes: 14 additions & 10 deletions cmd/mdatagen/internal/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"go.opentelemetry.io/collector/config/configtelemetry"
)

// Deprecated: [v0.108.0] use LeveledMeter instead.
func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("{{ .ScopeName }}")
}

// Deprecated: [v0.114.0] use Meter instead.
func LeveledMeter(settings component.TelemetrySettings, level configtelemetry.Level) metric.Meter {
return settings.LeveledMeterProvider(level).Meter("{{ .ScopeName }}")
}
Expand All @@ -44,7 +44,6 @@ type TelemetryBuilder struct {
observe{{ $name.Render }} func(context.Context, metric.Observer) error
{{- end }}
{{- end }}
meters map[configtelemetry.Level]metric.Meter
}

// TelemetryBuilderOption applies changes to default builder.
Expand All @@ -64,7 +63,7 @@ func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) {
// 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.meters[configtelemetry.Level{{ casesTitle $metric.Level.String }}].{{ $metric.Data.Instrument }}(
builder.{{ $name.Render }}, err = builder.meter.{{ $metric.Data.Instrument }}(
"otelcol_{{ $name }}",
metric.WithDescription("{{ $metric.Description }}"),
metric.WithUnit("{{ $metric.Unit }}"),
Expand All @@ -76,7 +75,7 @@ func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async
if err != nil {
return err
}
_, err = builder.meters[configtelemetry.Level{{ casesTitle $metric.Level.String }}].RegisterCallback(func(_ context.Context, o metric.Observer) error {
_, err = builder.meter.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 @@ -103,18 +102,16 @@ 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{meters: map[configtelemetry.Level]metric.Meter{}}
builder := TelemetryBuilder{}
for _, op := range options {
op.apply(&builder)
}
{{- range $level, $val := .Telemetry.Levels }}
builder.meters[configtelemetry.Level{{ casesTitle $level }}] = LeveledMeter(settings, configtelemetry.Level{{ casesTitle $level }})
{{- end }}
builder.meter = Meter(settings)
var err, errs error

{{- range $name, $metric := .Telemetry.Metrics }}
{{- if not $metric.Optional }}
builder.{{ $name.Render }}, err = builder.meters[configtelemetry.Level{{ casesTitle $metric.Level.String }}].{{ $metric.Data.Instrument }}(
builder.{{ $name.Render }}, err = getLeveledMeter(builder.meter, configtelemetry.Level{{ $metric.Level }}, settings.MetricsLevel).{{ $metric.Data.Instrument }}(
"otelcol_{{ $name }}",
metric.WithDescription("{{ $metric.Description }}{{ $metric.Stability }}"),
metric.WithUnit("{{ $metric.Unit }}"),
Expand All @@ -124,12 +121,19 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...Teleme
)
errs = errors.Join(errs, err)
{{- if $metric.Data.Async }}
_, err = builder.meters[configtelemetry.Level{{ casesTitle $metric.Level.String }}].RegisterCallback(builder.observe{{ $name.Render }}, builder.{{ $name.Render }})
_, err = getLeveledMeter(builder.meter, configtelemetry.Level{{ $metric.Level }}, settings.MetricsLevel).RegisterCallback(builder.observe{{ $name.Render }}, builder.{{ $name.Render }})
errs = errors.Join(errs, err)
{{- end }}
{{- end }}
{{- end }}
return &builder, errs
}

func getLeveledMeter(meter metric.Meter, cfgLevel, srvLevel configtelemetry.Level) metric.Meter {
if cfgLevel <= srvLevel {
return meter
}
return noop.Meter{}
}

{{- end }}
42 changes: 21 additions & 21 deletions component/componenttest/obsreporttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ package componenttest // import "go.opentelemetry.io/collector/component/compone

import (
"context"
"errors"

"go.opentelemetry.io/otel/attribute"
"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"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.uber.org/multierr"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/pdata/pcommon"
)

const (
Expand All @@ -32,12 +34,10 @@ const (
)

type TestTelemetry struct {
ts component.TelemetrySettings
id component.ID
ts component.TelemetrySettings
SpanRecorder *tracetest.SpanRecorder

reader *sdkmetric.ManualReader
meterProvider *sdkmetric.MeterProvider
reader *sdkmetric.ManualReader
}

// CheckExporterTraces checks that for the current exported values for trace exporter metrics match given values.
Expand Down Expand Up @@ -101,12 +101,9 @@ func (tts *TestTelemetry) CheckScraperMetrics(receiver component.ID, scraper com

// Shutdown unregisters any views and shuts down the SpanRecorder
func (tts *TestTelemetry) Shutdown(ctx context.Context) error {
var errs error
errs = multierr.Append(errs, tts.SpanRecorder.Shutdown(ctx))
if tts.meterProvider != nil {
errs = multierr.Append(errs, tts.meterProvider.Shutdown(ctx))
}
return errs
return errors.Join(
tts.ts.TracerProvider.(*sdktrace.TracerProvider).Shutdown(ctx),
tts.ts.MeterProvider.(*sdkmetric.MeterProvider).Shutdown(ctx))
}

// TelemetrySettings returns the TestTelemetry's TelemetrySettings
Expand All @@ -118,23 +115,26 @@ func (tts *TestTelemetry) TelemetrySettings() component.TelemetrySettings {
// The caller must pass the ID of the component being tested. The ID will be used by the CreateSettings and Check methods.
// The caller must defer a call to `Shutdown` on the returned TestTelemetry.
func SetupTelemetry(id component.ID) (TestTelemetry, error) {
sr := new(tracetest.SpanRecorder)
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))

settings := TestTelemetry{
ts: NewNopTelemetrySettings(),
id: id,
SpanRecorder: sr,
reader: sdkmetric.NewManualReader(),
SpanRecorder: new(tracetest.SpanRecorder),
}
settings.ts.TracerProvider = tp

settings.reader = sdkmetric.NewManualReader()
settings.meterProvider = sdkmetric.NewMeterProvider(
mp := sdkmetric.NewMeterProvider(
sdkmetric.WithResource(resource.Empty()),
sdkmetric.WithReader(settings.reader),
)
settings.ts.LeveledMeterProvider = func(_ configtelemetry.Level) metric.MeterProvider {
return settings.meterProvider

settings.ts = component.TelemetrySettings{
Logger: zap.NewNop(),
TracerProvider: sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(settings.SpanRecorder)),
MeterProvider: mp,
LeveledMeterProvider: func(_ configtelemetry.Level) metric.MeterProvider {
return mp
},
MetricsLevel: configtelemetry.LevelDetailed,
Resource: pcommon.NewResource(),
}

return settings, nil
Expand Down
3 changes: 1 addition & 2 deletions component/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ 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.
// Deprecated [v0.114.0]: use MeterProvider instead.
LeveledMeterProvider func(level configtelemetry.Level) metric.MeterProvider

// MetricsLevel represents the configuration value set when the collector
Expand Down
Loading

0 comments on commit 8e522ad

Please sign in to comment.