Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
- obsReport.NewExporter accepts a settings struct (#2668)

## 💡 Enhancements 💡

- `batch` processor: - Support max batch size for logs (#2736)
- Use `Endpoint` for health check extension (#2782)

## 🧰 Bug fixes 🧰

Expand Down
3 changes: 2 additions & 1 deletion extension/healthcheckextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ liveness and/or readiness probe on Kubernetes.

The following settings are required:

- `port` (default = 13133): What port to expose HTTP health information.
- `endpoint` (default = localhost:13133): Address to publish the health check status to
- `port` (default = 13133): [deprecated] What port to expose HTTP health information.

Example:

Expand Down
7 changes: 7 additions & 0 deletions extension/healthcheckextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package healthcheckextension

import (
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/confignet"
)

// Config has the configuration for the extension enabling the health check
Expand All @@ -25,5 +26,11 @@ type Config struct {

// Port is the port used to publish the health check status.
// The default value is 13133.
// Deprecated: use Endpoint instead.
Port uint16 `mapstructure:"port"`

// TCPAddr represents a tcp endpoint address that is to publish the health
// check status.
// The default endpoint is "localhost:13133".
TCPAddr confignet.TCPAddr `mapstructure:",squash"`
}
5 changes: 4 additions & 1 deletion extension/healthcheckextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtest"
)

Expand All @@ -47,7 +48,9 @@ func TestLoadConfig(t *testing.T) {
TypeVal: "health_check",
NameVal: "health_check/1",
},
Port: 13,
TCPAddr: confignet.TCPAddr{
Endpoint: "localhost:13",
},
},
ext1)

Expand Down
7 changes: 6 additions & 1 deletion extension/healthcheckextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/extension/extensionhelper"
)

const (
// The value of extension "type" in configuration.
typeStr = "health_check"

defaultEndpoint = "localhost:13133"
)

// NewFactory creates a factory for HealthCheck extension.
Expand All @@ -41,7 +44,9 @@ func createDefaultConfig() configmodels.Extension {
TypeVal: typeStr,
NameVal: typeStr,
},
Port: 13133,
TCPAddr: confignet.TCPAddr{
Endpoint: defaultEndpoint,
},
}
}

Expand Down
10 changes: 6 additions & 4 deletions extension/healthcheckextension/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configcheck"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/testutil"
)

Expand All @@ -35,9 +36,10 @@ func TestFactory_CreateDefaultConfig(t *testing.T) {
NameVal: typeStr,
TypeVal: typeStr,
},
Port: 13133,
},
cfg)
TCPAddr: confignet.TCPAddr{
Endpoint: defaultEndpoint,
},
}, cfg)

assert.NoError(t, configcheck.ValidateConfig(cfg))
ext, err := createExtension(context.Background(), component.ExtensionCreateParams{Logger: zap.NewNop()}, cfg)
Expand All @@ -47,7 +49,7 @@ func TestFactory_CreateDefaultConfig(t *testing.T) {

func TestFactory_CreateExtension(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.Port = testutil.GetAvailablePort(t)
cfg.TCPAddr.Endpoint = testutil.GetAvailableLocalAddress(t)

ext, err := createExtension(context.Background(), component.ExtensionCreateParams{Logger: zap.NewNop()}, cfg)
require.NoError(t, err)
Expand Down
13 changes: 11 additions & 2 deletions extension/healthcheckextension/healthcheckextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ func (hc *healthCheckExtension) Start(_ context.Context, host component.Host) er
hc.logger.Info("Starting health_check extension", zap.Any("config", hc.config))

// Initialize listener
portStr := ":" + strconv.Itoa(int(hc.config.Port))
ln, err := net.Listen("tcp", portStr)
var (
ln net.Listener
err error
)
if hc.config.Port != 0 && hc.config.TCPAddr.Endpoint == defaultEndpoint {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also log a deprecation warning for users to change to endpoint :)

hc.logger.Warn("`Port` is deprecated, use `Endpoint` instead")
portStr := ":" + strconv.Itoa(int(hc.config.Port))
ln, err = net.Listen("tcp", portStr)
} else {
ln, err = hc.config.TCPAddr.Listen()
}
if err != nil {
return err
}
Expand Down
31 changes: 18 additions & 13 deletions extension/healthcheckextension/healthcheckextension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"net"
"net/http"
"runtime"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -28,12 +27,15 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/testutil"
)

func TestHealthCheckExtensionUsage(t *testing.T) {
config := Config{
Port: testutil.GetAvailablePort(t),
TCPAddr: confignet.TCPAddr{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}

hcExt := newServer(config, zap.NewNop())
Expand All @@ -46,7 +48,7 @@ func TestHealthCheckExtensionUsage(t *testing.T) {
runtime.Gosched()

client := &http.Client{}
url := "http://localhost:" + strconv.Itoa(int(config.Port))
url := "http://" + config.TCPAddr.Endpoint
resp0, err := client.Get(url)
require.NoError(t, err)
defer resp0.Body.Close()
Expand All @@ -68,21 +70,18 @@ func TestHealthCheckExtensionUsage(t *testing.T) {

func TestHealthCheckExtensionPortAlreadyInUse(t *testing.T) {
endpoint := testutil.GetAvailableLocalAddress(t)
_, portStr, err := net.SplitHostPort(endpoint)
require.NoError(t, err)

// This needs to be ":port" because health checks also tries to connect to ":port".
// To avoid the pop-up "accept incoming network connections" health check should be changed
// to accept an address.
ln, err := net.Listen("tcp", ":"+portStr)
ln, err := net.Listen("tcp", endpoint)
require.NoError(t, err)
defer ln.Close()

port, err := strconv.Atoi(portStr)
require.NoError(t, err)

config := Config{
Port: uint16(port),
TCPAddr: confignet.TCPAddr{
Endpoint: endpoint,
},
}
hcExt := newServer(config, zap.NewNop())
require.NotNil(t, hcExt)
Expand All @@ -93,7 +92,9 @@ func TestHealthCheckExtensionPortAlreadyInUse(t *testing.T) {

func TestHealthCheckMultipleStarts(t *testing.T) {
config := Config{
Port: testutil.GetAvailablePort(t),
TCPAddr: confignet.TCPAddr{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}

hcExt := newServer(config, zap.NewNop())
Expand All @@ -108,7 +109,9 @@ func TestHealthCheckMultipleStarts(t *testing.T) {

func TestHealthCheckMultipleShutdowns(t *testing.T) {
config := Config{
Port: testutil.GetAvailablePort(t),
TCPAddr: confignet.TCPAddr{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}

hcExt := newServer(config, zap.NewNop())
Expand All @@ -121,7 +124,9 @@ func TestHealthCheckMultipleShutdowns(t *testing.T) {

func TestHealthCheckShutdownWithoutStart(t *testing.T) {
config := Config{
Port: testutil.GetAvailablePort(t),
TCPAddr: confignet.TCPAddr{
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}

hcExt := newServer(config, zap.NewNop())
Expand Down
2 changes: 1 addition & 1 deletion extension/healthcheckextension/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extensions:
health_check:
health_check/1:
port: 13
endpoint: "localhost:13"

service:
extensions: [health_check/1]
Expand Down
8 changes: 4 additions & 4 deletions service/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func TestApplication_Start(t *testing.T) {

const testPrefix = "a_test"
metricsPort := testutil.GetAvailablePort(t)
healthCheckPortStr := strconv.FormatUint(uint64(testutil.GetAvailablePort(t)), 10)
healthCheckEndpoint := testutil.GetAvailableLocalAddress(t)
app.rootCmd.SetArgs([]string{
"--config=testdata/otelcol-config.yaml",
"--metrics-addr=localhost:" + strconv.FormatUint(uint64(metricsPort), 10),
"--metrics-prefix=" + testPrefix,
"--set=extensions.health_check.port=" + healthCheckPortStr,
"--set=extensions.health_check.endpoint=" + healthCheckEndpoint,
})

appDone := make(chan struct{})
Expand All @@ -80,7 +80,7 @@ func TestApplication_Start(t *testing.T) {

assert.Equal(t, Starting, <-app.GetStateChannel())
assert.Equal(t, Running, <-app.GetStateChannel())
require.True(t, isAppAvailable(t, "http://localhost:"+healthCheckPortStr))
require.True(t, isAppAvailable(t, "http://"+healthCheckEndpoint))
assert.Equal(t, app.logger, app.GetLogger())
assert.True(t, loggingHookCalled)

Expand Down Expand Up @@ -275,7 +275,7 @@ func TestSetFlag(t *testing.T) {
"--set=processors.attributes.actions.key=foo",
"--set=processors.attributes.actions.value=bar",
"--set=receivers.jaeger.protocols.grpc.endpoint=localhost:12345",
"--set=extensions.health_check.port=8080",
"--set=extensions.health_check.endpoint=localhost:8080",
})
require.NoError(t, err)
cfg, err := FileLoaderConfigFactory(configload.NewViper(), app.rootCmd, factories)
Expand Down
3 changes: 1 addition & 2 deletions service/defaultcomponents/default_extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func TestDefaultExtensions(t *testing.T) {

extFactories := allFactories.Extensions
endpoint := testutil.GetAvailableLocalAddress(t)
port := testutil.GetAvailablePort(t)

tests := []struct {
extension configmodels.Type
Expand All @@ -47,7 +46,7 @@ func TestDefaultExtensions(t *testing.T) {
extension: "health_check",
getConfigFn: func() configmodels.Extension {
cfg := extFactories["health_check"].CreateDefaultConfig().(*healthcheckextension.Config)
cfg.Port = port
cfg.TCPAddr.Endpoint = endpoint
return cfg
},
},
Expand Down