Skip to content

Commit 7e6e3e0

Browse files
authored
Added service.instance.id into resource env (#2259)
* Added service.instance.id into resource env Signed-off-by: Yuri Sa <[email protected]> * Changed ServiceInstanceID following the RFC4122 Signed-off-by: Yuri Sa <[email protected]> * Changed ServiceInstanceID following the RFC4122 Signed-off-by: Yuri Sa <[email protected]> * Added semantic conventions Signed-off-by: Yuri Sa <[email protected]> --------- Signed-off-by: Yuri Sa <[email protected]>
1 parent c0f8e03 commit 7e6e3e0

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
5+
component: Operator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Added the service.instance.id as the pod.UID into the traces resource Env.
9+
10+
# One or more tracking issues related to the change
11+
issues: [1921]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext: ""

pkg/instrumentation/podmutator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,7 @@ func TestMutatePod(t *testing.T) {
29912991
},
29922992
{
29932993
Name: "OTEL_RESOURCE_ATTRIBUTES",
2994-
Value: "k8s.container.name=nginx,k8s.namespace.name=req-namespace,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=my-nginx-6c44bcbdd",
2994+
Value: "k8s.container.name=nginx,k8s.namespace.name=req-namespace,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=my-nginx-6c44bcbdd,service.instance.id=req-namespace.my-nginx-6c44bcbdd.nginx",
29952995
},
29962996
},
29972997
},

pkg/instrumentation/sdk.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
"unsafe"
2424

2525
"github.com/go-logr/logr"
26+
27+
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
28+
"github.com/open-telemetry/opentelemetry-operator/pkg/constants"
29+
2630
"go.opentelemetry.io/otel/attribute"
2731
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
2832
appsv1 "k8s.io/api/apps/v1"
@@ -33,9 +37,6 @@ import (
3337
"k8s.io/apimachinery/pkg/util/wait"
3438
"k8s.io/client-go/util/retry"
3539
"sigs.k8s.io/controller-runtime/pkg/client"
36-
37-
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
38-
"github.com/open-telemetry/opentelemetry-operator/pkg/constants"
3940
)
4041

4142
const (
@@ -401,6 +402,17 @@ func chooseServiceVersion(pod corev1.Pod, index int) string {
401402
return tag
402403
}
403404

405+
// creates the service.instance.id following the semantic defined by
406+
// https://github.com/open-telemetry/semantic-conventions/pull/312.
407+
func createServiceInstanceId(namespaceName, podName, containerName string) string {
408+
var serviceInstanceId string
409+
if namespaceName != "" && podName != "" && containerName != "" {
410+
resNames := []string{namespaceName, podName, containerName}
411+
serviceInstanceId = strings.Join(resNames, ".")
412+
}
413+
return serviceInstanceId
414+
}
415+
404416
// createResourceMap creates resource attribute map.
405417
// User defined attributes (in explicitly set env var) have higher precedence.
406418
func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.Instrumentation, ns corev1.Namespace, pod corev1.Pod, index int) map[string]string {
@@ -424,7 +436,6 @@ func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.I
424436
res[k] = v
425437
}
426438
}
427-
428439
k8sResources := map[attribute.Key]string{}
429440
k8sResources[semconv.K8SNamespaceNameKey] = ns.Name
430441
k8sResources[semconv.K8SContainerNameKey] = pod.Spec.Containers[index].Name
@@ -433,6 +444,7 @@ func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.I
433444
k8sResources[semconv.K8SPodNameKey] = pod.Name
434445
k8sResources[semconv.K8SPodUIDKey] = string(pod.UID)
435446
k8sResources[semconv.K8SNodeNameKey] = pod.Spec.NodeName
447+
k8sResources[semconv.ServiceInstanceIDKey] = createServiceInstanceId(ns.Name, pod.Name, pod.Spec.Containers[index].Name)
436448
i.addParentResourceLabels(ctx, otelinst.Spec.Resource.AddK8sUIDAttributes, ns, pod.ObjectMeta, k8sResources)
437449
for k, v := range k8sResources {
438450
if !existingRes[string(k)] && v != "" {

pkg/instrumentation/sdk_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestSDKInjection(t *testing.T) {
201201
},
202202
{
203203
Name: "OTEL_RESOURCE_ATTRIBUTES",
204-
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,service.version=latest",
204+
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,service.instance.id=project1.app.application-name,service.version=latest",
205205
},
206206
},
207207
},
@@ -369,7 +369,7 @@ func TestSDKInjection(t *testing.T) {
369369
},
370370
{
371371
Name: "OTEL_RESOURCE_ATTRIBUTES",
372-
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,service.version=latest",
372+
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,service.instance.id=project1.app.application-name,service.version=latest",
373373
},
374374
},
375375
},

0 commit comments

Comments
 (0)