Menu
OpenTelemetry OpenTelemetry Collector Enriching resource attributes
Open source

Adding Kubernetes resource Attributes

OpenTelemetry Semantic conventions define a clear and useful set of attributes from Kubernetes resources like k8s.pod.name, k8s.namespace.name. To enrich the telemetry datapoints with these resource attributes, we recommend the k8satrributes processor.

Kubernetes (k8s) Attributes Processor

The processor talks to the Kubernetes API and stores the pod metadata in memory. It attaches the metadata to the data points based on the connection IP (matching the IP of the incoming OTLP Push request with the pod IP) or by custom association rules defined in the config. We recommend reading the upstream documentation on configuring the processor.

Helm Chart

If you’re using the official Helm chart, it’s easy to enable the processor:

yaml
presets:
  kubernetesAttributes:
    enabled: true

Manually setting attributes

The k8s attributes processor requires RBAC permissions with get, watch and list on pods and namespaces resources. When it is not possible to assign these permissions to the Collector, you could manually set attributes on the application manifests (i.e, on application directly instead of in Collector).

yaml
  env:
    - name: OTEL_SERVICE_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: "metadata.labels['app.kubernetes.io/component']"
    - name: OTEL_K8S_NAMESPACE
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.namespace
    - name: OTEL_K8S_NODE_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: spec.nodeName
    - name: OTEL_K8S_POD_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.name
    - name: OTEL_K8S_POD_UID
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.uid
    - name: OTEL_RESOURCE_ATTRIBUTES
      value: service.name=$(OTEL_SERVICE_NAME),service.instance.id=$(OTEL_K8S_POD_UID),service.namespace=$(OTEL_K8S_NAMESPACE),k8s.namespace.name=$(OTEL_K8S_NAMESPACE),k8s.node.name=$(OTEL_K8S_NODE_NAME),k8s.pod.name=$(OTEL_K8S_POD_NAME)

Note: This is a brittle approach and we strongly recommend using the k8s attributes processor where possible.

Adding Cloud resource attributes

OpenTelemetry also defines a semantic conventions for workloads running in the cloud, for example, cloud.region, cloud.availability_zone, etc. These attributes can be added using resourcedetection processor from the OpenTelemetry Collector.

If you’d like to use the resource detection processor inside Kubernetes, you need to run the Collector as a daemonset and send OTLP from the pods to the OTel Collector running on the node.