Skip to content

Commit

Permalink
handle case where resource detection fails and populates cluster name…
Browse files Browse the repository at this point in the history
… with empty string
  • Loading branch information
jj22ee committed Nov 21, 2024
1 parent f718c2f commit 7f577f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,16 @@ func newKubernetesResourceAttributesResolver(platformCode, clusterName string) *
}
}

// Attempt to get the `k8s.cluster.name attribute that should be populated from resourcedetectionprocessor.
// 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 {
// 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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,16 @@ func TestK8sResourceAttributesResolverOnEKS(t *testing.T) {
common.AttributeEKSClusterName: "DetectedClusterName",
},
},
{
"testEmptyClusterNameFromDetector",
map[string]string{
attr.ResourceDetectionClusterName: "",
},
map[string]string{
attr.AWSLocalEnvironment: "eks:test-cluster/test-namespace-3",
common.AttributeEKSClusterName: "test-cluster",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -912,7 +922,7 @@ func TestK8sResourceAttributesResolverOnEKS(t *testing.T) {
assert.Equal(t, val, getStrAttr(attributes, key, t), fmt.Sprintf("expected %s for key %s", val, key))
}

if _, ok := resourceAttributes.Get(attr.ResourceDetectionClusterName); ok {
if val, ok := resourceAttributes.Get(attr.ResourceDetectionClusterName); ok && val.Str() != "" {
assert.Equal(t, "/aws/containerinsights/DetectedClusterName/application", getStrAttr(resourceAttributes, semconv.AttributeAWSLogGroupNames, t))
} else {
assert.Equal(t, "/aws/containerinsights/test-cluster/application", getStrAttr(resourceAttributes, semconv.AttributeAWSLogGroupNames, t))
Expand All @@ -926,10 +936,13 @@ func TestGetResourceDetectorClusterName(t *testing.T) {

resourceDetectorAttributes := pcommon.NewMap()
resourceDetectorClusterName := resolver.getResourceDetectorClusterName(resourceDetectorAttributes)
resourceDetectorAttributes.PutStr(attr.ResourceDetectionClusterName, "DetectedClusterName")
assert.Equal(t, resourceDetectorClusterName, "test-cluster")
resourceDetectorAttributes.PutStr(attr.ResourceDetectionClusterName, "DetectedClusterName")
resourceDetectorClusterName = resolver.getResourceDetectorClusterName(resourceDetectorAttributes)
assert.Equal(t, resourceDetectorClusterName, "DetectedClusterName")
resourceDetectorAttributes.PutStr(attr.ResourceDetectionClusterName, "")
resourceDetectorClusterName = resolver.getResourceDetectorClusterName(resourceDetectorAttributes)
assert.Equal(t, resourceDetectorClusterName, "test-cluster")
}

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

0 comments on commit 7f577f6

Please sign in to comment.