Skip to content

Commit 8745d54

Browse files
committed
changes from feedback
1 parent 40466a6 commit 8745d54

10 files changed

+201
-84
lines changed

.chloggen/resource-attribute-from-annotations.yaml

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
change_type: breaking
1+
change_type: enhancement
22

33
component: auto-instrumentation
44

5-
note: Add support for k8s labels such as app.kubernetes.io/name and annotations for resource attributes
5+
note: Add support for k8s labels such as app.kubernetes.io/name for resource attributes
66

77
issues: [3112]
88

99
subtext: |
10-
It's a breaking change because these annotations are well-known and widely used in the Kubernetes ecosystem. The following labels are supported:
10+
You can opt-in as follows:
11+
```yaml
12+
apiVersion: opentelemetry.io/v1alpha1
13+
kind: Instrumentation
14+
metadata:
15+
name: my-instrumentation
16+
spec:
17+
defaults:
18+
useLabelsForResourceAttributes: true
19+
```
20+
The following labels are supported:
1121
- `app.kubernetes.io/name` becomes `service.name`
1222
- `app.kubernetes.io/version` becomes `service.version`
1323
- `app.kubernetes.io/part-of` becomes `service.namespace`

README.md

+29-4
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ spec:
735735
image: your-image:tag
736736
```
737737

738-
You can also use labels to set resource attributes.
738+
You can also use common labels to set resource attributes.
739739

740740
The following labels are supported:
741741
- `app.kubernetes.io/name` becomes `service.name`
@@ -754,11 +754,36 @@ metadata:
754754
app.kubernetes.io/part-of: "shop"
755755
app.kubernetes.io/instance: "my-service-123"
756756
spec:
757-
containers:
758-
- name: main-container
759-
image: your-image:tag
757+
containers:
758+
- name: main-container
759+
image: your-image:tag
760+
```
761+
762+
This requires an explicit opt-in as follows:
763+
764+
```yaml
765+
apiVersion: opentelemetry.io/v1alpha1
766+
kind: Instrumentation
767+
metadata:
768+
name: my-instrumentation
769+
spec:
770+
defaults:
771+
useLabelsForResourceAttributes: true
760772
```
761773

774+
#### Priority for setting resource attributes
775+
776+
The priority for setting resource attributes is as follows (first found wins):
777+
778+
- Resource attributes set via `OTEL_RESOURCE_ATTRIBUTES` and `OTEL_SERVICE_NAME` environment variables
779+
- Resource attributes calculated from the pod's metadata (e.g. `k8s.pod.name`)
780+
- Resource attributes set via the `Instrumentation` CR (in the `spec.resource.resourceAttributes` section)
781+
- Resource attributes set via annotations (with the `resource.opentelemetry.io/` prefix)
782+
- Resource attributes set via labels (e.g. `app.kubernetes.io/name`)
783+
784+
This priority is applied for each resource attribute separately, so it is possible to set some attributes via
785+
annotations and others via labels.
786+
762787
## Compatibility matrix
763788

764789
### OpenTelemetry Operator vs. OpenTelemetry Collector

apis/v1alpha1/instrumentation_types.go

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type InstrumentationSpec struct {
4040
// +optional
4141
Sampler `json:"sampler,omitempty"`
4242

43+
// Defaults defines default values for the instrumentation.
44+
Defaults Defaults `json:"defaults,omitempty"`
45+
4346
// Env defines common env vars. There are four layers for env vars' definitions and
4447
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
4548
// If the former var had been defined, then the other vars would be ignored.
@@ -114,6 +117,12 @@ type Sampler struct {
114117
Argument string `json:"argument,omitempty"`
115118
}
116119

120+
// Defaults defines default values for the instrumentation.
121+
type Defaults struct {
122+
// UseLabelsForResourceAttributes defines whether to use common labels for resource attributes.
123+
UseLabelsForResourceAttributes bool `json:"useLabelsForResourceAttributes,omitempty"`
124+
}
125+
117126
// Java defines Java SDK and instrumentation configuration.
118127
type Java struct {
119128
// Image is a container image with javaagent auto-instrumentation JAR.

pkg/constants/env.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ const (
3131
AnnotationDefaultAutoInstrumentationApacheHttpd = InstrumentationPrefix + "default-auto-instrumentation-apache-httpd-image"
3232
AnnotationDefaultAutoInstrumentationNginx = InstrumentationPrefix + "default-auto-instrumentation-nginx-image"
3333

34-
AnnotationAppName = "app.kubernetes.io/name"
35-
AnnotationAppInstance = "app.kubernetes.io/instance"
36-
AnnotationAppVersion = "app.kubernetes.io/version"
37-
AnnotationAppPartOf = "app.kubernetes.io/part-of"
34+
LabelAppName = "app.kubernetes.io/name"
35+
LabelAppInstance = "app.kubernetes.io/instance"
36+
LabelAppVersion = "app.kubernetes.io/version"
37+
LabelAppPartOf = "app.kubernetes.io/part-of"
3838

3939
ResourceAttributeAnnotationPrefix = "resource.opentelemetry.io/"
4040

pkg/instrumentation/apachehttpd.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const (
5959
6) Inject mounting of volumes / files into appropriate directories in application container
6060
*/
6161

62-
func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod corev1.Pod, index int, otlpEndpoint string, resourceMap map[string]string) corev1.Pod {
62+
func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod corev1.Pod, useLabelsForResourceAttributes bool, index int, otlpEndpoint string, resourceMap map[string]string) corev1.Pod {
6363

6464
// caller checks if there is at least one container
6565
container := &pod.Spec.Containers[index]
@@ -95,7 +95,7 @@ func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod
9595
MountPath: apacheAgentConfDirFull,
9696
})
9797
// remove resource requirements since those are then reserved for the lifetime of a pod
98-
// and we definitely do not need them for the init container for cp command
98+
// ]and we definitely do not need them for the init container for cp command
9999
cloneContainer.Resources = apacheSpec.Resources
100100
// remove livenessProbe, readinessProbe, and startupProbe, since not supported on init containers
101101
cloneContainer.LivenessProbe = nil
@@ -162,7 +162,7 @@ func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod
162162
Env: []corev1.EnvVar{
163163
{
164164
Name: apacheAttributesEnvVar,
165-
Value: getApacheOtelConfig(pod, apacheSpec, index, otlpEndpoint, resourceMap),
165+
Value: getApacheOtelConfig(pod, useLabelsForResourceAttributes, apacheSpec, index, otlpEndpoint, resourceMap),
166166
},
167167
{Name: apacheServiceInstanceIdEnvVar,
168168
ValueFrom: &corev1.EnvVarSource{
@@ -201,7 +201,7 @@ func isApacheInitContainerMissing(pod corev1.Pod, containerName string) bool {
201201

202202
// Calculate Apache HTTPD agent configuration file based on attributes provided by the injection rules
203203
// and by the pod values.
204-
func getApacheOtelConfig(pod corev1.Pod, apacheSpec v1alpha1.ApacheHttpd, index int, otelEndpoint string, resourceMap map[string]string) string {
204+
func getApacheOtelConfig(pod corev1.Pod, useLabelsForResourceAttributes bool, apacheSpec v1alpha1.ApacheHttpd, index int, otelEndpoint string, resourceMap map[string]string) string {
205205
template := `
206206
#Load the Otel Webserver SDK
207207
LoadFile %[1]s/sdk_lib/lib/libopentelemetry_common.so
@@ -222,7 +222,7 @@ LoadModule otel_apache_module %[1]s/WebServerModule/Apache/libmod_apache_otel%[2
222222
if otelEndpoint == "" {
223223
otelEndpoint = "http://localhost:4317/"
224224
}
225-
serviceName := chooseServiceName(pod, resourceMap, index)
225+
serviceName := chooseServiceName(pod, useLabelsForResourceAttributes, resourceMap, index)
226226
serviceNamespace := pod.GetNamespace()
227227
if len(serviceNamespace) == 0 {
228228
serviceNamespace = resourceMap[string(semconv.K8SNamespaceNameKey)]

pkg/instrumentation/apachehttpd_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func TestInjectApacheHttpdagent(t *testing.T) {
417417

418418
for _, test := range tests {
419419
t.Run(test.name, func(t *testing.T) {
420-
pod := injectApacheHttpdagent(logr.Discard(), test.ApacheHttpd, test.pod, 0, "http://otlp-endpoint:4317", resourceMap)
420+
pod := injectApacheHttpdagent(logr.Discard(), test.ApacheHttpd, test.pod, false, 0, "http://otlp-endpoint:4317", resourceMap)
421421
assert.Equal(t, test.expected, pod)
422422
})
423423
}
@@ -527,7 +527,7 @@ func TestInjectApacheHttpdagentUnknownNamespace(t *testing.T) {
527527

528528
for _, test := range tests {
529529
t.Run(test.name, func(t *testing.T) {
530-
pod := injectApacheHttpdagent(logr.Discard(), test.ApacheHttpd, test.pod, 0, "http://otlp-endpoint:4317", resourceMap)
530+
pod := injectApacheHttpdagent(logr.Discard(), test.ApacheHttpd, test.pod, false, 0, "http://otlp-endpoint:4317", resourceMap)
531531
assert.Equal(t, test.expected, pod)
532532
})
533533
}

pkg/instrumentation/nginx.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const (
6262
6) Inject mounting of volumes / files into appropriate directories in the application container
6363
*/
6464

65-
func injectNginxSDK(_ logr.Logger, nginxSpec v1alpha1.Nginx, pod corev1.Pod, index int, otlpEndpoint string, resourceMap map[string]string) corev1.Pod {
65+
func injectNginxSDK(_ logr.Logger, nginxSpec v1alpha1.Nginx, pod corev1.Pod, useLabelsForResourceAttributes bool, index int, otlpEndpoint string, resourceMap map[string]string) corev1.Pod {
6666

6767
// caller checks if there is at least one container
6868
container := &pod.Spec.Containers[index]
@@ -217,7 +217,7 @@ mv ${NGINX_AGENT_CONF_DIR_FULL}/opentelemetry_agent.conf ${NGINX_AGENT_CONF_DIR
217217
Env: []corev1.EnvVar{
218218
{
219219
Name: nginxAttributesEnvVar,
220-
Value: getNginxOtelConfig(pod, nginxSpec, index, otlpEndpoint, resourceMap),
220+
Value: getNginxOtelConfig(pod, useLabelsForResourceAttributes, nginxSpec, index, otlpEndpoint, resourceMap),
221221
},
222222
{
223223
Name: "OTEL_NGINX_I13N_SCRIPT",
@@ -277,12 +277,12 @@ func isNginxInitContainerMissing(pod corev1.Pod, containerName string) bool {
277277

278278
// Calculate Nginx agent configuration file based on attributes provided by the injection rules
279279
// and by the pod values.
280-
func getNginxOtelConfig(pod corev1.Pod, nginxSpec v1alpha1.Nginx, index int, otelEndpoint string, resourceMap map[string]string) string {
280+
func getNginxOtelConfig(pod corev1.Pod, useLabelsForResourceAttributes bool, nginxSpec v1alpha1.Nginx, index int, otelEndpoint string, resourceMap map[string]string) string {
281281

282282
if otelEndpoint == "" {
283283
otelEndpoint = "http://localhost:4317/"
284284
}
285-
serviceName := chooseServiceName(pod, resourceMap, index)
285+
serviceName := chooseServiceName(pod, useLabelsForResourceAttributes, resourceMap, index)
286286
serviceNamespace := pod.GetNamespace()
287287
if len(serviceNamespace) == 0 {
288288
serviceNamespace = resourceMap[string(semconv.K8SNamespaceNameKey)]

pkg/instrumentation/nginx_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func TestInjectNginxSDK(t *testing.T) {
477477

478478
for _, test := range tests {
479479
t.Run(test.name, func(t *testing.T) {
480-
pod := injectNginxSDK(logr.Discard(), test.Nginx, test.pod, 0, "http://otlp-endpoint:4317", resourceMap)
480+
pod := injectNginxSDK(logr.Discard(), test.Nginx, test.pod, false, 0, "http://otlp-endpoint:4317", resourceMap)
481481
assert.Equal(t, test.expected, pod)
482482
})
483483
}
@@ -600,7 +600,7 @@ func TestInjectNginxUnknownNamespace(t *testing.T) {
600600

601601
for _, test := range tests {
602602
t.Run(test.name, func(t *testing.T) {
603-
pod := injectNginxSDK(logr.Discard(), test.Nginx, test.pod, 0, "http://otlp-endpoint:4317", resourceMap)
603+
pod := injectNginxSDK(logr.Discard(), test.Nginx, test.pod, false, 0, "http://otlp-endpoint:4317", resourceMap)
604604
assert.Equal(t, test.expected, pod)
605605
})
606606
}

0 commit comments

Comments
 (0)