|
| 1 | +<!--- Hugo front matter used to generate the website version of this page: |
| 2 | +linkTitle: K8s attributes |
| 3 | +---> |
| 4 | + |
| 5 | +## Specify resource attributes using Kubernetes annotations |
| 6 | + |
| 7 | +All annotations with the `resource.opentelemetry.io/` prefix should be translated into the corresponding resource |
| 8 | +attributes. |
| 9 | + |
| 10 | +For example, the annotation `resource.opentelemetry.io/service.name` should be translated into the `service.name` |
| 11 | +attribute. |
| 12 | + |
| 13 | +## Service attributes |
| 14 | + |
| 15 | +The following [service resource attributes](../attributes-registry/service.md) are recommended for Kubernetes services. |
| 16 | + |
| 17 | +There are different ways to calculate the service attributes. |
| 18 | + |
| 19 | +1. [Well-Known Labels](https://kubernetes.io/docs/reference/labels-annotations-taints/) |
| 20 | +2. Annotations on the pod template that have the `resource.opentelemetry.io/` prefix as described in |
| 21 | + [resource attributes using Kubernetes annotations](#specify-resource-attributes-using-kubernetes-annotations) |
| 22 | +3. A function of the Kubernetes resource attributes defined above |
| 23 | + |
| 24 | +This translation can typically be done by an OpenTelemetry Collector component like the |
| 25 | +[Kubernetes Attribute Processor](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/k8sattributesprocessor). |
| 26 | + |
| 27 | +Tools offering this functionality should provide an opt-in flag for the use of well-known labels, |
| 28 | +since users may not be aware that their labels are being used for this purpose. |
| 29 | + |
| 30 | +Each attribute has a priority order for how it should be calculated as described below. |
| 31 | + |
| 32 | +### How `service.namespace` should be calculated |
| 33 | + |
| 34 | +Choose the first value found: |
| 35 | + |
| 36 | +1. `pod.annotation[resource.opentelemetry.io/service.namespace]` |
| 37 | +2. `k8s.namespace.name` |
| 38 | + |
| 39 | +### How `service.name` should be calculated |
| 40 | + |
| 41 | +Choose the first value found: |
| 42 | + |
| 43 | +- `pod.annotation[resource.opentelemetry.io/service.name]` |
| 44 | +- `pod.label[app.kubernetes.io/instance]` (well-known label |
| 45 | + [app.kubernetes.io/instance](https://kubernetes.io/docs/reference/labels-annotations-taints/#app-kubernetes-io-instance)) |
| 46 | +- `pod.label[app.kubernetes.io/name]` (well-known label |
| 47 | + [app.kubernetes.io/name](https://kubernetes.io/docs/reference/labels-annotations-taints/#app-kubernetes-io-name)) |
| 48 | +- `k8s.deployment.name` |
| 49 | +- `k8s.replicaset.name` |
| 50 | +- `k8s.statefulset.name` |
| 51 | +- `k8s.daemonset.name` |
| 52 | +- `k8s.cronjob.name` |
| 53 | +- `k8s.job.name` |
| 54 | +- `k8s.pod.name` |
| 55 | +- `k8s.container.name` |
| 56 | + |
| 57 | +The rationale is to go from the ancestor to the descendant in the Kubernetes resource hierarchy, e.g. from `deployment` |
| 58 | +to `pod` - see [Kubernetes Object Hierarchy](https://gist.github.com/fardjad/ea3c38d566c845e0b353237d3959e365). |
| 59 | + |
| 60 | +### How `service.version` should be calculated |
| 61 | + |
| 62 | +Choose the first value found: |
| 63 | + |
| 64 | +- `pod.annotation[resource.opentelemetry.io/service.version]` |
| 65 | +- `pod.label[app.kubernetes.io/version]` (well-known label |
| 66 | + [app.kubernetes.io/version](https://kubernetes.io/docs/reference/labels-annotations-taints/#app-kubernetes-io-version)) |
| 67 | +- calculate the version using algorithm described below |
| 68 | + |
| 69 | +1. calculate tag and digest using the algorithm described in the |
| 70 | + [reference library](https://github.com/distribution/reference/blob/main/reference.go) |
| 71 | +2. choose the first value found: |
| 72 | + - `<tag>@<digest>` |
| 73 | + - `<digest>` |
| 74 | + - `<tag>` |
| 75 | + |
| 76 | +### How `service.instance.id` should be calculated |
| 77 | + |
| 78 | +Choose the first value found: |
| 79 | + |
| 80 | +- `pod.annotation[resource.opentelemetry.io/service.instance.id]` |
| 81 | +- `concat([k8s.namespace.name, k8s.pod.name, k8s.container.name], '.')` |
| 82 | + |
| 83 | +Note that the container restart count is not included in the `service.instance.id` calculation, because it makes |
| 84 | +troubleshooting harder when the ID changes on every restart, e.g. in a crash loop. |
0 commit comments