From e63ec44c6b3dbaaf461e3a8f89fe7fbd6fb0af42 Mon Sep 17 00:00:00 2001 From: Mateusz Rumian Date: Wed, 11 May 2022 16:57:21 +0200 Subject: [PATCH 1/4] chore(env-vars): move otel resource attributes to last env on the list --- pkg/instrumentation/sdk.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/instrumentation/sdk.go b/pkg/instrumentation/sdk.go index a5bf12e656..956a06b1bc 100644 --- a/pkg/instrumentation/sdk.go +++ b/pkg/instrumentation/sdk.go @@ -186,6 +186,13 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph } } + // Move OTEL_RESOURCE_ATTRIBUTES to last position. + // All used env vars as value have to be configured before + // to avoid incorrect attributes values. + idx = getIndexOfEnv(container.Env, constants.EnvOTELResourceAttrs) + envs := moveEnvToListEnd(container.Env, idx) + container.Env = envs + return pod } @@ -321,3 +328,10 @@ func getIndexOfEnv(envs []corev1.EnvVar, name string) int { } return -1 } + +func moveEnvToListEnd(envs []corev1.EnvVar, idx int) []corev1.EnvVar { + envToMove := envs[idx] + envs = append(envs[:idx], envs[idx+1:]...) + envs = append(envs, envToMove) + return envs +} From 62d28b502757c872af55a5189caafc9ae2498d96 Mon Sep 17 00:00:00 2001 From: Mateusz Rumian Date: Wed, 11 May 2022 16:59:14 +0200 Subject: [PATCH 2/4] chore(sdk-test): move otel resource attributes to last env on the list --- pkg/instrumentation/sdk_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/instrumentation/sdk_test.go b/pkg/instrumentation/sdk_test.go index 4ee0e63c0f..10030b6099 100644 --- a/pkg/instrumentation/sdk_test.go +++ b/pkg/instrumentation/sdk_test.go @@ -171,10 +171,6 @@ func TestSDKInjection(t *testing.T) { }, }, }, - { - Name: "OTEL_RESOURCE_ATTRIBUTES", - Value: "k8s.container.name=application-name,k8s.deployment.name=my-deployment,k8s.deployment.uid=depuid,k8s.namespace.name=project1,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=app,k8s.pod.uid=pod-uid,k8s.replicaset.name=my-replicaset,k8s.replicaset.uid=rsuid", - }, { Name: "OTEL_PROPAGATORS", Value: "b3,jaeger", @@ -187,6 +183,10 @@ func TestSDKInjection(t *testing.T) { Name: "OTEL_TRACES_SAMPLER_ARG", Value: "0.25", }, + { + Name: "OTEL_RESOURCE_ATTRIBUTES", + Value: "k8s.container.name=application-name,k8s.deployment.name=my-deployment,k8s.deployment.uid=depuid,k8s.namespace.name=project1,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=app,k8s.pod.uid=pod-uid,k8s.replicaset.name=my-replicaset,k8s.replicaset.uid=rsuid", + }, }, }, }, @@ -229,10 +229,6 @@ func TestSDKInjection(t *testing.T) { Name: "OTEL_EXPORTER_OTLP_ENDPOINT", Value: "explicitly_set", }, - { - Name: "OTEL_RESOURCE_ATTRIBUTES", - Value: "foo=bar,k8s.container.name=other,", - }, { Name: "OTEL_PROPAGATORS", Value: "b3", @@ -241,6 +237,10 @@ func TestSDKInjection(t *testing.T) { Name: "OTEL_TRACES_SAMPLER", Value: "always_on", }, + { + Name: "OTEL_RESOURCE_ATTRIBUTES", + Value: "foo=bar,k8s.container.name=other,", + }, }, }, }, @@ -263,10 +263,6 @@ func TestSDKInjection(t *testing.T) { Name: "OTEL_EXPORTER_OTLP_ENDPOINT", Value: "explicitly_set", }, - { - Name: "OTEL_RESOURCE_ATTRIBUTES", - Value: "foo=bar,k8s.container.name=other,fromcr=val,k8s.namespace.name=project1,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=app", - }, { Name: "OTEL_PROPAGATORS", Value: "b3", @@ -283,6 +279,10 @@ func TestSDKInjection(t *testing.T) { }, }, }, + { + Name: "OTEL_RESOURCE_ATTRIBUTES", + Value: "foo=bar,k8s.container.name=other,fromcr=val,k8s.namespace.name=project1,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=app", + }, }, }, }, From 5bbf62252b1a3b7cc972dd19f0994f024b404222 Mon Sep 17 00:00:00 2001 From: Mateusz Rumian Date: Wed, 11 May 2022 21:20:55 +0200 Subject: [PATCH 3/4] chore(e2e-test): update env order --- tests/e2e/instrumentation-java/01-assert.yaml | 2 +- tests/e2e/instrumentation-nodejs/01-assert.yaml | 2 +- tests/e2e/instrumentation-python/01-assert.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/instrumentation-java/01-assert.yaml b/tests/e2e/instrumentation-java/01-assert.yaml index 526b21b881..d3505b046c 100644 --- a/tests/e2e/instrumentation-java/01-assert.yaml +++ b/tests/e2e/instrumentation-java/01-assert.yaml @@ -34,9 +34,9 @@ spec: value: my-deployment-with-sidecar - name: OTEL_RESOURCE_ATTRIBUTES_POD_NAME - name: OTEL_RESOURCE_ATTRIBUTES_NODE_NAME - - name: OTEL_RESOURCE_ATTRIBUTES - name: OTEL_PROPAGATORS value: jaeger,b3 + - name: OTEL_RESOURCE_ATTRIBUTES volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount - mountPath: /otel-auto-instrumentation diff --git a/tests/e2e/instrumentation-nodejs/01-assert.yaml b/tests/e2e/instrumentation-nodejs/01-assert.yaml index 37164a387c..3190895a59 100644 --- a/tests/e2e/instrumentation-nodejs/01-assert.yaml +++ b/tests/e2e/instrumentation-nodejs/01-assert.yaml @@ -30,9 +30,9 @@ spec: value: my-deployment-with-sidecar - name: OTEL_RESOURCE_ATTRIBUTES_POD_NAME - name: OTEL_RESOURCE_ATTRIBUTES_NODE_NAME - - name: OTEL_RESOURCE_ATTRIBUTES - name: OTEL_PROPAGATORS value: jaeger,b3 + - name: OTEL_RESOURCE_ATTRIBUTES volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount - mountPath: /otel-auto-instrumentation diff --git a/tests/e2e/instrumentation-python/01-assert.yaml b/tests/e2e/instrumentation-python/01-assert.yaml index a34d32d9a5..c0336b0e3f 100644 --- a/tests/e2e/instrumentation-python/01-assert.yaml +++ b/tests/e2e/instrumentation-python/01-assert.yaml @@ -30,9 +30,9 @@ spec: value: my-deployment-with-sidecar - name: OTEL_RESOURCE_ATTRIBUTES_POD_NAME - name: OTEL_RESOURCE_ATTRIBUTES_NODE_NAME - - name: OTEL_RESOURCE_ATTRIBUTES - name: OTEL_PROPAGATORS value: jaeger,b3 + - name: OTEL_RESOURCE_ATTRIBUTES volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount - mountPath: /otel-auto-instrumentation From f854d3bb4c6f1a621465b0281201a0412e909a1f Mon Sep 17 00:00:00 2001 From: Mateusz Rumian Date: Thu, 12 May 2022 13:57:05 +0200 Subject: [PATCH 4/4] chore(env-vars): add condition, update comment --- pkg/instrumentation/sdk.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/instrumentation/sdk.go b/pkg/instrumentation/sdk.go index 956a06b1bc..65faf670dd 100644 --- a/pkg/instrumentation/sdk.go +++ b/pkg/instrumentation/sdk.go @@ -186,9 +186,11 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph } } - // Move OTEL_RESOURCE_ATTRIBUTES to last position. - // All used env vars as value have to be configured before - // to avoid incorrect attributes values. + // Move OTEL_RESOURCE_ATTRIBUTES to last position on env list. + // When OTEL_RESOURCE_ATTRIBUTES environment variable uses other env vars + // as attributes value they have to be configured before. + // It is mandatory to set right order to avoid attributes with value + // pointing to the name of used environment variable instead of its value. idx = getIndexOfEnv(container.Env, constants.EnvOTELResourceAttrs) envs := moveEnvToListEnd(container.Env, idx) container.Env = envs @@ -330,8 +332,11 @@ func getIndexOfEnv(envs []corev1.EnvVar, name string) int { } func moveEnvToListEnd(envs []corev1.EnvVar, idx int) []corev1.EnvVar { - envToMove := envs[idx] - envs = append(envs[:idx], envs[idx+1:]...) - envs = append(envs, envToMove) + if idx >= 0 && idx < len(envs) { + envToMove := envs[idx] + envs = append(envs[:idx], envs[idx+1:]...) + envs = append(envs, envToMove) + } + return envs }