From 2166b11137ec473b01b6cc5a0b9bf932cb752cf2 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Wed, 11 Sep 2024 16:28:07 -0700 Subject: [PATCH] [chore] Remove internal package localhostgate, move gate definition to globalgate (#11150) Signed-off-by: Bogdan Drutu --- config/configgrpc/configgrpc_test.go | 8 +-- config/configgrpc/go.mod | 12 +--- config/confighttp/confighttp_test.go | 8 +-- config/confighttp/go.mod | 12 +--- config/internal/go.mod | 25 +------- config/internal/warning.go | 6 +- connector/connectorprofiles/go.mod | 2 - connector/forwardconnector/go.mod | 2 - connector/go.mod | 2 - exporter/otlpexporter/go.mod | 3 + exporter/otlphttpexporter/go.mod | 3 + extension/memorylimiterextension/go.mod | 2 - extension/zpagesextension/go.mod | 3 + go.mod | 4 -- go.sum | 2 - internal/globalgates/globalgates.go | 38 +++++++++++- internal/localhostgate/featuregate.go | 68 ---------------------- internal/localhostgate/featuregate_test.go | 57 ------------------ processor/memorylimiterprocessor/go.mod | 2 - receiver/go.mod | 4 +- receiver/otlpreceiver/factory.go | 16 ++++- receiver/otlpreceiver/factory_test.go | 35 +++++++++++ receiver/otlpreceiver/go.mod | 13 +++-- service/service.go | 14 ++++- 24 files changed, 134 insertions(+), 207 deletions(-) delete mode 100644 internal/localhostgate/featuregate.go delete mode 100644 internal/localhostgate/featuregate_test.go diff --git a/config/configgrpc/configgrpc_test.go b/config/configgrpc/configgrpc_test.go index a172eaaafa3..da97997545c 100644 --- a/config/configgrpc/configgrpc_test.go +++ b/config/configgrpc/configgrpc_test.go @@ -35,7 +35,7 @@ import ( "go.opentelemetry.io/collector/extension/auth" "go.opentelemetry.io/collector/extension/auth/authtest" "go.opentelemetry.io/collector/featuregate" - "go.opentelemetry.io/collector/internal/localhostgate" + "go.opentelemetry.io/collector/internal/globalgates" "go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" ) @@ -494,11 +494,11 @@ func TestUseSecure(t *testing.T) { } func TestGRPCServerWarning(t *testing.T) { - prev := localhostgate.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() - require.NoError(t, featuregate.GlobalRegistry().Set(localhostgate.UseLocalHostAsDefaultHostID, false)) + prev := globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() + require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.UseLocalHostAsDefaultHostID, false)) defer func() { // Restore previous value. - require.NoError(t, featuregate.GlobalRegistry().Set(localhostgate.UseLocalHostAsDefaultHostID, prev)) + require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.UseLocalHostAsDefaultHostID, prev)) }() tests := []struct { diff --git a/config/configgrpc/go.mod b/config/configgrpc/go.mod index 62e20a9e3f8..c5ac470b783 100644 --- a/config/configgrpc/go.mod +++ b/config/configgrpc/go.mod @@ -5,7 +5,6 @@ go 1.22.0 require ( github.com/mostynb/go-grpc-compression v1.2.3 github.com/stretchr/testify v1.9.0 - go.opentelemetry.io/collector v0.109.0 go.opentelemetry.io/collector/client v1.15.0 go.opentelemetry.io/collector/component v0.109.0 go.opentelemetry.io/collector/config/configauth v0.109.0 @@ -17,6 +16,7 @@ require ( go.opentelemetry.io/collector/config/internal v0.109.0 go.opentelemetry.io/collector/extension/auth v0.109.0 go.opentelemetry.io/collector/featuregate v1.15.0 + go.opentelemetry.io/collector/internal/globalgates v0.109.0 go.opentelemetry.io/collector/pdata v1.15.0 go.opentelemetry.io/collector/pdata/testdata v0.109.0 go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 @@ -63,8 +63,6 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace go.opentelemetry.io/collector => ../../ - replace go.opentelemetry.io/collector/client => ../../client replace go.opentelemetry.io/collector/config/configauth => ../configauth @@ -85,8 +83,6 @@ replace go.opentelemetry.io/collector/extension => ../../extension replace go.opentelemetry.io/collector/extension/auth => ../../extension/auth -replace go.opentelemetry.io/collector/confmap => ../../confmap - replace go.opentelemetry.io/collector/featuregate => ../../featuregate replace go.opentelemetry.io/collector/pdata => ../../pdata @@ -99,8 +95,4 @@ replace go.opentelemetry.io/collector/component => ../../component replace go.opentelemetry.io/collector/consumer => ../../consumer -replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles - -replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/consumertest - -replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus +replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates diff --git a/config/confighttp/confighttp_test.go b/config/confighttp/confighttp_test.go index d82cef43768..9d08b561cc7 100644 --- a/config/confighttp/confighttp_test.go +++ b/config/confighttp/confighttp_test.go @@ -35,7 +35,7 @@ import ( "go.opentelemetry.io/collector/extension/auth" "go.opentelemetry.io/collector/extension/auth/authtest" "go.opentelemetry.io/collector/featuregate" - "go.opentelemetry.io/collector/internal/localhostgate" + "go.opentelemetry.io/collector/internal/globalgates" ) type customRoundTripper struct { @@ -520,11 +520,11 @@ func TestHTTPServerSettingsError(t *testing.T) { } func TestHTTPServerWarning(t *testing.T) { - prev := localhostgate.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() - require.NoError(t, featuregate.GlobalRegistry().Set(localhostgate.UseLocalHostAsDefaultHostID, false)) + prev := globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() + require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.UseLocalHostAsDefaultHostID, false)) defer func() { // Restore previous value. - require.NoError(t, featuregate.GlobalRegistry().Set(localhostgate.UseLocalHostAsDefaultHostID, prev)) + require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.UseLocalHostAsDefaultHostID, prev)) }() tests := []struct { diff --git a/config/confighttp/go.mod b/config/confighttp/go.mod index dfe96d265b0..edad28f0bb7 100644 --- a/config/confighttp/go.mod +++ b/config/confighttp/go.mod @@ -7,7 +7,6 @@ require ( github.com/klauspost/compress v1.17.9 github.com/rs/cors v1.11.1 github.com/stretchr/testify v1.9.0 - go.opentelemetry.io/collector v0.109.0 go.opentelemetry.io/collector/client v1.15.0 go.opentelemetry.io/collector/component v0.109.0 go.opentelemetry.io/collector/config/configauth v0.109.0 @@ -18,6 +17,7 @@ require ( go.opentelemetry.io/collector/config/internal v0.109.0 go.opentelemetry.io/collector/extension/auth v0.109.0 go.opentelemetry.io/collector/featuregate v1.15.0 + go.opentelemetry.io/collector/internal/globalgates v0.109.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 go.opentelemetry.io/otel v1.30.0 go.opentelemetry.io/otel/metric v1.30.0 @@ -58,8 +58,6 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace go.opentelemetry.io/collector => ../../ - replace go.opentelemetry.io/collector/config/configauth => ../configauth replace go.opentelemetry.io/collector/config/configcompression => ../configcompression @@ -76,8 +74,6 @@ replace go.opentelemetry.io/collector/extension => ../../extension replace go.opentelemetry.io/collector/extension/auth => ../../extension/auth -replace go.opentelemetry.io/collector/confmap => ../../confmap - replace go.opentelemetry.io/collector/featuregate => ../../featuregate replace go.opentelemetry.io/collector/pdata => ../../pdata @@ -92,8 +88,4 @@ replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata replace go.opentelemetry.io/collector/pdata/pprofile => ../../pdata/pprofile -replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles - -replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/consumertest - -replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus +replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates diff --git a/config/internal/go.mod b/config/internal/go.mod index 93f0d8b3f26..8fad43d6125 100644 --- a/config/internal/go.mod +++ b/config/internal/go.mod @@ -4,7 +4,7 @@ go 1.22.0 require ( github.com/stretchr/testify v1.9.0 - go.opentelemetry.io/collector v0.109.0 + go.opentelemetry.io/collector/internal/globalgates v0.109.0 go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 ) @@ -13,31 +13,12 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/hashicorp/go-version v1.7.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect go.opentelemetry.io/collector/featuregate v1.15.0 // indirect go.uber.org/multierr v1.11.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace go.opentelemetry.io/collector => ../../ +replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates replace go.opentelemetry.io/collector/featuregate => ../../featuregate - -replace go.opentelemetry.io/collector/confmap => ../../confmap - -replace go.opentelemetry.io/collector/config/configtelemetry => ../configtelemetry - -replace go.opentelemetry.io/collector/pdata => ../../pdata - -replace go.opentelemetry.io/collector/consumer => ../../consumer - -replace go.opentelemetry.io/collector/component => ../../component - -replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata - -replace go.opentelemetry.io/collector/pdata/pprofile => ../../pdata/pprofile - -replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles - -replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/consumertest - -replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus diff --git a/config/internal/warning.go b/config/internal/warning.go index f9e32fc1c64..26eca49a7bb 100644 --- a/config/internal/warning.go +++ b/config/internal/warning.go @@ -10,7 +10,7 @@ import ( "go.uber.org/zap" - "go.opentelemetry.io/collector/internal/localhostgate" + "go.opentelemetry.io/collector/internal/globalgates" ) func shouldWarn(endpoint string) bool { @@ -38,14 +38,14 @@ func shouldWarn(endpoint string) bool { // WarnOnUnspecifiedHost emits a warning if an endpoint has an unspecified host. func WarnOnUnspecifiedHost(logger *zap.Logger, endpoint string) { - if !localhostgate.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() && shouldWarn(endpoint) { + if !globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() && shouldWarn(endpoint) { logger.Warn( "Using the 0.0.0.0 address exposes this server to every network interface, which may facilitate Denial of Service attacks. Enable the feature gate to change the default and remove this warning.", zap.String( "documentation", "https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/security-best-practices.md#safeguards-against-denial-of-service-attacks", ), - zap.String("feature gate ID", localhostgate.UseLocalHostAsDefaultHostID), + zap.String("feature gate ID", globalgates.UseLocalHostAsDefaultHostID), ) } } diff --git a/connector/connectorprofiles/go.mod b/connector/connectorprofiles/go.mod index 68c187a0a10..ff8769d5bab 100644 --- a/connector/connectorprofiles/go.mod +++ b/connector/connectorprofiles/go.mod @@ -46,8 +46,6 @@ replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/co replace go.opentelemetry.io/collector/pdata/pprofile => ../../pdata/pprofile -replace go.opentelemetry.io/collector/featuregate => ../../featuregate - replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles replace go.opentelemetry.io/collector/component/componentprofiles => ../../component/componentprofiles diff --git a/connector/forwardconnector/go.mod b/connector/forwardconnector/go.mod index c1d36c509f8..a40c074e75e 100644 --- a/connector/forwardconnector/go.mod +++ b/connector/forwardconnector/go.mod @@ -70,8 +70,6 @@ replace go.opentelemetry.io/collector/pdata => ../../pdata replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata -replace go.opentelemetry.io/collector/featuregate => ../../featuregate - replace go.opentelemetry.io/collector/consumer => ../../consumer replace go.opentelemetry.io/collector/confmap => ../../confmap diff --git a/connector/go.mod b/connector/go.mod index 3466b7f8efd..35ef7f7e441 100644 --- a/connector/go.mod +++ b/connector/go.mod @@ -63,8 +63,6 @@ replace go.opentelemetry.io/collector/confmap => ../confmap replace go.opentelemetry.io/collector/consumer => ../consumer -replace go.opentelemetry.io/collector/featuregate => ../featuregate - replace go.opentelemetry.io/collector/pdata => ../pdata replace go.opentelemetry.io/collector/pdata/testdata => ../pdata/testdata diff --git a/exporter/otlpexporter/go.mod b/exporter/otlpexporter/go.mod index bfab5732a3b..17b2f95f785 100644 --- a/exporter/otlpexporter/go.mod +++ b/exporter/otlpexporter/go.mod @@ -64,6 +64,7 @@ require ( go.opentelemetry.io/collector/extension/auth v0.109.0 // indirect go.opentelemetry.io/collector/extension/experimental/storage v0.109.0 // indirect go.opentelemetry.io/collector/featuregate v1.15.0 // indirect + go.opentelemetry.io/collector/internal/globalgates v0.109.0 // indirect go.opentelemetry.io/collector/pdata/pprofile v0.109.0 // indirect go.opentelemetry.io/collector/receiver v0.109.0 // indirect go.opentelemetry.io/collector/receiver/receiverprofiles v0.109.0 // indirect @@ -141,3 +142,5 @@ replace go.opentelemetry.io/collector/component/componentstatus => ../../compone replace go.opentelemetry.io/collector/receiver/receiverprofiles => ../../receiver/receiverprofiles replace go.opentelemetry.io/collector/exporter/exporterprofiles => ../exporterprofiles + +replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates diff --git a/exporter/otlphttpexporter/go.mod b/exporter/otlphttpexporter/go.mod index 110dc40012b..fd09a924fcb 100644 --- a/exporter/otlphttpexporter/go.mod +++ b/exporter/otlphttpexporter/go.mod @@ -63,6 +63,7 @@ require ( go.opentelemetry.io/collector/extension/auth v0.109.0 // indirect go.opentelemetry.io/collector/extension/experimental/storage v0.109.0 // indirect go.opentelemetry.io/collector/featuregate v1.15.0 // indirect + go.opentelemetry.io/collector/internal/globalgates v0.109.0 // indirect go.opentelemetry.io/collector/pdata/pprofile v0.109.0 // indirect go.opentelemetry.io/collector/receiver v0.109.0 // indirect go.opentelemetry.io/collector/receiver/receiverprofiles v0.109.0 // indirect @@ -138,3 +139,5 @@ replace go.opentelemetry.io/collector/component/componentstatus => ../../compone replace go.opentelemetry.io/collector/receiver/receiverprofiles => ../../receiver/receiverprofiles replace go.opentelemetry.io/collector/exporter/exporterprofiles => ../exporterprofiles + +replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates diff --git a/extension/memorylimiterextension/go.mod b/extension/memorylimiterextension/go.mod index 3a2820faa98..bed21c8dc20 100644 --- a/extension/memorylimiterextension/go.mod +++ b/extension/memorylimiterextension/go.mod @@ -66,8 +66,6 @@ replace go.opentelemetry.io/collector/confmap => ../../confmap replace go.opentelemetry.io/collector/extension => ../../extension -replace go.opentelemetry.io/collector/featuregate => ../../featuregate - replace go.opentelemetry.io/collector/pdata => ../../pdata replace go.opentelemetry.io/collector/consumer => ../../consumer diff --git a/extension/zpagesextension/go.mod b/extension/zpagesextension/go.mod index 292bea8f9a4..6632d81b00a 100644 --- a/extension/zpagesextension/go.mod +++ b/extension/zpagesextension/go.mod @@ -52,6 +52,7 @@ require ( go.opentelemetry.io/collector/config/internal v0.109.0 // indirect go.opentelemetry.io/collector/extension/auth v0.109.0 // indirect go.opentelemetry.io/collector/featuregate v1.15.0 // indirect + go.opentelemetry.io/collector/internal/globalgates v0.109.0 // indirect go.opentelemetry.io/collector/pdata v1.15.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect go.opentelemetry.io/otel v1.30.0 // indirect @@ -114,3 +115,5 @@ replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/co replace go.opentelemetry.io/collector/client => ../../client replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus + +replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates diff --git a/go.mod b/go.mod index c8a51bfb90e..5ff241af35e 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,6 @@ require ( go.opentelemetry.io/collector/consumer v0.109.0 go.opentelemetry.io/collector/consumer/consumerprofiles v0.109.0 go.opentelemetry.io/collector/consumer/consumertest v0.109.0 - go.opentelemetry.io/collector/featuregate v1.15.0 go.opentelemetry.io/collector/pdata v1.15.0 go.opentelemetry.io/collector/pdata/pprofile v0.109.0 go.opentelemetry.io/collector/pdata/testdata v0.109.0 @@ -39,7 +38,6 @@ require ( github.com/go-viper/mapstructure/v2 v2.1.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/hashicorp/go-version v1.7.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect @@ -88,8 +86,6 @@ replace go.opentelemetry.io/collector/consumer => ./consumer replace go.opentelemetry.io/collector/consumer/consumertest => ./consumer/consumertest -replace go.opentelemetry.io/collector/featuregate => ./featuregate - replace go.opentelemetry.io/collector/pdata => ./pdata replace go.opentelemetry.io/collector/pdata/testdata => ./pdata/testdata diff --git a/go.sum b/go.sum index 31d3f1bd6c4..556ddb58cb8 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,6 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= -github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= diff --git a/internal/globalgates/globalgates.go b/internal/globalgates/globalgates.go index 0f36ecad4ac..3a44f1fdb61 100644 --- a/internal/globalgates/globalgates.go +++ b/internal/globalgates/globalgates.go @@ -3,10 +3,46 @@ package globalgates // import "go.opentelemetry.io/collector/internal/globalgates" -import "go.opentelemetry.io/collector/featuregate" +import ( + "errors" + + "go.opentelemetry.io/collector/featuregate" +) var NoopTracerProvider = featuregate.GlobalRegistry().MustRegister("service.noopTracerProvider", featuregate.StageAlpha, featuregate.WithRegisterFromVersion("v0.107.0"), featuregate.WithRegisterToVersion("v0.109.0"), featuregate.WithRegisterDescription("Sets a Noop OpenTelemetry TracerProvider to reduce memory allocations. This featuregate is incompatible with the zPages extension.")) + +const UseLocalHostAsDefaultHostID = "component.UseLocalHostAsDefaultHost" + +// UseLocalHostAsDefaultHostfeatureGate is the feature gate that controls whether +// server-like receivers and extensions such as the OTLP receiver use localhost as the default host for their endpoints. +var UseLocalHostAsDefaultHostfeatureGate = mustRegisterOrLoad( + featuregate.GlobalRegistry(), + UseLocalHostAsDefaultHostID, + featuregate.StageBeta, + featuregate.WithRegisterDescription("controls whether server-like receivers and extensions such as the OTLP receiver use localhost as the default host for their endpoints"), +) + +// mustRegisterOrLoad tries to register the feature gate and loads it if it already exists. +// It panics on any other error. +func mustRegisterOrLoad(reg *featuregate.Registry, id string, stage featuregate.Stage, opts ...featuregate.RegisterOption) *featuregate.Gate { + gate, err := reg.Register(id, stage, opts...) + + if errors.Is(err, featuregate.ErrAlreadyRegistered) { + // Gate is already registered; find it. + // Only a handful of feature gates are registered, so it's fine to iterate over all of them. + reg.VisitAll(func(g *featuregate.Gate) { + if g.ID() == id { + gate = g + return + } + }) + } else if err != nil { + panic(err) + } + + return gate +} diff --git a/internal/localhostgate/featuregate.go b/internal/localhostgate/featuregate.go deleted file mode 100644 index 568bbcdd786..00000000000 --- a/internal/localhostgate/featuregate.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -// package localhostgate defines a feature gate that controls whether server-like receivers and extensions use localhost as the default host for their endpoints. -// This package is duplicated across core and contrib to avoid exposing the feature gate as part of the public API. -// To do this we define a `registerOrLoad` helper and try to register the gate in both modules. -// IMPORTANT NOTE: ANY CHANGES TO THIS PACKAGE MUST BE MIRRORED IN THE CONTRIB COUNTERPART. -package localhostgate // import "go.opentelemetry.io/collector/internal/localhostgate" - -import ( - "errors" - "fmt" - - "go.uber.org/zap" - - "go.opentelemetry.io/collector/featuregate" -) - -const UseLocalHostAsDefaultHostID = "component.UseLocalHostAsDefaultHost" - -// UseLocalHostAsDefaultHostfeatureGate is the feature gate that controls whether -// server-like receivers and extensions such as the OTLP receiver use localhost as the default host for their endpoints. -var UseLocalHostAsDefaultHostfeatureGate = mustRegisterOrLoad( - featuregate.GlobalRegistry(), - UseLocalHostAsDefaultHostID, - featuregate.StageBeta, - featuregate.WithRegisterDescription("controls whether server-like receivers and extensions such as the OTLP receiver use localhost as the default host for their endpoints"), -) - -// mustRegisterOrLoad tries to register the feature gate and loads it if it already exists. -// It panics on any other error. -func mustRegisterOrLoad(reg *featuregate.Registry, id string, stage featuregate.Stage, opts ...featuregate.RegisterOption) *featuregate.Gate { - gate, err := reg.Register(id, stage, opts...) - - if errors.Is(err, featuregate.ErrAlreadyRegistered) { - // Gate is already registered; find it. - // Only a handful of feature gates are registered, so it's fine to iterate over all of them. - reg.VisitAll(func(g *featuregate.Gate) { - if g.ID() == id { - gate = g - return - } - }) - } else if err != nil { - panic(err) - } - - return gate -} - -// EndpointForPort gets the endpoint for a given port using localhost or 0.0.0.0 depending on the feature gate. -func EndpointForPort(port int) string { - host := "localhost" - if !UseLocalHostAsDefaultHostfeatureGate.IsEnabled() { - host = "0.0.0.0" - } - return fmt.Sprintf("%s:%d", host, port) -} - -// LogAboutUseLocalHostAsDefault logs about the upcoming change from 0.0.0.0 to localhost on server-like components. -func LogAboutUseLocalHostAsDefault(logger *zap.Logger) { - if UseLocalHostAsDefaultHostfeatureGate.IsEnabled() { - logger.Info( - "The default endpoints for all servers in components have changed to use localhost instead of 0.0.0.0. Disable the feature gate to temporarily revert to the previous default.", - zap.String("feature gate ID", UseLocalHostAsDefaultHostID), - ) - } -} diff --git a/internal/localhostgate/featuregate_test.go b/internal/localhostgate/featuregate_test.go deleted file mode 100644 index bde051c4f15..00000000000 --- a/internal/localhostgate/featuregate_test.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package localhostgate - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/collector/featuregate" -) - -func setFeatureGateForTest(t testing.TB, gate *featuregate.Gate, enabled bool) func() { - originalValue := gate.IsEnabled() - require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), enabled)) - return func() { - require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), originalValue)) - } -} - -func TestEndpointForPort(t *testing.T) { - tests := []struct { - port int - enabled bool - endpoint string - }{ - { - port: 4317, - enabled: false, - endpoint: "0.0.0.0:4317", - }, - { - port: 4317, - enabled: true, - endpoint: "localhost:4317", - }, - { - port: 0, - enabled: false, - endpoint: "0.0.0.0:0", - }, - { - port: 0, - enabled: true, - endpoint: "localhost:0", - }, - } - - for _, tt := range tests { - t.Run(tt.endpoint, func(t *testing.T) { - defer setFeatureGateForTest(t, UseLocalHostAsDefaultHostfeatureGate, tt.enabled)() - assert.Equal(t, EndpointForPort(tt.port), tt.endpoint) - }) - } -} diff --git a/processor/memorylimiterprocessor/go.mod b/processor/memorylimiterprocessor/go.mod index 8fb635fc96a..32e125c5222 100644 --- a/processor/memorylimiterprocessor/go.mod +++ b/processor/memorylimiterprocessor/go.mod @@ -76,8 +76,6 @@ replace go.opentelemetry.io/collector/component => ../../component replace go.opentelemetry.io/collector/confmap => ../../confmap -replace go.opentelemetry.io/collector/featuregate => ../../featuregate - replace go.opentelemetry.io/collector/pdata => ../../pdata replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata diff --git a/receiver/go.mod b/receiver/go.mod index b51fbab7e57..4a3b2b2f7ee 100644 --- a/receiver/go.mod +++ b/receiver/go.mod @@ -58,8 +58,6 @@ replace go.opentelemetry.io/collector/pdata => ../pdata replace go.opentelemetry.io/collector/pdata/testdata => ../pdata/testdata -retract v0.76.0 // Depends on retracted pdata v1.0.0-rc10 module - replace go.opentelemetry.io/collector/config/configtelemetry => ../config/configtelemetry replace go.opentelemetry.io/collector/pdata/pprofile => ../pdata/pprofile @@ -69,3 +67,5 @@ replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../consumer/c replace go.opentelemetry.io/collector/consumer/consumertest => ../consumer/consumertest replace go.opentelemetry.io/collector/receiver/receiverprofiles => ./receiverprofiles + +retract v0.76.0 // Depends on retracted pdata v1.0.0-rc10 module diff --git a/receiver/otlpreceiver/factory.go b/receiver/otlpreceiver/factory.go index 22181929879..8ba2e6d9092 100644 --- a/receiver/otlpreceiver/factory.go +++ b/receiver/otlpreceiver/factory.go @@ -5,13 +5,14 @@ package otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlprecei import ( "context" + "fmt" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configgrpc" "go.opentelemetry.io/collector/config/confighttp" "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/consumer" - "go.opentelemetry.io/collector/internal/localhostgate" + "go.opentelemetry.io/collector/internal/globalgates" "go.opentelemetry.io/collector/internal/sharedcomponent" "go.opentelemetry.io/collector/receiver" "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/metadata" @@ -43,7 +44,7 @@ func createDefaultConfig() component.Config { Protocols: Protocols{ GRPC: &configgrpc.ServerConfig{ NetAddr: confignet.AddrConfig{ - Endpoint: localhostgate.EndpointForPort(grpcPort), + Endpoint: endpointForPort(globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled(), grpcPort), Transport: confignet.TransportTypeTCP, }, // We almost write 0 bytes, so no need to tune WriteBufferSize. @@ -51,7 +52,7 @@ func createDefaultConfig() component.Config { }, HTTP: &HTTPConfig{ ServerConfig: &confighttp.ServerConfig{ - Endpoint: localhostgate.EndpointForPort(httpPort), + Endpoint: endpointForPort(globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled(), httpPort), }, TracesURLPath: defaultTracesURLPath, MetricsURLPath: defaultMetricsURLPath, @@ -137,3 +138,12 @@ func createLog( // When the receiver is shutdown it should be removed from this map so the same configuration // can be recreated successfully. var receivers = sharedcomponent.NewMap[*Config, *otlpReceiver]() + +// endpointForPort gets the endpoint for a given port using localhost or 0.0.0.0 depending on the feature gate. +func endpointForPort(useLocalHostAsDefault bool, port int) string { + host := "localhost" + if !useLocalHostAsDefault { + host = "0.0.0.0" + } + return fmt.Sprintf("%s:%d", host, port) +} diff --git a/receiver/otlpreceiver/factory_test.go b/receiver/otlpreceiver/factory_test.go index 02a6bf63a76..530c61548c3 100644 --- a/receiver/otlpreceiver/factory_test.go +++ b/receiver/otlpreceiver/factory_test.go @@ -331,3 +331,38 @@ func TestCreateLogReceiver(t *testing.T) { }) } } + +func TestEndpointForPort(t *testing.T) { + tests := []struct { + port int + enabled bool + endpoint string + }{ + { + port: 4317, + enabled: false, + endpoint: "0.0.0.0:4317", + }, + { + port: 4317, + enabled: true, + endpoint: "localhost:4317", + }, + { + port: 0, + enabled: false, + endpoint: "0.0.0.0:0", + }, + { + port: 0, + enabled: true, + endpoint: "localhost:0", + }, + } + + for _, tt := range tests { + t.Run(tt.endpoint, func(t *testing.T) { + assert.Equal(t, endpointForPort(tt.enabled, tt.port), tt.endpoint) + }) + } +} diff --git a/receiver/otlpreceiver/go.mod b/receiver/otlpreceiver/go.mod index 1fe4aaba38c..40182efcd8a 100644 --- a/receiver/otlpreceiver/go.mod +++ b/receiver/otlpreceiver/go.mod @@ -17,6 +17,7 @@ require ( go.opentelemetry.io/collector/confmap v1.15.0 go.opentelemetry.io/collector/consumer v0.109.0 go.opentelemetry.io/collector/consumer/consumertest v0.109.0 + go.opentelemetry.io/collector/internal/globalgates v0.109.0 go.opentelemetry.io/collector/pdata v1.15.0 go.opentelemetry.io/collector/pdata/testdata v0.109.0 go.opentelemetry.io/collector/receiver v0.109.0 @@ -111,6 +112,8 @@ replace go.opentelemetry.io/collector/extension/auth => ../../extension/auth replace go.opentelemetry.io/collector/featuregate => ../../featuregate +replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates + replace go.opentelemetry.io/collector/pdata => ../../pdata replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata @@ -119,11 +122,6 @@ replace go.opentelemetry.io/collector/receiver => ../ replace go.opentelemetry.io/collector/consumer => ../../consumer -retract ( - v0.76.0 // Depends on retracted pdata v1.0.0-rc10 module, use v0.76.1 - v0.69.0 // Release failed, use v0.69.1 -) - replace go.opentelemetry.io/collector/pdata/pprofile => ../../pdata/pprofile replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles @@ -135,3 +133,8 @@ replace go.opentelemetry.io/collector/client => ../../client replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus replace go.opentelemetry.io/collector/receiver/receiverprofiles => ../receiverprofiles + +retract ( + v0.76.0 // Depends on retracted pdata v1.0.0-rc10 module, use v0.76.1 + v0.69.0 // Release failed, use v0.69.1 +) diff --git a/service/service.go b/service/service.go index 0eb7a9f0149..29bd3d57e92 100644 --- a/service/service.go +++ b/service/service.go @@ -24,7 +24,7 @@ import ( "go.opentelemetry.io/collector/exporter" "go.opentelemetry.io/collector/extension" "go.opentelemetry.io/collector/featuregate" - "go.opentelemetry.io/collector/internal/localhostgate" + "go.opentelemetry.io/collector/internal/globalgates" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/processor" "go.opentelemetry.io/collector/receiver" @@ -251,7 +251,7 @@ func (srv *Service) Start(ctx context.Context) error { } srv.telemetrySettings.Logger.Info("Everything is ready. Begin running and processing data.") - localhostgate.LogAboutUseLocalHostAsDefault(srv.telemetrySettings.Logger) + logAboutUseLocalHostAsDefault(srv.telemetrySettings.Logger) return nil } @@ -357,3 +357,13 @@ func pdataFromSdk(res *sdkresource.Resource) pcommon.Resource { } return pcommonRes } + +// logAboutUseLocalHostAsDefault logs about the upcoming change from 0.0.0.0 to localhost on server-like components. +func logAboutUseLocalHostAsDefault(logger *zap.Logger) { + if globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() { + logger.Info( + "The default endpoints for all servers in components have changed to use localhost instead of 0.0.0.0. Disable the feature gate to temporarily revert to the previous default.", + zap.String("feature gate ID", globalgates.UseLocalHostAsDefaultHostID), + ) + } +}