From a94a13888165ec688a5f83d4e444e9fbc934fca2 Mon Sep 17 00:00:00 2001 From: Pavol Loffay Date: Thu, 2 Dec 2021 19:50:28 +0100 Subject: [PATCH] Do not set exporter endpoint if it is empty (#587) Signed-off-by: Pavol Loffay --- pkg/instrumentation/sdk.go | 14 ++++--- pkg/instrumentation/sdk_test.go | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/pkg/instrumentation/sdk.go b/pkg/instrumentation/sdk.go index 9668b91307..ad72757411 100644 --- a/pkg/instrumentation/sdk.go +++ b/pkg/instrumentation/sdk.go @@ -94,12 +94,14 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph Value: chooseServiceName(pod, resourceMap), }) } - idx = getIndexOfEnv(container.Env, envOTELExporterOTLPEndpoint) - if idx == -1 { - container.Env = append(container.Env, corev1.EnvVar{ - Name: envOTELExporterOTLPEndpoint, - Value: otelinst.Spec.Endpoint, - }) + if otelinst.Spec.Exporter.Endpoint != "" { + idx = getIndexOfEnv(container.Env, envOTELExporterOTLPEndpoint) + if idx == -1 { + container.Env = append(container.Env, corev1.EnvVar{ + Name: envOTELExporterOTLPEndpoint, + Value: otelinst.Spec.Endpoint, + }) + } } // Some attributes might be empty, we should get them via k8s downward API diff --git a/pkg/instrumentation/sdk_test.go b/pkg/instrumentation/sdk_test.go index 6f6c089d70..aca097318a 100644 --- a/pkg/instrumentation/sdk_test.go +++ b/pkg/instrumentation/sdk_test.go @@ -289,6 +289,74 @@ func TestSDKInjection(t *testing.T) { }, }, }, + { + name: "Empty instrumentation spec", + inst: v1alpha1.Instrumentation{ + Spec: v1alpha1.InstrumentationSpec{}, + }, + pod: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "project1", + Name: "app", + UID: "pod-uid", + OwnerReferences: []metav1.OwnerReference{ + { + Kind: "ReplicaSet", + Name: "my-replicaset", + UID: "rsuid", + APIVersion: "apps/v1", + }, + }, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "application-name", + }, + }, + }, + }, + expected: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "project1", + Name: "app", + UID: "pod-uid", + OwnerReferences: []metav1.OwnerReference{ + { + Kind: "ReplicaSet", + Name: "my-replicaset", + UID: "rsuid", + APIVersion: "apps/v1", + }, + }, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "application-name", + Env: []corev1.EnvVar{ + { + Name: "OTEL_SERVICE_NAME", + Value: "my-deployment", + }, + { + Name: "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME", + ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{ + FieldPath: "spec.nodeName", + }, + }, + }, + { + Name: "OTEL_RESOURCE_ATTRIBUTES", + Value: "k8s.container.name=application-name,k8s.deployment.name=my-deployment,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", + }, + }, + }, + }, + }, + }, + }, } for _, test := range tests {