diff --git a/translator/translate/logs/util/get_eks_cluster_name.go b/translator/translate/logs/util/get_eks_cluster_name.go index f5aa8539af..1febfd6078 100644 --- a/translator/translate/logs/util/get_eks_cluster_name.go +++ b/translator/translate/logs/util/get_eks_cluster_name.go @@ -5,6 +5,7 @@ package util import ( "log" + "os" "strings" "time" @@ -34,6 +35,12 @@ func GetEKSClusterName(sectionKey string, input map[string]interface{}) string { //The key is in current input instance, use the value in JSON. clusterName = val.(string) } + if clusterName == "" { + envVarClusterName := os.Getenv("K8S_CLUSTER_NAME") + if envVarClusterName != "" { + clusterName = envVarClusterName + } + } if clusterName == "" { clusterName = GetClusterNameFromEc2Tagger() } diff --git a/translator/translate/otel/common/common.go b/translator/translate/otel/common/common.go index ea8e00f309..e240825da3 100644 --- a/translator/translate/otel/common/common.go +++ b/translator/translate/otel/common/common.go @@ -486,9 +486,14 @@ func KueueContainerInsightsEnabled(conf *confmap.Conf) bool { } func GetClusterName(conf *confmap.Conf) string { - val, ok := GetString(conf, ConfigKey(LogsKey, MetricsCollectedKey, KubernetesKey, "cluster_name")) - if ok && val != "" { - return val + val1, ok1 := GetString(conf, ConfigKey(LogsKey, MetricsCollectedKey, KubernetesKey, "cluster_name")) + if ok1 && val1 != "" { + return val1 + } + + val2, ok2 := GetString(conf, ConfigKey(LogsKey, MetricsCollectedKey, PrometheusKey, "cluster_name")) + if ok2 && val2 != "" { + return val2 } envVarClusterName := os.Getenv("K8S_CLUSTER_NAME") diff --git a/translator/translate/otel/processor/awsentity/translator_test.go b/translator/translate/otel/processor/awsentity/translator_test.go index 833bde2285..e88fe2004a 100644 --- a/translator/translate/otel/processor/awsentity/translator_test.go +++ b/translator/translate/otel/processor/awsentity/translator_test.go @@ -82,6 +82,9 @@ func TestTranslate(t *testing.T) { "kubernetes": map[string]interface{}{ "cluster_name": "ci-logs", }, + "prometheus": map[string]interface{}{ + "cluster_name": "ci-prometheus", + }, }, }}, mode: config.ModeEC2, @@ -99,6 +102,9 @@ func TestTranslate(t *testing.T) { "kubernetes": map[string]interface{}{ "cluster_name": "ci-logs", }, + "prometheus": map[string]interface{}{ + "cluster_name": "ci-prometheus", + }, }, }, }, @@ -111,6 +117,43 @@ func TestTranslate(t *testing.T) { Platform: config.ModeEC2, }, }, + "PrometheusUnderLogs": { + input: map[string]interface{}{ + "logs": map[string]interface{}{ + "metrics_collected": map[string]interface{}{ + "prometheus": map[string]interface{}{ + "cluster_name": "ci-prometheus", + }, + }, + }, + }, + mode: config.ModeEC2, + kubernetesMode: config.ModeEKS, + want: &awsentity.Config{ + ClusterName: "ci-prometheus", + KubernetesMode: config.ModeEKS, + Platform: config.ModeEC2, + }, + }, + "PrometheusPrecedence": { + input: map[string]interface{}{ + "logs": map[string]interface{}{ + "metrics_collected": map[string]interface{}{ + "prometheus": map[string]interface{}{ + "cluster_name": "ci-prometheus", + }, + }, + }, + }, + mode: config.ModeEC2, + kubernetesMode: config.ModeEKS, + envClusterName: "env-cluster", + want: &awsentity.Config{ + ClusterName: "ci-prometheus", + KubernetesMode: config.ModeEKS, + Platform: config.ModeEC2, + }, + }, "ECS": { input: map[string]interface{}{}, mode: config.ModeECS,