Skip to content

Commit f42dcc3

Browse files
committed
fix cluster name resolved path
1 parent 44e3985 commit f42dcc3

14 files changed

Lines changed: 45 additions & 33 deletions

File tree

translator/cmdutil/translatorutil_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,6 @@ func TestOpenTelemetryWindowsEventsInvalidSchemaValidation(t *testing.T) {
451451
})
452452
}
453453

454-
func TestOpenTelemetryInvalidClusterNameSchemaValidation(t *testing.T) {
455-
checkIfSchemaValidateAsExpected(t, "../../translator/config/sampleSchema/opentelemetry/invalidClusterName.json", false, map[string]int{
456-
"pattern": 1,
457-
})
458-
}
459-
460454
func TestCombinedV1V2SchemaValidation(t *testing.T) {
461455
checkIfSchemaValidateAsExpected(t, "../../translator/config/sampleSchema/opentelemetry/validCombinedV1V2Config.json", true, map[string]int{})
462456
}

translator/config/sampleSchema/opentelemetry/invalidClusterName.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

translator/config/schema.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,8 +1808,7 @@
18081808
"properties": {
18091809
"cluster_name": {
18101810
"description": "The name of the K8s cluster. Applied to all telemetry collected under opentelemetry.",
1811-
"type": "string",
1812-
"pattern": "^[0-9A-Za-z][A-Za-z0-9\\-_]*$"
1811+
"type": "string"
18131812
},
18141813
"collect": {
18151814
"type": "object",

translator/translate/otel/common/common.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"reflect"
11+
"regexp"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -556,9 +557,9 @@ func SanitizeName(input string) string {
556557
}, strings.ToLower(input))
557558
}
558559

559-
func GetClusterName(conf *confmap.Conf) string {
560-
val, ok := GetString(conf, ConfigKey(LogsKey, MetricsCollectedKey, KubernetesKey, "cluster_name"))
561-
if ok && val != "" {
560+
func GetClusterName(conf *confmap.Conf, key string) string {
561+
// Check any config keys passed
562+
if val, ok := GetString(conf, key); ok && val != "" {
562563
return val
563564
}
564565

@@ -598,3 +599,17 @@ func EscapeDollarDigit(s string) string {
598599

599600
// OtelClusterNameKey is the config key for the root-level cluster name under opentelemetry.
600601
var OtelClusterNameKey = ConfigKey(OpenTelemetryKey, ClusterNameKey)
602+
603+
// LegacyClusterNameKey is the config key for the cluster name in the V1 config path.
604+
var LegacyClusterNameKey = ConfigKey(LogsKey, MetricsCollectedKey, KubernetesKey, ClusterNameKey)
605+
606+
// ClusterNameRegex validates cluster names.
607+
var ClusterNameRegex = regexp.MustCompile(`^[0-9A-Za-z][A-Za-z0-9\-_]*$`)
608+
609+
// ValidateClusterName returns an error if the cluster name does not match the expected pattern.
610+
func ValidateClusterName(name string) error {
611+
if !ClusterNameRegex.MatchString(name) {
612+
return fmt.Errorf("cluster_name %q is invalid: must match pattern %s", name, ClusterNameRegex.String())
613+
}
614+
return nil
615+
}

translator/translate/otel/exporter/awsemf/prometheus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func setPrometheusLogGroup(conf *confmap.Conf, cfg *awsemfexporter.Config) error
3939
}
4040
} else {
4141

42-
if clusterName := common.GetClusterName(conf); clusterName != "" {
42+
if clusterName := common.GetClusterName(conf, common.LegacyClusterNameKey); clusterName != "" {
4343
cfg.LogGroupName = fmt.Sprintf(eksDefaultLogGroupFormat, clusterName)
4444
}
4545
}

translator/translate/otel/pipeline/opentelemetry/containerinsights/common.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ func (t *yamlComponentTranslator) Translate(_ *confmap.Conf) (component.Config,
7575
}
7676

7777
func getClusterName(conf *confmap.Conf) (string, error) {
78-
name, _ := common.GetString(conf, common.OtelClusterNameKey)
78+
name := common.GetClusterName(conf, common.OtelClusterNameKey)
7979
if name == "" {
80-
return "", fmt.Errorf("cluster_name is required for container_insights: set opentelemetry::cluster_name in config")
80+
return "", fmt.Errorf("cluster_name is required for container_insights: set opentelemetry::cluster_name in config or K8S_CLUSTER_NAME environment variable")
81+
}
82+
if err := common.ValidateClusterName(name); err != nil {
83+
return "", err
8184
}
8285
return name, nil
8386
}

translator/translate/otel/pipeline/opentelemetry/translator_logs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ func (t *baseLogsTranslator) Translate(conf *confmap.Conf) (*common.ComponentTra
113113
processors.Set(k8sattributesprocessor.NewTranslator(common.OpenTelemetryKey))
114114
}
115115
// Apply root-level cluster name if set
116-
if clusterName, ok := common.GetString(conf, common.OtelClusterNameKey); ok && clusterName != "" {
116+
clusterName := common.GetClusterName(conf, common.OtelClusterNameKey)
117+
if clusterName != "" {
118+
if err := common.ValidateClusterName(clusterName); err != nil {
119+
return nil, err
120+
}
117121
stmt := fmt.Sprintf(`set(resource.attributes["k8s.cluster.name"], "%s")`, clusterName)
118122
processors.Set(transformprocessor.NewTranslatorWithName("set_cluster_name",
119123
transformprocessor.WithMetricResourceStatements([]string{stmt}),

translator/translate/otel/pipeline/opentelemetry/translator_metrics.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ func (t *baseMetricsTranslator) Translate(conf *confmap.Conf) (*common.Component
6464
processors.Set(k8sattributesprocessor.NewTranslator(common.OpenTelemetryKey))
6565
}
6666
// Apply root-level cluster name if set
67-
if clusterName, ok := common.GetString(conf, common.OtelClusterNameKey); ok && clusterName != "" {
67+
clusterName := common.GetClusterName(conf, common.OtelClusterNameKey)
68+
if clusterName != "" {
69+
if err := common.ValidateClusterName(clusterName); err != nil {
70+
return nil, err
71+
}
6872
stmt := fmt.Sprintf(`set(resource.attributes["k8s.cluster.name"], "%s")`, clusterName)
6973
processors.Set(transformprocessor.NewTranslatorWithName("set_cluster_name",
7074
transformprocessor.WithMetricResourceStatements([]string{stmt}),

translator/translate/otel/pipeline/opentelemetry/translator_traces.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ func (t *baseTracesTranslator) Translate(conf *confmap.Conf) (*common.ComponentT
5959
processors.Set(k8sattributesprocessor.NewTranslator(common.OpenTelemetryKey))
6060
}
6161
// Apply root-level cluster name if set
62-
if clusterName, ok := common.GetString(conf, common.OtelClusterNameKey); ok && clusterName != "" {
62+
clusterName := common.GetClusterName(conf, common.OtelClusterNameKey)
63+
if clusterName != "" {
64+
if err := common.ValidateClusterName(clusterName); err != nil {
65+
return nil, err
66+
}
6367
stmt := fmt.Sprintf(`set(resource.attributes["k8s.cluster.name"], "%s")`, clusterName)
6468
processors.Set(transformprocessor.NewTranslatorWithName("set_cluster_name",
6569
transformprocessor.WithMetricResourceStatements([]string{stmt}),

translator/translate/otel/processor/awsapplicationsignals/translator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
6767
hostedIn, hostedInConfigured := common.GetHostedIn(conf)
6868
if common.IsAppSignalsKubernetes() {
6969
if !hostedInConfigured {
70-
hostedIn = common.GetClusterName(conf)
70+
hostedIn = common.GetClusterName(conf, common.LegacyClusterNameKey)
7171
}
7272
}
7373

0 commit comments

Comments
 (0)