diff --git a/.chloggen/change-prom-address-to-localhost.yaml b/.chloggen/change-prom-address-to-localhost.yaml new file mode 100644 index 00000000000..1aa4d229540 --- /dev/null +++ b/.chloggen/change-prom-address-to-localhost.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: 'breaking' + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: service/telemetry + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Change default metrics address to "localhost:8888" instead of ":8888" + +# One or more tracking issues or pull requests related to the change +issues: [11251] + +# (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: This behavior can be disabled by disabling the feature gate 'telemetry.UseLocalHostAsDefaultMetricsAddress'. + +# 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: [user, api] diff --git a/service/telemetry/config_test.go b/service/telemetry/config_test.go index 4bd7c81bcaa..9f8f9da1efd 100644 --- a/service/telemetry/config_test.go +++ b/service/telemetry/config_test.go @@ -71,7 +71,7 @@ func TestUnmarshalConfigDeprecatedAddressGateEnabled(t *testing.T) { cfg := NewFactory().CreateDefaultConfig() require.NoError(t, cm.Unmarshal(&cfg)) require.Len(t, cfg.(*Config).Metrics.Readers, 1) - assert.Equal(t, "", *cfg.(*Config).Metrics.Readers[0].Pull.Exporter.Prometheus.Host) + assert.Equal(t, "localhost", *cfg.(*Config).Metrics.Readers[0].Pull.Exporter.Prometheus.Host) assert.Equal(t, 8888, *cfg.(*Config).Metrics.Readers[0].Pull.Exporter.Prometheus.Port) } diff --git a/service/telemetry/factory.go b/service/telemetry/factory.go index 630fe49f4e4..9f25402adbe 100644 --- a/service/telemetry/factory.go +++ b/service/telemetry/factory.go @@ -19,6 +19,13 @@ import ( "go.opentelemetry.io/collector/service/internal/resource" ) +var useLocalHostAsDefaultMetricsAddressFeatureGate = featuregate.GlobalRegistry().MustRegister( + "telemetry.UseLocalHostAsDefaultMetricsAddress", + featuregate.StageBeta, + featuregate.WithRegisterFromVersion("v0.111.0"), + featuregate.WithRegisterDescription("controls whether default Prometheus metrics server use localhost as the default host for their endpoints"), +) + // disableHighCardinalityMetricsFeatureGate is the feature gate that controls whether the collector should enable // potentially high cardinality metrics. The gate will be removed when the collector allows for view configuration. var disableHighCardinalityMetricsFeatureGate = featuregate.GlobalRegistry().MustRegister( @@ -82,6 +89,11 @@ func NewFactory() Factory { } func createDefaultConfig() component.Config { + metricsHost := "localhost" + if !useLocalHostAsDefaultMetricsAddressFeatureGate.IsEnabled() { + metricsHost = "" + } + return &Config{ Logs: LogsConfig{ Level: zapcore.InfoLevel, @@ -103,7 +115,7 @@ func createDefaultConfig() component.Config { Level: configtelemetry.LevelNormal, Readers: []config.MetricReader{{ Pull: &config.PullMetricReader{Exporter: config.MetricExporter{Prometheus: &config.Prometheus{ - Host: newPtr(""), + Host: &metricsHost, Port: newPtr(8888), }}}}, }, diff --git a/service/telemetry/factory_test.go b/service/telemetry/factory_test.go index 75f4196c6ce..86ba38cd460 100644 --- a/service/telemetry/factory_test.go +++ b/service/telemetry/factory_test.go @@ -5,6 +5,7 @@ package telemetry import ( "context" + "strconv" "testing" "time" @@ -15,8 +16,40 @@ import ( "go.uber.org/zap/zapcore" "go.opentelemetry.io/collector/config/configtelemetry" + "go.opentelemetry.io/collector/featuregate" ) +func TestDefaultConfig(t *testing.T) { + tests := []struct { + expected string + gate bool + }{ + { + expected: "localhost", + gate: true, + }, + { + expected: "", + gate: false, + }, + } + + for _, tt := range tests { + t.Run("UseLocalHostAsDefaultMetricsAddress/"+strconv.FormatBool(tt.gate), func(t *testing.T) { + prev := useLocalHostAsDefaultMetricsAddressFeatureGate.IsEnabled() + require.NoError(t, featuregate.GlobalRegistry().Set(useLocalHostAsDefaultMetricsAddressFeatureGate.ID(), tt.gate)) + defer func() { + // Restore previous value. + require.NoError(t, featuregate.GlobalRegistry().Set(useLocalHostAsDefaultMetricsAddressFeatureGate.ID(), prev)) + }() + cfg := NewFactory().CreateDefaultConfig() + require.Len(t, cfg.(*Config).Metrics.Readers, 1) + assert.Equal(t, tt.expected, *cfg.(*Config).Metrics.Readers[0].Pull.Exporter.Prometheus.Host) + assert.Equal(t, 8888, *cfg.(*Config).Metrics.Readers[0].Pull.Exporter.Prometheus.Port) + }) + } +} + func TestTelemetryConfiguration(t *testing.T) { tests := []struct { name string