diff --git a/extension/healthcheckextension/README.md b/extension/healthcheckextension/README.md index 299028a36620..1880998a165f 100644 --- a/extension/healthcheckextension/README.md +++ b/extension/healthcheckextension/README.md @@ -30,7 +30,8 @@ The following settings are required: - `endpoint` (default = localhost:13133): Address to publish the health check status. For full list of `ServerConfig` refer [here](https://github.com/open-telemetry/opentelemetry-collector/tree/main/config/confighttp). You can temporarily disable the `component.UseLocalHostAsDefaultHost` feature gate to change this to 0.0.0.0:13133. This feature gate will be removed in a future release. - `path` (default = "/"): Specifies the path to be configured for the health check server. -- `response_body` (default = ""): Specifies a static body that overrides the default response returned by the health check service. +- `response_body` (default = ""): Specifies a static body that overrides the default response returned by the health check service. +- `shutdown_delay_duration` (default = 0): Specifies a sleep interval for setting NotReady status. This is useful in k8s env to delay shutdown, allowing pod readiness to detect NotReady state and move traffic. Example: @@ -44,6 +45,7 @@ extensions: cert_file: "/path/to/cert.crt" key_file: "/path/to/key.key" path: "/health/status" + shutdown_delay_duration: 2s ``` The full list of settings exposed for this exporter is documented [here](./config.go) diff --git a/extension/healthcheckextension/config.go b/extension/healthcheckextension/config.go index cd9526d7f21f..ce307635c289 100644 --- a/extension/healthcheckextension/config.go +++ b/extension/healthcheckextension/config.go @@ -37,6 +37,10 @@ type Config struct { // CheckCollectorPipeline contains the list of settings of collector pipeline health check CheckCollectorPipeline checkCollectorPipelineSettings `mapstructure:"check_collector_pipeline"` + + // ShutdownDelayDuration represents the sleep interval for setting NotReady status + // This is useful in k8s env to delay shutdown, allowing pod readiness to detect NotReady state and move traffic + ShutdownDelayDuration time.Duration `mapstructure:"shutdown_delay_duration"` } var _ component.Config = (*Config)(nil) diff --git a/extension/healthcheckextension/config_test.go b/extension/healthcheckextension/config_test.go index ba4fb69d0fb2..bdcb874bbe7b 100644 --- a/extension/healthcheckextension/config_test.go +++ b/extension/healthcheckextension/config_test.go @@ -6,6 +6,7 @@ package healthcheckextension import ( "path/filepath" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -59,6 +60,16 @@ func TestLoadConfig(t *testing.T) { id: component.NewIDWithName(metadata.Type, "invalidpath"), expectedErr: errInvalidPath, }, + { + id: component.NewIDWithName(metadata.Type, "shutdowndelay5s"), + expected: &Config{ + ServerConfig: confighttp.NewDefaultServerConfig(), + Path: "/", + ResponseBody: nil, + CheckCollectorPipeline: defaultCheckCollectorPipelineSettings(), + ShutdownDelayDuration: 5 * time.Second, + }, + }, } for _, tt := range tests { t.Run(tt.id.String(), func(t *testing.T) { diff --git a/extension/healthcheckextension/factory.go b/extension/healthcheckextension/factory.go index aca68b892e60..504f3264ce5d 100644 --- a/extension/healthcheckextension/factory.go +++ b/extension/healthcheckextension/factory.go @@ -33,6 +33,7 @@ func createDefaultConfig() component.Config { }, CheckCollectorPipeline: defaultCheckCollectorPipelineSettings(), Path: "/", + ShutdownDelayDuration: 0, } } diff --git a/extension/healthcheckextension/factory_test.go b/extension/healthcheckextension/factory_test.go index 2397dda4d3d6..cd6ba2d7b64b 100644 --- a/extension/healthcheckextension/factory_test.go +++ b/extension/healthcheckextension/factory_test.go @@ -24,6 +24,7 @@ func TestFactory_CreateDefaultConfig(t *testing.T) { }, CheckCollectorPipeline: defaultCheckCollectorPipelineSettings(), Path: "/", + ShutdownDelayDuration: 0, }, cfg) assert.NoError(t, componenttest.CheckConfigStruct(cfg)) diff --git a/extension/healthcheckextension/healthcheckextension.go b/extension/healthcheckextension/healthcheckextension.go index b2eb00769d53..b247f0b49fbe 100644 --- a/extension/healthcheckextension/healthcheckextension.go +++ b/extension/healthcheckextension/healthcheckextension.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "net/http" + "time" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/extension" @@ -91,6 +92,11 @@ func (hc *healthCheckExtension) Ready() error { func (hc *healthCheckExtension) NotReady() error { hc.state.Set(healthcheck.Unavailable) + + if hc.config.ShutdownDelayDuration > 0 { + time.Sleep(hc.config.ShutdownDelayDuration) + } + return nil } diff --git a/extension/healthcheckextension/testdata/config.yaml b/extension/healthcheckextension/testdata/config.yaml index b9ff04c3e8df..955052ac1843 100644 --- a/extension/healthcheckextension/testdata/config.yaml +++ b/extension/healthcheckextension/testdata/config.yaml @@ -28,3 +28,5 @@ health_check/invalidpath: enabled: false interval: "5m" exporter_failure_threshold: 5 +health_check/shutdowndelay5s: + shutdown_delay_duration: 5s