Skip to content

Conversation

@jaehanbyun
Copy link
Contributor

@jaehanbyun jaehanbyun commented Nov 2, 2025

Description

Collect k8s.container metrics for sidecar containers and ephemeral containers.

Sidecar containers (Kubernetes 1.29+): Init containers with restartPolicy: Always are now recognized as sidecars and their metrics are collected alongside regular containers.

Ephemeral containers: Debug containers added via kubectl debug are now included in metrics collection.

Regular init containers (without restartPolicy: Always) are intentionally NOT collected, as they are short-lived and complete before the main containers start.

Link to tracking issue

Fixes #42571

Testing

Unit Tests Added:

  • TestSidecarMetrics: Verifies sidecar container metrics are collected while regular init containers are not
  • TestEphemeralContainerMetrics: Verifies ephemeral container metrics are collected

Manual Testing with Kind cluster:

Test Pod:

apiVersion: v1
kind: Pod
metadata:
  name: sidecar-test-pod
spec:
  containers:
    - name: main-app
      image: busybox:latest
      resources:
        limits:
          cpu: "100m"
          memory: "64Mi"
  initContainers:
    - name: my-sidecar
      image: busybox:latest
      restartPolicy: Always  # This makes it a sidecar
      resources:
        limits:
          cpu: "50m"
          memory: "32Mi"
    - name: regular-init  # No restartPolicy - regular init container
      image: busybox:latest

Sidecar Container Metrics Output:

Resource attributes:
     -> k8s.pod.name: Str(sidecar-test-pod)
     -> k8s.container.name: Str(my-sidecar)
     -> container.image.name: Str(docker.io/library/busybox)
Metrics:
     -> k8s.container.cpu_limit: 0.050000
     -> k8s.container.cpu_request: 0.050000
     -> k8s.container.memory_limit: 33554432
     -> k8s.container.memory_request: 33554432
     -> k8s.container.ready: 1
     -> k8s.container.restarts: 0

Ephemeral Container (added via kubectl debug):

kubectl debug sidecar-test-pod -it --image=busybox:latest --target=main-app
Resource attributes:
     -> k8s.pod.name: Str(sidecar-test-pod)
     -> k8s.container.name: Str(debugger-4sw76)
     -> container.image.name: Str(docker.io/library/busybox)
Metrics:
     -> k8s.container.ready: 0
     -> k8s.container.restarts: 0

Documentation

Changelog entry added in .chloggen/collect-sidecar-metrics.yaml

@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Nov 17, 2025
@ChrsMark ChrsMark marked this pull request as draft November 24, 2025 13:08
@ChrsMark
Copy link
Member

Since the tittle states [WIP] I've converted this to draft. Feel free to mark it ready for review when it's not WIP anymore.

@jaehanbyun
Copy link
Contributor Author

@ChrsMark Thanks! I’ll do that once it’s no longer WIP.

@jaehanbyun jaehanbyun force-pushed the feat/container-metrics branch 4 times, most recently from 134f294 to b45071d Compare November 24, 2025 13:58
@github-actions github-actions bot removed the Stale label Nov 25, 2025
@jaehanbyun jaehanbyun force-pushed the feat/container-metrics branch from b45071d to b4e3234 Compare December 7, 2025 13:09
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Dec 22, 2025
@jaehanbyun jaehanbyun changed the title [WIP] k8sclusterreceiver: add init/ephemeral container metrics k8sclusterreceiver: add init/ephemeral container metrics Dec 28, 2025
@jaehanbyun jaehanbyun marked this pull request as ready for review December 28, 2025 09:08
@jaehanbyun jaehanbyun force-pushed the feat/container-metrics branch from b4e3234 to 851cb1e Compare December 28, 2025 09:11
@jaehanbyun
Copy link
Contributor Author

@ChrsMark I'd like to discuss the current implementation.

Init Container Metrics Scope

The current implementation only collects metrics for sidecar containers (init containers with restartPolicy: Always), not regular init containers.

Rationale:

Should we also collect metrics for all init containers, or is the sidecar-only approach preferred?

@ChrsMark
Copy link
Member

The current implementation only collects metrics for sidecar containers (init containers with restartPolicy: Always), not regular init containers.

Hmm, I wonder if that's a fair assumption though. It could be that some init containers turned out to be problematic and hence we want to monitor them 🤔 .

I also wonder if we should make the collection of these data configurable? @povilasv @dmitryax @TylerHelmuth WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[receiver/k8scluster] Collect k8s.container metrics for sidecars

5 participants