Skip to content

Commit

Permalink
update resourceAttributes for cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
jj22ee committed Nov 22, 2024
1 parent e5073c9 commit 499afb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,18 @@ func newKubernetesResourceAttributesResolver(platformCode, clusterName string) *
// 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 {
func (h *kubernetesResourceAttributesResolver) resolveResourceDetectorClusterName(resourceAttributes pcommon.Map) string {
clusterName := h.clusterName

// Also check for empty string from `k8s.cluster.name` attribute, since resource detection could fail
// and upstream will populate this attribute with an empty string
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.114.0/processor/resourcedetectionprocessor/internal/aws/eks/detector.go#L137-L176
if val, ok := resourceAttributes.Get(attr.ResourceDetectionClusterName); ok && val.Str() != "" {
clusterName = val.Str()
if val, ok := resourceAttributes.Get(attr.ResourceDetectionClusterName); ok {
if val.Str() == "" {
resourceAttributes.PutStr(attr.ResourceDetectionClusterName, h.clusterName)
} else {
clusterName = val.Str()
}
}

return clusterName
Expand All @@ -617,10 +621,10 @@ func (h *kubernetesResourceAttributesResolver) Process(attributes, resourceAttri
}
if h.platformCode == config.PlatformEKS {
attributes.PutStr(common.AttributePlatformType, AttributePlatformEKS)
attributes.PutStr(common.AttributeEKSClusterName, h.getResourceDetectorClusterName(resourceAttributes))
attributes.PutStr(common.AttributeEKSClusterName, h.resolveResourceDetectorClusterName(resourceAttributes))
} else {
attributes.PutStr(common.AttributePlatformType, AttributePlatformK8S)
attributes.PutStr(common.AttributeK8SClusterName, h.getResourceDetectorClusterName(resourceAttributes))
attributes.PutStr(common.AttributeK8SClusterName, h.resolveResourceDetectorClusterName(resourceAttributes))
}
var namespace string
if nsAttr, ok := resourceAttributes.Get(semconv.AttributeK8SNamespaceName); ok {
Expand All @@ -630,7 +634,7 @@ func (h *kubernetesResourceAttributesResolver) Process(attributes, resourceAttri
}

if val, ok := attributes.Get(attr.AWSLocalEnvironment); !ok {
env := generateLocalEnvironment(h.platformCode, h.getResourceDetectorClusterName(resourceAttributes)+"/"+namespace)
env := generateLocalEnvironment(h.platformCode, h.resolveResourceDetectorClusterName(resourceAttributes)+"/"+namespace)
attributes.PutStr(attr.AWSLocalEnvironment, env)
} else {
attributes.PutStr(attr.AWSLocalEnvironment, val.Str())
Expand All @@ -640,7 +644,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.getResourceDetectorClusterName(resourceAttributes) + "/application"
logGroupName := "/aws/containerinsights/" + h.resolveResourceDetectorClusterName(resourceAttributes) + "/application"
resourceAttributes.PutStr(semconv.AttributeAWSLogGroupNames, logGroupName)

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,27 +922,31 @@ func TestK8sResourceAttributesResolverOnEKS(t *testing.T) {
assert.Equal(t, val, getStrAttr(attributes, key, t), fmt.Sprintf("expected %s for key %s", val, key))
}

if val, ok := resourceAttributes.Get(attr.ResourceDetectionClusterName); ok && val.Str() != "" {
assert.Equal(t, "/aws/containerinsights/DetectedClusterName/application", getStrAttr(resourceAttributes, semconv.AttributeAWSLogGroupNames, t))
if val, ok := resourceAttributes.Get(attr.ResourceDetectionClusterName); ok {
assert.Equal(t, "/aws/containerinsights/"+val.Str()+"/application", getStrAttr(resourceAttributes, semconv.AttributeAWSLogGroupNames, t))
} else {
assert.Equal(t, "/aws/containerinsights/test-cluster/application", getStrAttr(resourceAttributes, semconv.AttributeAWSLogGroupNames, t))
}
})
}
}

func TestGetResourceDetectorClusterName(t *testing.T) {
func TestresolveResourceDetectorClusterName(t *testing.T) {
resolver := newKubernetesResourceAttributesResolver(config.PlatformEKS, "test-cluster")

resourceDetectorAttributes := pcommon.NewMap()
resourceDetectorClusterName := resolver.getResourceDetectorClusterName(resourceDetectorAttributes)
resourceDetectorClusterName := resolver.resolveResourceDetectorClusterName(resourceDetectorAttributes)
assert.Equal(t, resourceDetectorClusterName, "test-cluster")

resourceDetectorAttributes.PutStr(attr.ResourceDetectionClusterName, "DetectedClusterName")
resourceDetectorClusterName = resolver.getResourceDetectorClusterName(resourceDetectorAttributes)
resourceDetectorClusterName = resolver.resolveResourceDetectorClusterName(resourceDetectorAttributes)
assert.Equal(t, resourceDetectorClusterName, "DetectedClusterName")

resourceDetectorAttributes.PutStr(attr.ResourceDetectionClusterName, "")
resourceDetectorClusterName = resolver.getResourceDetectorClusterName(resourceDetectorAttributes)
resourceDetectorClusterName = resolver.resolveResourceDetectorClusterName(resourceDetectorAttributes)
assert.Equal(t, resourceDetectorClusterName, "test-cluster")
updatedClusterName, _ := resourceDetectorAttributes.Get(attr.ResourceDetectionClusterName)
assert.Equal(t, updatedClusterName.Str(), "test-cluster")
}

func TestK8sResourceAttributesResolverOnK8S(t *testing.T) {
Expand Down

0 comments on commit 499afb1

Please sign in to comment.