
Beyond the defaults: extending Kubernetes enrichment with your own labels and annotations
By Nicolas Narbais
Add Kubernetes pod and namespace labels or annotations to OpenTelemetry logs with the k8s_attributes processor.
Turn on k8s_attributes and every span, log, and metric gets six built-in fields: k8s.namespace.name, k8s.pod.name, k8s.pod.uid, k8s.pod.start_time, k8s.deployment.name, and k8s.node.name.
Those six fields tell you where something ran, not who owns it or what tier it serves. That context already lives on your Kubernetes labels and annotations, one config block away from your telemetry.
This guide shows how to extract those fields safely, why extraction rules silently lose to upstream attributes, and what extra Kubernetes watchers cost your Collector.
Pinned versions: Examples below target Collector Contrib v0.158.0, whose k8s_attributes docs declare Semantic Conventions v1.42.0. This article assumes pod association already works: association finds which pod emitted a record, and extraction adds metadata after association succeeds.
Check built-in metadata first: Before extracting labels, check extract.metadata. It already supports k8s.container.name, container.id, container image fields, ReplicaSet/StatefulSet/DaemonSet/Job/CronJob, cluster identity, and derived service.* fields.
Extract labels and annotations from pods, namespaces, and nodes
Two keys drive custom extraction (extract.labels and extract.annotations) using the same four fields (k8s_attributes README):
| Field | Purpose |
|---|---|
key | Exact label or annotation key to read |
key_regex | Pattern match (mutually exclusive with key) |
from | Object scope: pod (default), namespace, deployment, statefulset, daemonset, job, or node |
tag_name | Optional output attribute name |
Put shared values at the highest scope. Putting project on one namespace beats maintaining it on forty Deployments.
processors:
k8s_attributes:
auth_type: serviceAccount
extract:
labels:
# Set once on the namespace rather than every workload
- from: namespace
key: project
tag_name: deployment.project.name
# Ownership label on the pod template
- from: pod
key: app.kubernetes.io/component
# Topology label on the node
- from: node
key: topology.kubernetes.io/zone
tag_name: cloud.availability_zone
For a governed family of keys, use a narrow prefix with key_regex and a $1 backreference:
labels:
- from: pod
key_regex: observability\.example\.com/(.*)
tag_name: $1
Translate resource.opentelemetry.io/ pod annotations with one switch
OpenTelemetry defines the resource.opentelemetry.io/ prefix for pod annotations that map directly into resource attributes (Kubernetes semantic conventions). Enable it with one flag:
extract:
otel_annotations: true
This option is pod-scoped (not configurable with from:) and defaults to false. With it on, pod annotations translate automatically without per-key rules:
metadata:
annotations:
resource.opentelemetry.io/service.version: "2.4.1"
resource.opentelemetry.io/deployment.environment.name: production
Any key under resource.opentelemetry.io/ translates, including custom names like deployment.project.name that are not part of OpenTelemetry semantic conventions. Enable otel_annotations only when resource.opentelemetry.io/ is governed by policy or review; if platform teams need a strict allowlist, stick to explicit extract.labels and extract.annotations.
Omit tag_name to keep Kubernetes provenance
tag_name is optional. Leave it out and the processor names the attribute from the Kubernetes object and key:
extract:
labels:
- from: pod
key: app.kubernetes.io/component
- from: namespace
key: team
- from: node
key: topology.kubernetes.io/zone
On v0.158.0, the generated name depends on the processor.k8sattributes.EmitV1K8sConventions feature gate:
| Feature gate state | Generated attribute names |
|---|---|
| Off (legacy plural) | k8s.pod.labels.app.kubernetes.io/component, k8s.namespace.labels.team, k8s.node.labels.topology.kubernetes.io/zone |
| On (Stable singular) | k8s.pod.label.app.kubernetes.io/component, k8s.namespace.label.team, k8s.node.label.topology.kubernetes.io/zone |
Use this simple rule to decide whether to set tag_name:
- Omit
tag_namewhen you want to preserve provenance (k8s.namespace.label.team=paymentstells you both the value and where it lives). - Set
tag_namewhen intentionally translating Kubernetes metadata into a cross-environment concept shared with VMs or serverless (for exampleorganization.team,deployment.project.name, orcloud.availability_zone).
Why extraction rules never overwrite existing attributes
Your label exists, RBAC is valid, k8s_attributes is wired in the pipeline, and the extracted value still never shows up. Check whether the attribute already had a value before reaching the Collector.
In v0.158.0, the processor enforces a fill-gaps-only rule with no overwrite and no warning:
func setResourceAttribute(attributes pcommon.Map, key, val string) {
attr, found := attributes.Get(key)
if !found || attr.AsString() == "" {
attributes.PutStr(key, val)
}
}
A concrete collision
Suppose the Collector extracts pod label app.kubernetes.io/name=checkout-from-label as service.name and namespace label project=payment as deployment.project.name:
| Scenario | Application sends | Result after k8s_attributes |
|---|---|---|
| Run 1 | service.name=checkout-from-sdk only | service.name=checkout-from-sdk, deployment.project.name=payment |
| Run 2 | service.name=checkout-from-sdk and deployment.project.name=payment-sdk | service.name=checkout-from-sdk, deployment.project.name=payment-sdk |
Nothing in the Collector YAML changed. The incoming resource map decided what survived.
Six sources across two pipeline stages
Because upstream and Collector sources run at different stages, they do not collapse into one global precedence table:
| Stage | Source | Typical attributes set |
|---|---|---|
| Before export | Application SDK (OTEL_SERVICE_NAME, OTEL_RESOURCE_ATTRIBUTES, detectors) | service.name, service.version, service.instance.id, custom tags |
| Before export | OpenTelemetry Operator auto-instrumentation | service.name, service.namespace, service.instance.id |
| Before export | resource.opentelemetry.io/ annotations translated by Operator | Any annotated resource attribute |
In k8s_attributes | otel_annotations: true | Candidate attributes from pod annotations |
In k8s_attributes | extract.labels / extract.annotations | Candidate mapped labels/annotations |
In k8s_attributes | extract.metadata | k8s.namespace.name, k8s.pod.name, k8s.deployment.name, k8s.node.name |
Internal derivation rules within semantic conventions (such as deriving service.name from annotations, app labels, then workload hierarchy) have shifted across releases, so test them on your exact versions. To debug any rule fast, inspect what arrived before k8s_attributes.
Use Collector enrichment for attributes the application should not know (topology, cluster, namespace ownership). Keep service.name owned by the service team, treating a Collector tag_name: service.name rule only as a fallback.
What to allowlist and what extra watchers cost
Every extracted key becomes a resource attribute attached to every span, log record, and metric point from that workload.
Never run key_regex: (.*) unless you control every label in the cluster. It sweeps in pod-template-hash, rollout hashes, chart metadata, and unintended annotations. Keeping noise out of the pipeline is cheaper than redacting it downstream.
Keep Kubernetes’s own boundary intact too: labels are for selection, annotations are for non-identifying metadata. Do not convert metadata into labels just to ingest it; use extract.annotations.
Gate every proposed attribute with four questions:
- Cardinality: Does it have ~20 values (team name) or unbounded churn (commit SHA on every deploy)?
- Ownership: If a controller or Helm chart generates the key, upgrades can reshape it without warning.
- Query value: If nobody filters or groups by it during an incident, drop it.
- Blast radius: Is it safe inside your telemetry backend’s access boundary?
Collector memory and watch overhead
Extracting labels or annotations from deployment, statefulset, daemonset, or job is disabled by default because each object type adds Kubernetes informers, memory caches, and RBAC requirements.
One distinction matters on v0.158.0: the default k8s.deployment.name is derived from ReplicaSet ownership without watching Deployment objects. Extracting labels from Deployments enables extra ReplicaSet/Deployment watches and is not free.
Two knobs control cache overhead and lifecycle edges:
watch_sync_period: defaults to5m; set to0son large clusters to stop periodic resync churn across cached objects.pod_delete_grace_period: defaults to120s; keeps deleted pods in cache so delayed telemetry still gets enriched.
processors:
k8s_attributes:
watch_sync_period: 0s
pod_delete_grace_period: 120s
Verify enrichment and avoid startup gaps
Temporarily enable debug with verbosity: detailed and inspect the Resource attributes block to confirm exact key names and check for upstream collisions:
exporters:
debug:
verbosity: detailed
Then watch two processor metrics:
otelcol.k8s.pod.association: counts association checks (must succeed before extraction runs).otelcol.k8s.watcher.pod_cache.size: measures pod cache size only. Higher-level object watchers report under their own watcher counters.
A common false negative happens right after a Collector restart: the first batches pass through before the informer cache finishes syncing (upstream issue #36147). Enable wait_for_metadata so startup blocks until caches are ready:
processors:
k8s_attributes:
wait_for_metadata: true
wait_for_metadata_timeout: 10s
The tradeoff is intentional: if metadata cannot sync inside 10 seconds, the Collector fails fast (triggering an alert) instead of silently shipping unattributed telemetry. Together, wait_for_metadata protects Collector startup while pod_delete_grace_period protects pod teardown.
Two non-extraction failure modes sit outside these rules:
- Attributes missing entirely: association is broken upstream (typically a gateway, proxy, or mesh hiding the source pod IP).
- Multi-container pods missing image metadata: pod association alone does not identify the container; incoming records need
container.idork8s.container.name.
Who owns each attribute
The YAML takes ten minutes. Making it durable means writing down who owns each row:
| Attribute | Owner | Mechanism |
|---|---|---|
service.name, service.version | Application team | SDK env vars or resource.opentelemetry.io/ annotations |
deployment.project.name (custom) | Platform | Namespace label → extract.labels with tag_name |
| Cluster, namespace, node, workload identity | Platform | Built-in extract.metadata |
| Topology and zone | Platform | Node labels or cloud resource detector |
| Team, tier, cost center | Label maintainer | extract.labels without tag_name |
Pick one label your on-call rotation already uses to route pages, extract it without tag_name, enable wait_for_metadata, and verify it in debug output before adding a second rule.
Written by
Nicolas Narbais
Nicolas Narbais builds, breaks, debugs, and scales observability systems using OpenTelemetry and modern telemetry pipelines. He shares practical, opinionated lessons to help engineers get better signal with less noise.
Olatuak Editorial Philosophy
Better signal, not more telemetry.
Olatuak helps platform engineers, SREs, and observability teams turn noisy telemetry into clear operational decisions without blindly collecting more data.
