Skip to content

Commit

Permalink
Add logic to get cluster name from attributes populated by resourcede…
Browse files Browse the repository at this point in the history
…tectionprocessor
  • Loading branch information
jj22ee committed Nov 8, 2024
1 parent 113eee2 commit 8a94183
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion processor/awsapplicationsignalsprocessor/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (cfg *Config) Validate() error {
switch resolver.Platform {
case PlatformEKS:
if resolver.Name == "" {
return errors.New("name must not be empty for eks resolver")
resolver.Name = "UNKNOWN"
}
case PlatformK8s:
if resolver.Name == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ func TestValidateFailedOnEmptyResolverName(t *testing.T) {
name string
resolver Resolver
}{
{
"testEKS",
NewEKSResolver(""),
},
{
"testK8S",
NewK8sResolver(""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const (
AWSECSTaskID = "aws.ecs.task.id"

// resource detection processor attributes
ResourceDetectionHostID = "host.id"
ResourceDetectionHostName = "host.name"
ResourceDetectionASG = "ec2.tag.aws:autoscaling:groupName"
ResourceDetectionHostID = "host.id"
ResourceDetectionHostName = "host.name"
ResourceDetectionASG = "ec2.tag.aws:autoscaling:groupName"
ResourceDetectionClusterName = "k8s.cluster.name"
)
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,20 @@ func newKubernetesResourceAttributesResolver(platformCode, clusterName string) *
attributeMap: DefaultInheritedAttributes,
}
}

// Attempt to get the `k8s.cluster.name“ attribute that should be populated from resourcedetectionprocessor.
// If that attribute doesn't exist (e.g. resourcedetectionprocessor is not used or fails to get the k8s attributes),
// fallback to the processor's configured clusterName (which is "UNKNOWN" if not specified).
func (h *kubernetesResourceAttributesResolver) getResourceDetectorClusterName(resourceAttributes pcommon.Map) string {
clusterName := h.clusterName

if val, ok := resourceAttributes.Get(attr.ResourceDetectionClusterName); ok {
clusterName = val.Str()
}

return clusterName
}

func (h *kubernetesResourceAttributesResolver) Process(attributes, resourceAttributes pcommon.Map) error {
for attrKey, mappingKey := range h.attributeMap {
if val, ok := resourceAttributes.Get(attrKey); ok {
Expand All @@ -600,10 +614,10 @@ func (h *kubernetesResourceAttributesResolver) Process(attributes, resourceAttri
}
if h.platformCode == config.PlatformEKS {
attributes.PutStr(common.AttributePlatformType, AttributePlatformEKS)
attributes.PutStr(common.AttributeEKSClusterName, h.clusterName)
attributes.PutStr(common.AttributeEKSClusterName, h.getResourceDetectorClusterName(resourceAttributes))
} else {
attributes.PutStr(common.AttributePlatformType, AttributePlatformK8S)
attributes.PutStr(common.AttributeK8SClusterName, h.clusterName)
attributes.PutStr(common.AttributeK8SClusterName, h.getResourceDetectorClusterName(resourceAttributes))
}
var namespace string
if nsAttr, ok := resourceAttributes.Get(semconv.AttributeK8SNamespaceName); ok {
Expand All @@ -613,7 +627,7 @@ func (h *kubernetesResourceAttributesResolver) Process(attributes, resourceAttri
}

if val, ok := attributes.Get(attr.AWSLocalEnvironment); !ok {
env := generateLocalEnvironment(h.platformCode, h.clusterName+"/"+namespace)
env := generateLocalEnvironment(h.platformCode, h.getResourceDetectorClusterName(resourceAttributes)+"/"+namespace)
attributes.PutStr(attr.AWSLocalEnvironment, env)
} else {
attributes.PutStr(attr.AWSLocalEnvironment, val.Str())
Expand All @@ -623,7 +637,7 @@ func (h *kubernetesResourceAttributesResolver) Process(attributes, resourceAttri
// The application log group in Container Insights is a fixed pattern:
// "/aws/containerinsights/{Cluster_Name}/application"
// See https://github.com/aws/amazon-cloudwatch-agent-operator/blob/fe144bb02d7b1930715aa3ea32e57a5ff13406aa/helm/templates/fluent-bit-configmap.yaml#L82
logGroupName := "/aws/containerinsights/" + h.clusterName + "/application"
logGroupName := "/aws/containerinsights/" + h.getResourceDetectorClusterName(resourceAttributes) + "/application"
resourceAttributes.PutStr(semconv.AttributeAWSLogGroupNames, logGroupName)

return nil
Expand Down

0 comments on commit 8a94183

Please sign in to comment.