Skip to content

Commit

Permalink
Check the usage_data in the config. (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Oct 30, 2023
1 parent dcbdde0 commit e1bdbd3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
OperationPutMetricData = "PutMetricData"
OperationPutLogEvents = "PutLogEvents"
OperationPutTraceSegments = "PutTraceSegments"

usageDataKey = "usage_data"
)

var (
Expand Down Expand Up @@ -49,9 +51,12 @@ func (t *translator) ID() component.ID {
}

// Translate creates an extension configuration.
func (t *translator) Translate(*confmap.Conf) (component.Config, error) {
func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
cfg := t.factory.CreateDefaultConfig().(*agenthealth.Config)
cfg.IsUsageDataEnabled = t.isUsageDataEnabled
if usageData, ok := common.GetBool(conf, common.ConfigKey(common.AgentKey, usageDataKey)); ok {
cfg.IsUsageDataEnabled = cfg.IsUsageDataEnabled && usageData
}
cfg.Stats = agent.StatsConfig{Operations: t.operations}
return cfg, nil
}
68 changes: 58 additions & 10 deletions translator/translate/otel/extension/agenthealth/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,69 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/confmap"

"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth"
"github.com/aws/amazon-cloudwatch-agent/extension/agenthealth/handler/stats/agent"
)

func TestTranslate(t *testing.T) {
operations := []string{OperationPutLogEvents}
tt := NewTranslator("test", operations).(*translator)
assert.Equal(t, "agenthealth/test", tt.ID().String())
tt.isUsageDataEnabled = true
got, err := tt.Translate(nil)
assert.NoError(t, err)
assert.Equal(t, &agenthealth.Config{IsUsageDataEnabled: true, Stats: agent.StatsConfig{Operations: operations}}, got)
tt.isUsageDataEnabled = false
got, err = tt.Translate(nil)
assert.NoError(t, err)
assert.Equal(t, &agenthealth.Config{IsUsageDataEnabled: false, Stats: agent.StatsConfig{Operations: operations}}, got)
testCases := map[string]struct {
input map[string]interface{}
isEnvUsageData bool
want *agenthealth.Config
}{
"WithUsageData/NotInConfig": {
input: map[string]interface{}{"agent": map[string]interface{}{}},
isEnvUsageData: true,
want: &agenthealth.Config{
IsUsageDataEnabled: true,
Stats: agent.StatsConfig{
Operations: operations,
},
},
},
"WithUsageData/FalseInConfig": {
input: map[string]interface{}{"agent": map[string]interface{}{"usage_data": false}},
isEnvUsageData: true,
want: &agenthealth.Config{
IsUsageDataEnabled: false,
Stats: agent.StatsConfig{
Operations: operations,
},
},
},
"WithUsageData/FalseInEnv": {
input: map[string]interface{}{"agent": map[string]interface{}{"usage_data": true}},
isEnvUsageData: false,
want: &agenthealth.Config{
IsUsageDataEnabled: false,
Stats: agent.StatsConfig{
Operations: operations,
},
},
},
"WithUsageData/BothTrue": {
input: map[string]interface{}{"agent": map[string]interface{}{"usage_data": true}},
isEnvUsageData: true,
want: &agenthealth.Config{
IsUsageDataEnabled: true,
Stats: agent.StatsConfig{
Operations: operations,
},
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
tt := NewTranslator("test", operations).(*translator)
assert.Equal(t, "agenthealth/test", tt.ID().String())
tt.isUsageDataEnabled = testCase.isEnvUsageData
conf := confmap.NewFromStringMap(testCase.input)
got, err := tt.Translate(conf)
assert.NoError(t, err)
assert.Equal(t, testCase.want, got)
})
}
}

0 comments on commit e1bdbd3

Please sign in to comment.