Skip to content

Commit

Permalink
Improve cluster detection for components during translation (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
musa-asad committed Feb 5, 2025
1 parent 77aa32a commit 5e1b3aa
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 52 deletions.
14 changes: 13 additions & 1 deletion translator/translate/otel/common/appsignals.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@

package common

import "os"
import (
"os"

"go.opentelemetry.io/collector/confmap"
)

const KubernetesEnvVar = "K8S_NAMESPACE"

func IsAppSignalsKubernetes() bool {
_, isSet := os.LookupEnv(KubernetesEnvVar)
return isSet
}

func GetHostedIn(conf *confmap.Conf) (string, bool) {
hostedIn, hostedInConfigured := GetString(conf, ConfigKey(LogsKey, MetricsCollectedKey, AppSignals, "hosted_in"))
if !hostedInConfigured {
hostedIn, hostedInConfigured = GetString(conf, ConfigKey(LogsKey, MetricsCollectedKey, AppSignalsFallback, "hosted_in"))
}
return hostedIn, hostedInConfigured
}
17 changes: 17 additions & 0 deletions translator/translate/otel/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package common
import (
"container/list"
"fmt"
"os"
"reflect"
"strconv"
"strings"
Expand All @@ -14,6 +15,8 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"gopkg.in/yaml.v3"

"github.com/aws/amazon-cloudwatch-agent/translator/translate/logs/util"
)

const (
Expand Down Expand Up @@ -454,3 +457,17 @@ func IsAnySet(conf *confmap.Conf, keys []string) bool {
func KueueContainerInsightsEnabled(conf *confmap.Conf) bool {
return GetOrDefaultBool(conf, ConfigKey(LogsKey, MetricsCollectedKey, KubernetesKey, EnableKueueContainerInsights), false)
}

func GetClusterName(conf *confmap.Conf) string {
val, ok := GetString(conf, ConfigKey(LogsKey, MetricsCollectedKey, KubernetesKey, "cluster_name"))
if ok && val != "" {
return val
}

envVarClusterName := os.Getenv("K8S_CLUSTER_NAME")
if envVarClusterName != "" {
return envVarClusterName
}

return util.GetClusterNameFromEc2Tagger()
}
3 changes: 1 addition & 2 deletions translator/translate/otel/exporter/awsemf/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"go.opentelemetry.io/collector/confmap"

"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/logs/util"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
"github.com/aws/amazon-cloudwatch-agent/translator/util/ecsutil"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ func setPrometheusLogGroup(conf *confmap.Conf, cfg *awsemfexporter.Config) error
}
} else {

if clusterName := util.GetClusterNameFromEc2Tagger(); clusterName != "" {
if clusterName := common.GetClusterName(conf); clusterName != "" {
cfg.LogGroupName = fmt.Sprintf(eksDefaultLogGroupFormat, clusterName)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/rules"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/logs/util"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
"github.com/aws/amazon-cloudwatch-agent/translator/util/ecsutil"
)
Expand Down Expand Up @@ -64,15 +63,10 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
configKey := common.AppSignalsConfigKeys[t.dataType]
cfg := t.factory.CreateDefaultConfig().(*appsignalsconfig.Config)

hostedInConfigKey := common.ConfigKey(common.LogsKey, common.MetricsCollectedKey, common.AppSignals, "hosted_in")
hostedIn, hostedInConfigured := common.GetString(conf, hostedInConfigKey)
if !hostedInConfigured {
hostedInConfigKey = common.ConfigKey(common.LogsKey, common.MetricsCollectedKey, common.AppSignalsFallback, "hosted_in")
hostedIn, hostedInConfigured = common.GetString(conf, hostedInConfigKey)
}
hostedIn, hostedInConfigured := common.GetHostedIn(conf)
if common.IsAppSignalsKubernetes() {
if !hostedInConfigured {
hostedIn = util.GetClusterNameFromEc2Tagger()
hostedIn = common.GetClusterName(conf)
}
}

Expand Down
33 changes: 12 additions & 21 deletions translator/translate/otel/processor/awsentity/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsentity"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/logs/util"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
"github.com/aws/amazon-cloudwatch-agent/translator/util/ecsutil"
)
Expand Down Expand Up @@ -55,8 +54,10 @@ func (t *translator) ID() component.ID {
}

func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
ctx := context.CurrentContext()

// Do not send entity for ECS
if context.CurrentContext().RunInContainer() && ecsutil.GetECSUtilSingleton().IsECS() {
if ctx.RunInContainer() && ecsutil.GetECSUtilSingleton().IsECS() {
return nil, nil
}

Expand All @@ -70,32 +71,22 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
cfg.ScrapeDatapointAttribute = true
}

hostedInConfigKey := common.ConfigKey(common.LogsKey, common.MetricsCollectedKey, common.AppSignals, "hosted_in")
hostedIn, hostedInConfigured := common.GetString(conf, hostedInConfigKey)
if !hostedInConfigured {
hostedInConfigKey = common.ConfigKey(common.LogsKey, common.MetricsCollectedKey, common.AppSignalsFallback, "hosted_in")
hostedIn, hostedInConfigured = common.GetString(conf, hostedInConfigKey)
}
if common.IsAppSignalsKubernetes() {
if !hostedInConfigured {
hostedIn = util.GetClusterNameFromEc2Tagger()
}
}

//TODO: This logic is more or less identical to what AppSignals does. This should be moved to a common place for reuse
ctx := context.CurrentContext()
mode := ctx.KubernetesMode()
cfg.KubernetesMode = mode
cfg.KubernetesMode = ctx.KubernetesMode()

mode = ctx.Mode()
if cfg.KubernetesMode != "" {
cfg.ClusterName = hostedIn
clusterName, clusterNameConfigured := common.GetHostedIn(conf)

if !clusterNameConfigured {
clusterName = common.GetClusterName(conf)
}

cfg.ClusterName = clusterName
}

// We want to keep platform config variable to be
// anything that is non-Kubernetes related so the
// processor can perform different logics for EKS
// in EC2 or Non-EC2
cfg.Platform = mode
cfg.Platform = ctx.Mode()
return cfg, nil
}
74 changes: 74 additions & 0 deletions translator/translate/otel/processor/awsentity/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestTranslate(t *testing.T) {
input map[string]interface{}
mode string
kubernetesMode string
envClusterName string
want *awsentity.Config
}{
"OnlyProfile": {
Expand All @@ -39,6 +40,74 @@ func TestTranslate(t *testing.T) {
Platform: config.ModeEC2,
},
},
"KubernetesUnderLogs": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"kubernetes": map[string]interface{}{
"cluster_name": "ci-logs",
},
},
},
},
mode: config.ModeEC2,
kubernetesMode: config.ModeEKS,
want: &awsentity.Config{
ClusterName: "ci-logs",
KubernetesMode: config.ModeEKS,
Platform: config.ModeEC2,
},
},
"EnvVar": {
input: map[string]interface{}{},
mode: config.ModeEC2,
kubernetesMode: config.ModeEKS,
envClusterName: "env-cluster",
want: &awsentity.Config{
ClusterName: "env-cluster",
KubernetesMode: config.ModeEKS,
Platform: config.ModeEC2,
},
},
"AppSignalsPrecedence": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"app_signals": map[string]interface{}{
"hosted_in": "test",
},
"kubernetes": map[string]interface{}{
"cluster_name": "ci-logs",
},
},
}},
mode: config.ModeEC2,
kubernetesMode: config.ModeEKS,
want: &awsentity.Config{
ClusterName: "test",
KubernetesMode: config.ModeEKS,
Platform: config.ModeEC2,
},
},
"KubernetesPrecedence": {
input: map[string]interface{}{
"logs": map[string]interface{}{
"metrics_collected": map[string]interface{}{
"kubernetes": map[string]interface{}{
"cluster_name": "ci-logs",
},
},
},
},
mode: config.ModeEC2,
kubernetesMode: config.ModeEKS,
envClusterName: "env-cluster",
want: &awsentity.Config{
ClusterName: "ci-logs",
KubernetesMode: config.ModeEKS,
Platform: config.ModeEC2,
},
},
"ECS": {
input: map[string]interface{}{},
mode: config.ModeECS,
Expand All @@ -56,6 +125,11 @@ func TestTranslate(t *testing.T) {
context.CurrentContext().SetMode(testCase.mode)
context.CurrentContext().SetKubernetesMode(testCase.kubernetesMode)
}
if testCase.envClusterName != "" {
t.Setenv("K8S_CLUSTER_NAME", testCase.envClusterName)
} else {
t.Setenv("K8S_CLUSTER_NAME", "")
}
tt := NewTranslator()
assert.Equal(t, "awsentity", tt.ID().String())
conf := confmap.NewFromStringMap(testCase.input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/logs/util"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
)

Expand Down Expand Up @@ -113,11 +112,7 @@ func (t *translator) getJMXAttributes(conf *confmap.Conf) []any {
}

func (t *translator) getContainerInsightsJMXAttributes(conf *confmap.Conf) []any {
clusterName, ok := common.GetString(conf, common.ConfigKey(k8sKey, "cluster_name"))

if !ok {
clusterName = util.GetClusterNameFromEc2Tagger()
}
clusterName := common.GetClusterName(conf)
nodeName := os.Getenv(config.HOST_NAME)
return []any{
map[string]any{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/agent"
globallogs "github.com/aws/amazon-cloudwatch-agent/translator/translate/logs"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/logs/util"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/extension/agenthealth"
logsutil "github.com/aws/amazon-cloudwatch-agent/translator/translate/util"
Expand Down Expand Up @@ -128,12 +127,7 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
}

func (t *translator) setClusterName(conf *confmap.Conf, cfg *awscontainerinsightreceiver.Config) error {
clusterNameKey := common.ConfigKey(common.LogsKey, common.MetricsCollectedKey, common.KubernetesKey, "cluster_name")
if clusterName, ok := common.GetString(conf, clusterNameKey); ok {
cfg.ClusterName = clusterName
} else {
cfg.ClusterName = util.GetClusterNameFromEc2Tagger()
}
cfg.ClusterName = common.GetClusterName(conf)

if cfg.ClusterName == "" {
return errors.New("cluster name is not provided and was not auto-detected from EC2 tags")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/receiver"

"github.com/aws/amazon-cloudwatch-agent/translator/translate/logs/util"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
)

Expand Down Expand Up @@ -60,12 +59,7 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
}

func (t *translator) setClusterName(conf *confmap.Conf, cfg *awscontainerinsightskueuereceiver.Config) error {
clusterNameKey := common.ConfigKey(common.LogsKey, common.MetricsCollectedKey, common.KubernetesKey, "cluster_name")
if clusterName, ok := common.GetString(conf, clusterNameKey); ok {
cfg.ClusterName = clusterName
} else {
cfg.ClusterName = util.GetClusterNameFromEc2Tagger()
}
cfg.ClusterName = common.GetClusterName(conf)

if cfg.ClusterName == "" {
return errors.New("cluster name is not provided and was not auto-detected from EC2 tags")
Expand Down

0 comments on commit 5e1b3aa

Please sign in to comment.