-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/otlp, internal, featuregate] Add featuregate to switch to l…
…ocalhost defaults for server-like components (#8622) **Description:** - Define `component.UseLocalHostAsDefaultHost` in the `internal/localhostgate` package. - Define `featuregate.ErrIsAlreadyRegistered` error, returned by `Register` when a gate is already registered. - Adds support for the localhost gate on the OTLP receiver. This PR does not remove the current warning in any way, we can remove this separately. **Link to tracking Issue:** Updates #8510 **Testing:** Adds unit tests **Documentation:** Document on OTLP receiver template and add related logging.
- Loading branch information
Showing
13 changed files
with
220 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 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 `component.UseLocalHostAsDefaultHost` feature gate that changes default endpoints from 0.0.0.0 to localhost" | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [8510] | ||
|
||
# (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: | | ||
The only component in this repository affected by this is the OTLP receiver. | ||
# 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: [] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 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: featuregate | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Add the `featuregate.ErrAlreadyRegistered` error, which is returned by `featuregate.Registry`'s `Register` when adding a feature gate that is already registered." | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [8622] | ||
|
||
# (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: | | ||
Use `errors.Is` to check for this error. | ||
# 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: [api] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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.StageAlpha, | ||
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.Warn( | ||
"The default endpoints for all servers in components will change to use localhost instead of 0.0.0.0 in a future version. Use the feature gate to preview the new default.", | ||
zap.String("feature gate ID", UseLocalHostAsDefaultHostID), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters