Skip to content

[receiver/k8sclusterreceiver] Add missing attributes to entities in experimental entity feature #39038

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 27 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a5a989a
Modified entity payloads to include additional attributes for the fol…
k-shaikh Mar 28, 2025
50ae9b5
Merge branch 'main' into main
k-shaikh Mar 28, 2025
6ebe7d8
Added changelog file
k-shaikh Mar 28, 2025
65dd34e
Merge branch 'main' of github.com:k-shaikh/opentelemetry-collector-co…
k-shaikh Mar 28, 2025
fb6c99d
fixed tests in watcher_test.go
k-shaikh Mar 29, 2025
4fb7df8
Merge branch 'main' into main
k-shaikh Mar 29, 2025
88a52ee
Merge branch 'main' into main
k-shaikh Apr 2, 2025
25d3757
Removed unused constant containerID. Renamed containerImage value to …
k-shaikh Apr 2, 2025
429713f
Merge branch 'main' of github.com:k-shaikh/opentelemetry-collector-co…
k-shaikh Apr 2, 2025
be0da30
Merge branch 'main' into main
k-shaikh Apr 2, 2025
1b2e63c
Merge branch 'main' into main
k-shaikh Apr 2, 2025
3d87839
Changed attribute label from 'container.name' to 'k8s.container.name'
k-shaikh Apr 3, 2025
83920df
Merge branch 'main' of github.com:k-shaikh/opentelemetry-collector-co…
k-shaikh Apr 3, 2025
c21b10a
Split container image into separate attributes viz container.image.na…
k-shaikh Apr 3, 2025
0792af6
Merge branch 'main' into main
k-shaikh Apr 3, 2025
be1eec7
Include k8s.namespace.name and k8s.node.name into the metadata
k-shaikh Apr 3, 2025
8435296
Merge branch 'main' of github.com:k-shaikh/opentelemetry-collector-co…
k-shaikh Apr 3, 2025
6e542b3
Merge branch 'main' into main
k-shaikh Apr 3, 2025
3aa2e47
Merge branch 'main' into main
k-shaikh Apr 3, 2025
6f46fc2
Merge branch 'main' into main
k-shaikh Apr 3, 2025
a3eafbd
Merge branch 'main' into main
k-shaikh Apr 4, 2025
01935ca
Merge branch 'main' into main
k-shaikh Apr 4, 2025
eab1dd4
Merge branch 'main' into main
k-shaikh Apr 7, 2025
3b28f9d
Merge branch 'main' into main
k-shaikh Apr 7, 2025
e341bc2
Merge branch 'main' into main
k-shaikh Apr 7, 2025
ba17e78
Merge branch 'main' into main
k-shaikh Apr 7, 2025
50a8c92
Merge branch 'main' into main
k-shaikh Apr 7, 2025
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/partial-fix-issue-39038.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: k8sclusterreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add missing attributes to entities in experimental entity feature

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

# (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: [user]
4 changes: 4 additions & 0 deletions receiver/k8sclusterreceiver/internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ const (
K8sKeyReplicationControllerUID = "k8s.replicationcontroller.uid"
K8sKeyResourceQuotaUID = "k8s.resourcequota.uid"
K8sKeyClusterResourceQuotaUID = "openshift.clusterquota.uid"
K8sKeyPodUID = "k8s.pod.uid"

// Resource labels keys for Name.
K8sKeyReplicationControllerName = "k8s.replicationcontroller.name"
K8sKeyResourceQuotaName = "k8s.resourcequota.name"
K8sKeyClusterResourceQuotaName = "openshift.clusterquota.name"
K8sKeyNamespaceName = "k8s.namespace.name"
K8sKeyPodName = "k8s.pod.name"
K8sKeyNodeName = "k8s.node.name"

// Kubernetes resource kinds
K8sKindCronJob = "CronJob"
Expand Down
20 changes: 19 additions & 1 deletion receiver/k8sclusterreceiver/internal/container/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/docker"
metadataPkg "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
imetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/utils"
Expand All @@ -23,6 +24,9 @@ const (
containerKeyStatus = "container.status"
containerKeyStatusReason = "container.status.reason"
containerCreationTimestamp = "container.creation_timestamp"
containerName = "k8s.container.name"
containerImageName = "container.image.name"
containerImageTag = "container.image.tag"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
containerImageTag = "container.image.tag"
containerImageTags = "container.image.tags"

I think this needs to be alist based on https://opentelemetry.io/docs/specs/semconv/attributes-registry/container/#container-image-tags

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@povilasv , the current struct for sending metadata only allows values of type string. We can change the label to 'container.image.tags' but changing the value to a list will be very big change.

IMO, we can use the label container.image.tag since its also used here

Also even though an image may contain multiple tags the container spec references a specific tag so the multiple tags maybe relevant for the 'Image' entity it may not be relevant for the container entity.

@dmitryax , do you have any opinion?

Copy link
Member

@dmitryax dmitryax Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the collector currently doesn't send any container.image.tags attribute, only container.image.tag, it'd be in inconsistent state sending data that cannot be easily correlated. I believe, in order to migrate from container.image.tag to container.image.tags attribute, we need to apply the changes to all sources consistently via a feature gate, maybe even keeping the old behavior as an option.


// Values for container metadata
containerStatusRunning = "running"
Expand Down Expand Up @@ -96,9 +100,23 @@ func RecordSpecMetrics(logger *zap.Logger, mb *imetadata.MetricsBuilder, c corev
mb.EmitForResource(imetadata.WithResource(rb.Emit()))
}

func GetMetadata(cs corev1.ContainerStatus) *metadata.KubernetesMetadata {
func GetMetadata(pod *corev1.Pod, cs corev1.ContainerStatus, logger *zap.Logger) *metadata.KubernetesMetadata {
mdata := map[string]string{}

imageStr := cs.Image
image, err := docker.ParseImageName(cs.Image)
if err != nil {
docker.LogParseError(err, imageStr, logger)
} else {
mdata[containerImageName] = image.Repository
mdata[containerImageTag] = image.Tag
}
mdata[containerName] = cs.Name
mdata[constants.K8sKeyPodName] = pod.Name
mdata[constants.K8sKeyPodUID] = string(pod.UID)
mdata[constants.K8sKeyNamespaceName] = pod.Namespace
mdata[constants.K8sKeyNodeName] = pod.Spec.NodeName

if cs.State.Running != nil {
mdata[containerKeyStatus] = containerStatusRunning
if !cs.State.Running.StartedAt.IsZero() {
Expand Down
91 changes: 76 additions & 15 deletions receiver/k8sclusterreceiver/internal/container/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,42 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants"
)

func TestGetMetadata(t *testing.T) {
refTime := v1.Now()
pod := &corev1.Pod{
ObjectMeta: v1.ObjectMeta{
Name: "test-pod",
Namespace: "test-namespace",
UID: types.UID("test-pod-uid"),
},
Spec: corev1.PodSpec{
NodeName: "test-node",
},
}

tests := []struct {
name string
containerState corev1.ContainerState
expectedStatus string
expectedReason string
expectedStartedAt string
name string
containerState corev1.ContainerState
expectedStatus string
expectedReason string
expectedStartedAt string
containerName string
containerID string
containerImage string
containerImageName string
containerImageTag string
podName string
podUID string
nodeName string
namespaceName string
}{
{
name: "Running container",
Expand All @@ -29,8 +53,17 @@ func TestGetMetadata(t *testing.T) {
StartedAt: refTime,
},
},
expectedStatus: containerStatusRunning,
expectedStartedAt: refTime.Format(time.RFC3339),
expectedStatus: containerStatusRunning,
expectedStartedAt: refTime.Format(time.RFC3339),
containerName: "my-test-container1",
containerID: "f37ee861-f093-4cea-aa26-f39fff8b0998",
containerImage: "docker/someimage1:v1.0",
containerImageName: "docker/someimage1",
containerImageTag: "v1.0",
podName: pod.Name,
podUID: string(pod.UID),
namespaceName: "test-namespace",
nodeName: "test-node",
},
{
name: "Terminated container",
Expand All @@ -43,9 +76,18 @@ func TestGetMetadata(t *testing.T) {
ExitCode: 0,
},
},
expectedStatus: containerStatusTerminated,
expectedReason: "Completed",
expectedStartedAt: refTime.Format(time.RFC3339),
expectedStatus: containerStatusTerminated,
expectedReason: "Completed",
expectedStartedAt: refTime.Format(time.RFC3339),
containerName: "my-test-container2",
containerID: "f37ee861-f093-4cea-aa26-f39fff8b0997",
containerImage: "docker/someimage2:v1.1",
containerImageName: "docker/someimage2",
containerImageTag: "v1.1",
podName: pod.Name,
podUID: string(pod.UID),
namespaceName: "test-namespace",
nodeName: "test-node",
},
{
name: "Waiting container",
Expand All @@ -54,17 +96,29 @@ func TestGetMetadata(t *testing.T) {
Reason: "CrashLoopBackOff",
},
},
expectedStatus: containerStatusWaiting,
expectedReason: "CrashLoopBackOff",
expectedStatus: containerStatusWaiting,
expectedReason: "CrashLoopBackOff",
containerName: "my-test-container3",
containerID: "f37ee861-f093-4cea-aa26-f39fff8b0996",
containerImage: "docker/someimage3:latest",
containerImageName: "docker/someimage3",
containerImageTag: "latest",
podName: pod.Name,
podUID: string(pod.UID),
namespaceName: "test-namespace",
nodeName: "test-node",
},
}

logger := zap.NewNop()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cs := corev1.ContainerStatus{
State: tt.containerState,
State: tt.containerState,
Name: tt.containerName,
ContainerID: tt.containerID,
Image: tt.containerImage,
}
md := GetMetadata(cs)
md := GetMetadata(pod, cs, logger)

require.NotNil(t, md)
assert.Equal(t, tt.expectedStatus, md.Metadata[containerKeyStatus])
Expand All @@ -75,6 +129,13 @@ func TestGetMetadata(t *testing.T) {
assert.Contains(t, md.Metadata, containerCreationTimestamp)
assert.Equal(t, tt.expectedStartedAt, md.Metadata[containerCreationTimestamp])
}
assert.Equal(t, tt.containerName, md.Metadata[containerName])
assert.Equal(t, tt.containerImageName, md.Metadata[containerImageName])
assert.Equal(t, tt.containerImageTag, md.Metadata[containerImageTag])
assert.Equal(t, tt.podName, md.Metadata[constants.K8sKeyPodName])
assert.Equal(t, tt.podUID, md.Metadata[constants.K8sKeyPodUID])
assert.Equal(t, tt.namespaceName, md.Metadata[constants.K8sKeyNamespaceName])
assert.Equal(t, tt.nodeName, md.Metadata[constants.K8sKeyNodeName])
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestCronJobMetadata(t *testing.T) {
"concurrency_policy": "concurrency_policy",
"k8s.workload.kind": "CronJob",
"k8s.workload.name": "test-cronjob-1",
"k8s.namespace.name": "test-namespace",
},
},
*actualMetadata["test-cronjob-1-uid"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func GetGenericMetadata(om *v1.ObjectMeta, resourceType string) *KubernetesMetad
metadata[constants.K8sKeyWorkLoadKind] = resourceType
metadata[constants.K8sKeyWorkLoadName] = om.Name
metadata[rType+".creation_timestamp"] = om.GetCreationTimestamp().Format(time.RFC3339)
metadata[constants.K8sKeyNamespaceName] = om.Namespace

for _, or := range om.OwnerReferences {
kind := strings.ToLower(or.Kind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func Test_getGenericMetadata(t *testing.T) {
om := &v1.ObjectMeta{
Name: "test-name",
UID: "test-uid",
Namespace: "test-namespace",
Generation: 0,
CreationTimestamp: v1.NewTime(now),
Labels: map[string]string{
Expand Down Expand Up @@ -46,6 +47,7 @@ func Test_getGenericMetadata(t *testing.T) {
assert.Equal(t, map[string]string{
"k8s.workload.name": "test-name",
"k8s.workload.kind": "ResourceType",
"k8s.namespace.name": "test-namespace",
"resourcetype.creation_timestamp": now.Format(time.RFC3339),
"k8s.owner-kind-1.name": "owner1",
"k8s.owner-kind-1.uid": "owner1",
Expand Down
9 changes: 6 additions & 3 deletions receiver/k8sclusterreceiver/internal/pod/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func GetMetadata(pod *corev1.Pod, mc *metadata.Store, logger *zap.Logger) map[ex
meta = maps.MergeStringMaps(meta, collectPodReplicaSetProperties(pod, store, logger))
}

meta[constants.K8sKeyNamespaceName] = pod.Namespace
meta[constants.K8sKeyPodName] = pod.Name

podID := experimentalmetricmetadata.ResourceID(pod.UID)
return metadata.MergeKubernetesMetadataMaps(map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{
podID: {
Expand All @@ -172,7 +175,7 @@ func GetMetadata(pod *corev1.Pod, mc *metadata.Store, logger *zap.Logger) map[ex
ResourceID: podID,
Metadata: meta,
},
}, getPodContainerProperties(pod))
}, getPodContainerProperties(pod, logger))
}

// collectPodJobProperties checks if pod owner of type Job is cached. Check owners reference
Expand Down Expand Up @@ -249,10 +252,10 @@ func getWorkloadProperties(ref *v1.OwnerReference, labelKey string) map[string]s
}
}

func getPodContainerProperties(pod *corev1.Pod) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
func getPodContainerProperties(pod *corev1.Pod, logger *zap.Logger) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
km := map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{}
for _, cs := range pod.Status.ContainerStatuses {
md := container.GetMetadata(cs)
md := container.GetMetadata(pod, cs, logger)
km[md.ResourceID] = md
}
return km
Expand Down
16 changes: 13 additions & 3 deletions receiver/k8sclusterreceiver/internal/pod/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ func testCaseForPodWorkload(to testCaseOptions) testCase {

func expectedKubernetesMetadata(to testCaseOptions) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
podUIDLabel := "test-pod-0-uid"
podNameLabel := "test-pod-0"
kindLower := strings.ToLower(to.kind)
kindObjName := fmt.Sprintf("test-%s-0", kindLower)
namespaceLabel := "test-namespace"
kindObjUID := fmt.Sprintf("test-%s-0-uid", kindLower)
kindNameLabel := fmt.Sprintf("k8s.%s.name", kindLower)
kindUIDLabel := fmt.Sprintf("k8s.%s.uid", kindLower)
Expand All @@ -247,9 +249,11 @@ 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.
kindNameLabel: kindObjName,
kindUIDLabel: kindObjUID,
"k8s.pod.phase": "Unknown", // Default value when phase is not set.
"k8s.namespace.name": namespaceLabel,
"k8s.pod.name": podNameLabel,
},
},
}
Expand Down Expand Up @@ -485,6 +489,8 @@ func TestTransform(t *testing.T) {
func TestPodMetadata(t *testing.T) {
tests := []struct {
name string
podName string
namespace string
statusPhase corev1.PodPhase
statusReason string
expectedMetadata map[string]string
Expand All @@ -494,6 +500,8 @@ 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",
Expand All @@ -509,6 +517,8 @@ func TestPodMetadata(t *testing.T) {
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestStatefulsetMetadata(t *testing.T) {
Metadata: map[string]string{
"k8s.workload.name": "test-statefulset-1",
"k8s.workload.kind": "StatefulSet",
"k8s.namespace.name": "test-namespace",
"statefulset.creation_timestamp": "0001-01-01T00:00:00Z",
"foo": "bar",
"foo1": "",
Expand Down
Loading