diff --git a/pkg/instrumentation/sdk.go b/pkg/instrumentation/sdk.go index a5bf12e656..65faf670dd 100644 --- a/pkg/instrumentation/sdk.go +++ b/pkg/instrumentation/sdk.go @@ -186,6 +186,15 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph } } + // 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 + return pod } @@ -321,3 +330,13 @@ func getIndexOfEnv(envs []corev1.EnvVar, name string) int { } return -1 } + +func moveEnvToListEnd(envs []corev1.EnvVar, idx int) []corev1.EnvVar { + if idx >= 0 && idx < len(envs) { + envToMove := envs[idx] + envs = append(envs[:idx], envs[idx+1:]...) + envs = append(envs, envToMove) + } + + return envs +} 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", + }, }, }, }, 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