Skip to content

Change default metrics address to localhost:8888 #11251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/change-prom-address-to-localhost.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: '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]
2 changes: 1 addition & 1 deletion service/telemetry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 13 additions & 1 deletion service/telemetry/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -82,6 +89,11 @@ func NewFactory() Factory {
}

func createDefaultConfig() component.Config {
metricsHost := "localhost"
if !useLocalHostAsDefaultMetricsAddressFeatureGate.IsEnabled() {
metricsHost = ""
}

return &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Expand All @@ -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),
}}}},
},
Expand Down
33 changes: 33 additions & 0 deletions service/telemetry/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package telemetry

import (
"context"
"strconv"
"testing"
"time"

Expand All @@ -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
Expand Down
Loading