Skip to content

Commit 647de87

Browse files
zeitlingersmithclayjoaopgrassilmolkovatrask
authored
Kubernetes: How to populate resource attributes based on attributes, labels and transformation (#1756)
Co-authored-by: smithclay <clay@lightstep.com> Co-authored-by: Joao Grassi <5938087+joaopgrassi@users.noreply.github.com> Co-authored-by: Liudmila Molkova <limolkova@microsoft.com> Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent 194dcf5 commit 647de87

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
change_type: enhancement
2+
3+
component: k8s
4+
5+
note: How to populate resource attributes based on attributes, labels and transformation
6+
7+
issues: [236]

.github/CODEOWNERS

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@
5959
/model/device/ @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-mobile-approvers
6060

6161
# K8s semantic conventions
62-
/docs/resource/k8s.md @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-k8s-approvers
63-
/model/k8s/ @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-k8s-approvers
64-
/docs/non-normative/k8s-migration.md @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-k8s-approvers
62+
/docs/resource/k8s.md @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-k8s-approvers
63+
/model/k8s/ @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-k8s-approvers
64+
/docs/non-normative/k8s-attributes.md @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-k8s-approvers
65+
/docs/non-normative/k8s-migration.md @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-k8s-approvers
6566

6667
# Container semantic conventions
6768
/docs/resource/container.md @open-telemetry/specs-semconv-approvers @open-telemetry/semconv-container-approvers

docs/non-normative/k8s-attributes.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.

docs/resource/k8s.md

+6
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,9 @@ A ResourceQuota provides constraints that limit aggregate resource consumption p
439439
<!-- endsemconv -->
440440

441441
[DocumentStatus]: https://opentelemetry.io/docs/specs/otel/document-status
442+
443+
## Kubernetes specific guidelines
444+
445+
The following guidelines are specific to Kubernetes:
446+
447+
- [Service in Kubernetes](../non-normative/k8s-attributes.md#service-attributes)

0 commit comments

Comments
 (0)