Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix K8s attributes values in OTEL_RESOURCE_ATTRIBUTES env var #864

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
mat-rumian marked this conversation as resolved.
Show resolved Hide resolved
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