diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fd8a76a59f..4882149db78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Deprecated `receiverhelper.WithMetrics` in favour of `component.WithMetricsReceiver` - Deprecated `receiverhelper.WithLogs` in favour of `component.WithLogsReceiver` - Deprecated `receiverhelper.NewFactory` in favour of `component.NewReceiverFactory` +- Change otel collector to enable open telemetry metrics through feature gate instead of a constant - Remove support for legacy otlp/http port. (#4916) ### 💡 Enhancements 💡 diff --git a/config/configtelemetry/configtelemetry.go b/config/configtelemetry/configtelemetry.go index 01584f11cf4..4a1fdce7d1b 100644 --- a/config/configtelemetry/configtelemetry.go +++ b/config/configtelemetry/configtelemetry.go @@ -36,6 +36,8 @@ const ( levelDetailedStr = "detailed" ) +// Deprecated: UseOpenTelemetryForInternalMetrics has been deprecated. Uses feature flag in +// telemetry.useOtelForInternalMetrics to handle this feature const UseOpenTelemetryForInternalMetrics = false // Level is the level of internal telemetry (metrics, logs, traces about the component itself) diff --git a/service/collector_test.go b/service/collector_test.go index 27a80f1bb1d..4d9b4ec19ed 100644 --- a/service/collector_test.go +++ b/service/collector_test.go @@ -38,6 +38,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/internal/testcomponents" "go.opentelemetry.io/collector/internal/testutil" + "go.opentelemetry.io/collector/service/featuregate" ) // TestCollector_StartAsGoRoutine must be the first unit test on the file, @@ -80,7 +81,7 @@ func TestCollector_StartAsGoRoutine(t *testing.T) { }, time.Second*2, time.Millisecond*200) } -func TestCollector_Start(t *testing.T) { +func testCollectorStartHelper(t *testing.T) { factories, err := testcomponents.NewDefaultFactories() require.NoError(t, err) var once sync.Once @@ -135,6 +136,31 @@ func TestCollector_Start(t *testing.T) { }, time.Second*2, time.Millisecond*200) } +// as telemetry instance is initialized only once, we need to reset it before each test so the metrics endpoint can +// have correct handler spawned +func resetCollectorTelemetry() { + collectorTelemetry = &colTelemetry{} +} + +func TestCollector_Start(t *testing.T) { + resetCollectorTelemetry() + testCollectorStartHelper(t) +} + +func TestCollector_StartWithOtelInternalMetrics(t *testing.T) { + resetCollectorTelemetry() + originalFlag := featuregate.IsEnabled(useOtelForInternalMetricsfeatureGateID) + defer func() { + featuregate.Apply(map[string]bool{ + useOtelForInternalMetricsfeatureGateID: originalFlag, + }) + }() + featuregate.Apply(map[string]bool{ + useOtelForInternalMetricsfeatureGateID: true, + }) + testCollectorStartHelper(t) +} + // TestCollector_ShutdownNoop verifies that shutdown can be called even if a collector // has yet to be started and it will execute without error. func TestCollector_ShutdownNoop(t *testing.T) { diff --git a/service/telemetry.go b/service/telemetry.go index 88e17fe3aa7..daed07bd04b 100644 --- a/service/telemetry.go +++ b/service/telemetry.go @@ -38,6 +38,7 @@ import ( "go.opentelemetry.io/collector/internal/version" semconv "go.opentelemetry.io/collector/model/semconv/v1.5.0" "go.opentelemetry.io/collector/processor/batchprocessor" + "go.opentelemetry.io/collector/service/featuregate" telemetry2 "go.opentelemetry.io/collector/service/internal/telemetry" ) @@ -50,8 +51,21 @@ const AddCollectorVersionTag = true const ( zapKeyTelemetryAddress = "address" zapKeyTelemetryLevel = "level" + + // useOtelForInternalMetricsfeatureGateID is the feature gate ID that controls whether the collector uses open + // telemetry for internal metrics. + useOtelForInternalMetricsfeatureGateID = "telemetry.useOtelForInternalMetrics" ) +func init() { + //nolint:staticcheck + featuregate.Register(featuregate.Gate{ + ID: useOtelForInternalMetricsfeatureGateID, + Description: "controls whether the collector to uses open telemetry for internal metrics", + Enabled: configtelemetry.UseOpenTelemetryForInternalMetrics, + }) +} + type collectorTelemetryExporter interface { init(col *Collector) error shutdown() error @@ -98,7 +112,7 @@ func (tel *colTelemetry) initOnce(col *Collector) error { instanceID := instanceUUID.String() var pe http.Handler - if configtelemetry.UseOpenTelemetryForInternalMetrics { + if featuregate.IsEnabled(useOtelForInternalMetricsfeatureGateID) { otelHandler, err := tel.initOpenTelemetry() if err != nil { return err