Skip to content
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

[processor/k8sattributes] Implement container.image.id for k8sattributes processor #32314

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 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/container-image-id.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: processor/k8sattributes

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for `container.image.id` metadata

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

# (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]
2 changes: 2 additions & 0 deletions processor/k8sattributesprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ Additional container level attributes can be extracted provided that certain res
- k8s.container.name
- container.image.name
- container.image.tag
- container.image.id
2. If the `k8s.container.name` resource attribute is provided, the following additional attributes will be available:
- container.image.name
- container.image.tag
- container.image.id
3. If the `k8s.container.restart_count` resource attribute is provided, it can be used to associate with a particular container
instance. If it's not set, the latest container instance will be used:
- container.id (not added by default, has to be specified in `metadata`)
Expand Down
9 changes: 5 additions & 4 deletions processor/k8sattributesprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"regexp"

conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
conventions "go.opentelemetry.io/collector/semconv/v1.21.0"
evantorrie marked this conversation as resolved.
Show resolved Hide resolved

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor/internal/kube"
Expand Down Expand Up @@ -94,7 +94,8 @@ func (cfg *Config) Validate() error {
conventions.AttributeK8SNodeName, conventions.AttributeK8SNodeUID,
conventions.AttributeK8SContainerName, conventions.AttributeContainerID,
conventions.AttributeContainerImageName, conventions.AttributeContainerImageTag,
clusterUID:
conventions.AttributeContainerImageID,
conventions.AttributeK8SClusterUID:
default:
return fmt.Errorf("\"%s\" is not a supported metadata field", field)
}
Expand Down Expand Up @@ -132,8 +133,8 @@ type ExtractConfig struct {
// k8s.daemonset.name, k8s.daemonset.uid,
// k8s.job.name, k8s.job.uid, k8s.cronjob.name,
// k8s.statefulset.name, k8s.statefulset.uid,
// k8s.container.name, container.image.name,
// container.image.tag, container.id
// k8s.container.name, container.id, container.image.name,
// container.image.tag, container.image.id,
// k8s.cluster.uid
//
// Specifying anything other than these values will result in an error.
Expand Down
18 changes: 18 additions & 0 deletions processor/k8sattributesprocessor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-traces-job"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -150,6 +151,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-traces-statefulset"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -176,6 +178,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-traces-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -199,6 +202,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-traces-daemonset"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -223,6 +227,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-metrics-job"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -247,6 +252,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-metrics-statefulset"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -273,6 +279,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-metrics-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -296,6 +303,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-metrics-daemonset"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -320,6 +328,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-logs-job"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -344,6 +353,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-logs-statefulset"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -369,6 +379,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-logs-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -393,6 +404,7 @@ func TestE2E_ClusterRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-logs-daemonset"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand Down Expand Up @@ -487,6 +499,7 @@ func TestE2E_NamespacedRBAC(t *testing.T) {
"k8s.annotations.workload": newExpectedValue(equal, "deployment"),
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-traces-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -510,6 +523,7 @@ func TestE2E_NamespacedRBAC(t *testing.T) {
"k8s.annotations.workload": newExpectedValue(equal, "deployment"),
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-metrics-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -533,6 +547,7 @@ func TestE2E_NamespacedRBAC(t *testing.T) {
"k8s.annotations.workload": newExpectedValue(equal, "deployment"),
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-logs-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand Down Expand Up @@ -641,6 +656,7 @@ func TestE2E_MixRBAC(t *testing.T) {
"k8s.annotations.workload": newExpectedValue(equal, "deployment"),
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-traces-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -667,6 +683,7 @@ func TestE2E_MixRBAC(t *testing.T) {
"k8s.annotations.workload": newExpectedValue(equal, "deployment"),
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-metrics-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand All @@ -693,6 +710,7 @@ func TestE2E_MixRBAC(t *testing.T) {
"k8s.annotations.workload": newExpectedValue(equal, "deployment"),
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-logs-deployment"),
"k8s.container.name": newExpectedValue(equal, "telemetrygen"),
"container.image.id": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"),
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
Expand Down
38 changes: 24 additions & 14 deletions processor/k8sattributesprocessor/internal/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,24 +559,26 @@ func removeUnnecessaryPodData(pod *api_v1.Pod, rules ExtractionRules) *api_v1.Po
}

if needContainerAttributes(rules) {
removeUnnecessaryContainerStatus := func(c api_v1.ContainerStatus) api_v1.ContainerStatus {
transformedContainerStatus := api_v1.ContainerStatus{
Name: c.Name,
ContainerID: c.ContainerID,
RestartCount: c.RestartCount,
}
if rules.ContainerImageID {
transformedContainerStatus.ImageID = c.ImageID
}
return transformedContainerStatus
}

for _, containerStatus := range pod.Status.ContainerStatuses {
transformedPod.Status.ContainerStatuses = append(
transformedPod.Status.ContainerStatuses,
api_v1.ContainerStatus{
Name: containerStatus.Name,
ContainerID: containerStatus.ContainerID,
RestartCount: containerStatus.RestartCount,
},
transformedPod.Status.ContainerStatuses, removeUnnecessaryContainerStatus(containerStatus),
)
}
for _, containerStatus := range pod.Status.InitContainerStatuses {
transformedPod.Status.InitContainerStatuses = append(
transformedPod.Status.InitContainerStatuses,
api_v1.ContainerStatus{
Name: containerStatus.Name,
ContainerID: containerStatus.ContainerID,
RestartCount: containerStatus.RestartCount,
},
transformedPod.Status.InitContainerStatuses, removeUnnecessaryContainerStatus(containerStatus),
)
}

Expand Down Expand Up @@ -657,11 +659,18 @@ func (c *WatchClient) extractPodContainersAttributes(pod *api_v1.Pod) PodContain
containerID = parts[1]
}
containers.ByID[containerID] = container
if c.Rules.ContainerID {
if c.Rules.ContainerID || c.Rules.ContainerImageID {
if container.Statuses == nil {
container.Statuses = map[int]ContainerStatus{}
}
container.Statuses[int(apiStatus.RestartCount)] = ContainerStatus{containerID}
containerStatus := ContainerStatus{}
if c.Rules.ContainerID {
containerStatus.ContainerID = containerID
}
if c.Rules.ContainerImageID {
containerStatus.ImageID = apiStatus.ImageID
}
container.Statuses[int(apiStatus.RestartCount)] = containerStatus
}
}
return containers
Expand Down Expand Up @@ -952,6 +961,7 @@ func needContainerAttributes(rules ExtractionRules) bool {
return rules.ContainerImageName ||
rules.ContainerName ||
rules.ContainerImageTag ||
rules.ContainerImageID ||
rules.ContainerID
}

Expand Down
Loading
Loading