Skip to content

Commit

Permalink
Do not set exporter endpoint if it is empty (#587)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <[email protected]>
  • Loading branch information
pavolloffay authored Dec 2, 2021
1 parent be1df56 commit a94a138
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions pkg/instrumentation/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a94a138

Please sign in to comment.