Skip to content

[receiver/k8scluster] Add node name to pod metadata #38669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/k8sclusterreceiver-node-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: receiver/k8scluster

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Add node name to pod metadata"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [37454]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
2 changes: 2 additions & 0 deletions receiver/k8sclusterreceiver/internal/pod/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func GetMetadata(pod *corev1.Pod, mc *metadata.Store, logger *zap.Logger) map[ex
meta[podStatusReason] = reason
}

meta[conventions.AttributeK8SNodeName] = pod.Spec.NodeName

for _, or := range pod.OwnerReferences {
kind := strings.ToLower(or.Kind)
meta[metadata.GetOTelNameFromKind(kind)] = or.Name
Expand Down
54 changes: 29 additions & 25 deletions receiver/k8sclusterreceiver/internal/pod/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/receiver/receivertest"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
Expand Down Expand Up @@ -249,11 +250,12 @@ func expectedKubernetesMetadata(to testCaseOptions) map[experimentalmetricmetada
ResourceIDKey: "k8s.pod.uid",
ResourceID: experimentalmetricmetadata.ResourceID(podUIDLabel),
Metadata: map[string]string{
kindNameLabel: kindObjName,
kindUIDLabel: kindObjUID,
"k8s.pod.phase": "Unknown", // Default value when phase is not set.
"k8s.namespace.name": namespaceLabel,
"k8s.pod.name": podNameLabel,
kindNameLabel: kindObjName,
kindUIDLabel: kindObjUID,
conventions.AttributeK8SNodeName: "test-node",
"k8s.pod.name": podNameLabel,
"k8s.pod.phase": "Unknown", // Default value when phase is not set.
"k8s.namespace.name": namespaceLabel,
},
},
}
Expand Down Expand Up @@ -348,7 +350,7 @@ func podWithOwnerReference(kind string) *corev1.Pod {
Name: fmt.Sprintf("test-%s-0", kindLower),
UID: types.UID(fmt.Sprintf("test-%s-0-uid", kindLower)),
},
}, testutils.NewPodWithContainer("0", &corev1.PodSpec{}, &corev1.PodStatus{}),
}, testutils.NewPodWithContainer("0", &corev1.PodSpec{NodeName: "test-node"}, &corev1.PodStatus{}),
).(*corev1.Pod)
}

Expand Down Expand Up @@ -500,32 +502,34 @@ func TestPodMetadata(t *testing.T) {
statusPhase: corev1.PodFailed,
statusReason: "Evicted",
expectedMetadata: map[string]string{
"k8s.pod.name": "test-pod-0",
"k8s.namespace.name": "test-namespace",
"k8s.pod.phase": "Failed",
"k8s.pod.status_reason": "Evicted",
"k8s.workload.kind": "Deployment",
"k8s.workload.name": "test-deployment-0",
"k8s.replicaset.name": "test-replicaset-0",
"k8s.replicaset.uid": "test-replicaset-0-uid",
"k8s.deployment.name": "test-deployment-0",
"k8s.deployment.uid": "test-deployment-0-uid",
"k8s.pod.name": "test-pod-0",
"k8s.namespace.name": "test-namespace",
"k8s.pod.phase": "Failed",
"k8s.pod.status_reason": "Evicted",
"k8s.workload.kind": "Deployment",
"k8s.workload.name": "test-deployment-0",
"k8s.replicaset.name": "test-replicaset-0",
"k8s.replicaset.uid": "test-replicaset-0-uid",
"k8s.deployment.name": "test-deployment-0",
"k8s.deployment.uid": "test-deployment-0-uid",
conventions.AttributeK8SNodeName: "test-node",
},
},
{
name: "Pod without status reason",
statusPhase: corev1.PodRunning,
statusReason: "",
expectedMetadata: map[string]string{
"k8s.pod.name": "test-pod-0",
"k8s.namespace.name": "test-namespace",
"k8s.pod.phase": "Running",
"k8s.workload.kind": "Deployment",
"k8s.workload.name": "test-deployment-0",
"k8s.replicaset.name": "test-replicaset-0",
"k8s.replicaset.uid": "test-replicaset-0-uid",
"k8s.deployment.name": "test-deployment-0",
"k8s.deployment.uid": "test-deployment-0-uid",
"k8s.pod.name": "test-pod-0",
"k8s.namespace.name": "test-namespace",
"k8s.pod.phase": "Running",
"k8s.workload.kind": "Deployment",
"k8s.workload.name": "test-deployment-0",
"k8s.replicaset.name": "test-replicaset-0",
"k8s.replicaset.uid": "test-replicaset-0-uid",
"k8s.deployment.name": "test-deployment-0",
"k8s.deployment.uid": "test-deployment-0-uid",
conventions.AttributeK8SNodeName: "test-node",
},
},
}
Expand Down
3 changes: 3 additions & 0 deletions receiver/k8sclusterreceiver/mock_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func createPods(t *testing.T, client *fake.Clientset, numPods int) []*corev1.Pod
Name: strconv.Itoa(i),
Namespace: "test",
},
Spec: corev1.PodSpec{
NodeName: "test-node",
},
}

createdPod, err := client.CoreV1().Pods(p.Namespace).Create(context.Background(), p, v1.CreateOptions{})
Expand Down
3 changes: 3 additions & 0 deletions receiver/k8sclusterreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ func getUpdatedPod(pod *corev1.Pod) any {
"key": "value",
},
},
Spec: corev1.PodSpec{
NodeName: "test-node",
},
}
}

Expand Down
37 changes: 20 additions & 17 deletions receiver/k8sclusterreceiver/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
Expand Down Expand Up @@ -270,7 +271,7 @@ func TestSyncMetadataAndEmitEntityEvents(t *testing.T) {
"otel.entity.interval": int64(7200000), // 2h in milliseconds
"otel.entity.type": "k8s.pod",
"otel.entity.id": map[string]any{"k8s.pod.uid": "pod0"},
"otel.entity.attributes": map[string]any{"pod.creation_timestamp": "0001-01-01T00:00:00Z", "k8s.pod.phase": "Unknown", "k8s.namespace.name": "test", "k8s.pod.name": "0"},
"otel.entity.attributes": map[string]any{"pod.creation_timestamp": "0001-01-01T00:00:00Z", "k8s.pod.phase": "Unknown", "k8s.namespace.name": "test", "k8s.pod.name": "0", conventions.AttributeK8SNodeName: "test-node"},
}
assert.Equal(t, expected, lr.Attributes().AsRaw())
assert.WithinRange(t, lr.Timestamp().AsTime(), step1, step2)
Expand Down Expand Up @@ -324,7 +325,7 @@ func TestObjMetadata(t *testing.T) {
EntityType: "k8s.pod",
ResourceIDKey: "k8s.pod.uid",
ResourceID: "test-pod-0-uid",
Metadata: allPodMetadata(map[string]string{"k8s.pod.phase": "Succeeded", "k8s.pod.name": "test-pod-0", "k8s.namespace.name": "test-namespace"}),
Metadata: allPodMetadata(map[string]string{"k8s.pod.phase": "Succeeded", "k8s.pod.name": "test-pod-0", "k8s.namespace.name": "test-namespace", conventions.AttributeK8SNodeName: "test-node"}),
},
experimentalmetricmetadata.ResourceID("container-id"): {
EntityType: "container",
Expand Down Expand Up @@ -353,21 +354,22 @@ func TestObjMetadata(t *testing.T) {
Name: "test-statefulset-0",
UID: "test-statefulset-0-uid",
},
}, testutils.NewPodWithContainer("0", &corev1.PodSpec{}, &corev1.PodStatus{Phase: corev1.PodFailed, Reason: "Evicted"})),
}, testutils.NewPodWithContainer("0", &corev1.PodSpec{NodeName: "test-node"}, &corev1.PodStatus{Phase: corev1.PodFailed, Reason: "Evicted"})),
want: map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{
experimentalmetricmetadata.ResourceID("test-pod-0-uid"): {
EntityType: "k8s.pod",
ResourceIDKey: "k8s.pod.uid",
ResourceID: "test-pod-0-uid",
Metadata: allPodMetadata(map[string]string{
"k8s.workload.kind": "StatefulSet",
"k8s.workload.name": "test-statefulset-0",
"k8s.statefulset.name": "test-statefulset-0",
"k8s.statefulset.uid": "test-statefulset-0-uid",
"k8s.pod.phase": "Failed",
"k8s.pod.status_reason": "Evicted",
"k8s.pod.name": "test-pod-0",
"k8s.namespace.name": "test-namespace",
"k8s.workload.kind": "StatefulSet",
"k8s.workload.name": "test-statefulset-0",
"k8s.statefulset.name": "test-statefulset-0",
"k8s.statefulset.uid": "test-statefulset-0-uid",
"k8s.pod.phase": "Failed",
"k8s.pod.status_reason": "Evicted",
"k8s.pod.name": "test-pod-0",
"k8s.namespace.name": "test-namespace",
conventions.AttributeK8SNodeName: "test-node",
}),
},
},
Expand Down Expand Up @@ -396,19 +398,20 @@ func TestObjMetadata(t *testing.T) {
}(),
resource: podWithAdditionalLabels(
map[string]string{"k8s-app": "my-app"},
testutils.NewPodWithContainer("0", &corev1.PodSpec{}, &corev1.PodStatus{Phase: corev1.PodRunning}),
testutils.NewPodWithContainer("0", &corev1.PodSpec{NodeName: "test-node"}, &corev1.PodStatus{Phase: corev1.PodRunning}),
),
want: map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{
experimentalmetricmetadata.ResourceID("test-pod-0-uid"): {
EntityType: "k8s.pod",
ResourceIDKey: "k8s.pod.uid",
ResourceID: "test-pod-0-uid",
Metadata: allPodMetadata(map[string]string{
"k8s.service.test-service": "",
"k8s-app": "my-app",
"k8s.pod.phase": "Running",
"k8s.namespace.name": "test-namespace",
"k8s.pod.name": "test-pod-0",
"k8s.service.test-service": "",
"k8s-app": "my-app",
"k8s.pod.phase": "Running",
"k8s.namespace.name": "test-namespace",
"k8s.pod.name": "test-pod-0",
conventions.AttributeK8SNodeName: "test-node",
}),
},
},
Expand Down
Loading