From 3c1cf33f8c532d2f8d768c12dcca674402486225 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:57:12 -0700 Subject: [PATCH] [component] add LeveledMeterProvider (#10931) This func will MeterProvider and MetricsLevel in the near future. Split from https://github.com/open-telemetry/opentelemetry-collector/pull/10912 --------- Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- .../codeboten_leveled-meter-split-2.yaml | 25 +++++++++++++++++++ component/telemetry.go | 4 +++ processor/processorhelper/obsreport_test.go | 11 ++++++++ .../proctelemetry/process_telemetry_test.go | 5 ++++ service/service.go | 7 ++++++ 5 files changed, 52 insertions(+) create mode 100644 .chloggen/codeboten_leveled-meter-split-2.yaml diff --git a/.chloggen/codeboten_leveled-meter-split-2.yaml b/.chloggen/codeboten_leveled-meter-split-2.yaml new file mode 100644 index 00000000000..72419990771 --- /dev/null +++ b/.chloggen/codeboten_leveled-meter-split-2.yaml @@ -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: enhancement + +# 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: "Add `TelemetrySettings.LeveledMeterProvider` func to replace MetricsLevel in the near future" + +# One or more tracking issues or pull requests related to the change +issues: [10931] + +# (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: [] diff --git a/component/telemetry.go b/component/telemetry.go index a3d153c7050..3d33efbdab0 100644 --- a/component/telemetry.go +++ b/component/telemetry.go @@ -24,6 +24,10 @@ type TelemetrySettings struct { // MeterProvider that the factory can pass to other instrumented third-party libraries. 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 configtelemetry.Level diff --git a/processor/processorhelper/obsreport_test.go b/processor/processorhelper/obsreport_test.go index 57ae4d04184..63e88bb5ced 100644 --- a/processor/processorhelper/obsreport_test.go +++ b/processor/processorhelper/obsreport_test.go @@ -9,6 +9,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/metric/noop" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" @@ -217,6 +219,9 @@ func TestNoMetrics(t *testing.T) { set := tt.TelemetrySettings() set.MetricsLevel = configtelemetry.LevelNone + set.LeveledMeterProvider = func(_ configtelemetry.Level) metric.MeterProvider { + return noop.MeterProvider{} + } por, err := NewObsReport(ObsReportSettings{ ProcessorID: processorID, @@ -239,6 +244,9 @@ func TestNoMetrics(t *testing.T) { set := tt.TelemetrySettings() set.MetricsLevel = configtelemetry.LevelNone + set.LeveledMeterProvider = func(_ configtelemetry.Level) metric.MeterProvider { + return noop.MeterProvider{} + } por, err := NewObsReport(ObsReportSettings{ ProcessorID: processorID, @@ -261,6 +269,9 @@ func TestNoMetrics(t *testing.T) { set := tt.TelemetrySettings() set.MetricsLevel = configtelemetry.LevelNone + set.LeveledMeterProvider = func(_ configtelemetry.Level) metric.MeterProvider { + return noop.MeterProvider{} + } por, err := NewObsReport(ObsReportSettings{ ProcessorID: processorID, diff --git a/service/internal/proctelemetry/process_telemetry_test.go b/service/internal/proctelemetry/process_telemetry_test.go index 5a1e2db24b4..1367192db0f 100644 --- a/service/internal/proctelemetry/process_telemetry_test.go +++ b/service/internal/proctelemetry/process_telemetry_test.go @@ -17,6 +17,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" 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" @@ -56,6 +57,10 @@ func setupTelemetry(t *testing.T) testTelemetry { ) settings.TelemetrySettings.MeterProvider = settings.meterProvider + settings.TelemetrySettings.LeveledMeterProvider = func(_ configtelemetry.Level) metric.MeterProvider { + return settings.meterProvider + } + settings.promHandler = promhttp.HandlerFor(promReg, promhttp.HandlerOpts{}) t.Cleanup(func() { assert.NoError(t, settings.meterProvider.Shutdown(context.Background())) }) diff --git a/service/service.go b/service/service.go index a4d18690429..5496021b266 100644 --- a/service/service.go +++ b/service/service.go @@ -12,6 +12,7 @@ import ( "runtime" "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/metric/noop" sdkresource "go.opentelemetry.io/otel/sdk/resource" "go.uber.org/multierr" "go.uber.org/zap" @@ -127,6 +128,12 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) { logsAboutMeterProvider(logger, cfg.Telemetry.Metrics, mp, extendedConfig) srv.telemetrySettings = component.TelemetrySettings{ + LeveledMeterProvider: func(level configtelemetry.Level) metric.MeterProvider { + if level <= cfg.Telemetry.Metrics.Level { + return mp + } + return noop.MeterProvider{} + }, Logger: logger, MeterProvider: mp, TracerProvider: tracerProvider,