Skip to content

Commit 36c8164

Browse files
committed
Add health check extension for liveness & readiness probes
1 parent 0ed03d7 commit 36c8164

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package extension
5+
6+
import (
7+
"sync"
8+
9+
"go.opentelemetry.io/collector/component"
10+
"go.opentelemetry.io/collector/confmap"
11+
12+
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
13+
)
14+
15+
type healthCheckTranslator struct {
16+
name string
17+
mux sync.RWMutex
18+
}
19+
20+
var _ common.Translator[component.Config, component.ID] = (*healthCheckTranslator)(nil)
21+
22+
func NewHealthCheckTranslator() common.Translator[component.Config, component.ID] {
23+
return &healthCheckTranslator{name: "health_check"}
24+
}
25+
26+
func (t *healthCheckTranslator) ID() component.ID {
27+
t.mux.RLock()
28+
defer t.mux.RUnlock()
29+
return component.NewIDWithName(component.MustNewType("health_check"), t.name)
30+
}
31+
32+
func (t *healthCheckTranslator) Translate(_ *confmap.Conf) (component.Config, error) {
33+
cfg := &struct {
34+
Endpoint string `mapstructure:"endpoint"`
35+
Path string `mapstructure:"path"`
36+
}{
37+
Endpoint: "0.0.0.0:13133",
38+
Path: "/",
39+
}
40+
41+
return cfg, nil
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package extension
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"go.opentelemetry.io/collector/confmap"
11+
)
12+
13+
func TestHealthCheckTranslator(t *testing.T) {
14+
translator := NewHealthCheckTranslator()
15+
assert.Equal(t, "health_check", translator.ID().Type().String())
16+
17+
conf := confmap.New()
18+
cfg, err := translator.Translate(conf)
19+
assert.NoError(t, err)
20+
21+
// Assert the config has the expected fields
22+
healthCheckCfg, ok := cfg.(*struct {
23+
Endpoint string `mapstructure:"endpoint"`
24+
Path string `mapstructure:"path"`
25+
})
26+
assert.True(t, ok)
27+
assert.Equal(t, "0.0.0.0:13133", healthCheckCfg.Endpoint)
28+
assert.Equal(t, "/", healthCheckCfg.Path)
29+
}

translator/translate/otel/translate_otel.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/aws/amazon-cloudwatch-agent/translator/context"
2424
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
25+
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/extension"
2526
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/extension/entitystore"
2627
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/extension/server"
2728
pipelinetranslator "github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/pipeline"
@@ -94,6 +95,8 @@ func Translate(jsonConfig interface{}, os string) (*otelcol.Config, error) {
9495
pipelines.Translators.Extensions.Set(server.NewTranslator())
9596
}
9697

98+
pipelines.Translators.Extensions.Set(extension.NewHealthCheckTranslator())
99+
97100
cfg := &otelcol.Config{
98101
Receivers: map[component.ID]component.Config{},
99102
Exporters: map[component.ID]component.Config{},

translator/translate/otel/translate_otel_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ import (
2020
"github.com/aws/amazon-cloudwatch-agent/translator/util/eksdetector"
2121
)
2222

23+
func TestHealthCheckExtension(t *testing.T) {
24+
agent.Global_Config.Region = "us-east-1"
25+
input := map[string]interface{}{
26+
"metrics": map[string]interface{}{
27+
"metrics_collected": map[string]interface{}{
28+
"cpu": map[string]interface{}{},
29+
},
30+
},
31+
}
32+
33+
cfg, err := Translate(input, "linux")
34+
require.NoError(t, err)
35+
require.NotNil(t, cfg)
36+
37+
// Verify that the health check extension is registered
38+
extensionFound := false
39+
for _, ext := range cfg.Service.Extensions {
40+
if ext.Type().String() == "health_check" {
41+
extensionFound = true
42+
break
43+
}
44+
}
45+
assert.True(t, extensionFound, "Health check extension should be registered")
46+
}
47+
2348
func TestTranslator(t *testing.T) {
2449
agent.Global_Config.Region = "us-east-1"
2550
testutil.SetPrometheusRemoteWriteTestingEnv(t)

0 commit comments

Comments
 (0)