Skip to content

Commit

Permalink
Fix K8s attributes values in OTEL_RESOURCE_ATTRIBUTES env var (#864)
Browse files Browse the repository at this point in the history
* chore(env-vars): move otel resource attributes to last env on the list

* chore(sdk-test): move otel resource attributes to last env on the list

* chore(e2e-test): update env order

* chore(env-vars): add condition, update comment
  • Loading branch information
mat-rumian authored May 13, 2022
1 parent 9a17de6 commit a02d99c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
19 changes: 19 additions & 0 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
24 changes: 12 additions & 12 deletions pkg/instrumentation/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
},
},
},
Expand Down Expand Up @@ -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",
Expand All @@ -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,",
},
},
},
},
Expand All @@ -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",
Expand All @@ -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",
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/instrumentation-java/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/instrumentation-nodejs/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/instrumentation-python/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a02d99c

Please sign in to comment.