Skip to content
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

Add internal localhostgate package #30774

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 69 additions & 0 deletions internal/common/localhostgate/featuregate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 CORE COUNTERPART.
// See https://github.com/open-telemetry/opentelemetry-collector/blob/main/internal/localhostgate/featuregate.go
package localhostgate // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/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),
)
}
}
57 changes: 57 additions & 0 deletions internal/common/localhostgate/featuregate_test.go
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)
})
}
}