Skip to content

Commit

Permalink
Added service.instance.id into resource env (#2259)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
yuriolisa authored Nov 14, 2023
1 parent c0f8e03 commit 7e6e3e0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
16 changes: 16 additions & 0 deletions .chloggen/new-service-instance-id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: Operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added the service.instance.id as the pod.UID into the traces resource Env.

# One or more tracking issues related to the change
issues: [1921]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: ""
2 changes: 1 addition & 1 deletion pkg/instrumentation/podmutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2991,7 +2991,7 @@ func TestMutatePod(t *testing.T) {
},
{
Name: "OTEL_RESOURCE_ATTRIBUTES",
Value: "k8s.container.name=nginx,k8s.namespace.name=req-namespace,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=my-nginx-6c44bcbdd",
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",
},
},
},
Expand Down
20 changes: 16 additions & 4 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"unsafe"

"github.com/go-logr/logr"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/pkg/constants"

"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -33,9 +37,6 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/pkg/constants"
)

const (
Expand Down Expand Up @@ -401,6 +402,17 @@ func chooseServiceVersion(pod corev1.Pod, index int) string {
return tag
}

// creates the service.instance.id following the semantic defined by
// https://github.com/open-telemetry/semantic-conventions/pull/312.
func createServiceInstanceId(namespaceName, podName, containerName string) string {
var serviceInstanceId string
if namespaceName != "" && podName != "" && containerName != "" {
resNames := []string{namespaceName, podName, containerName}
serviceInstanceId = strings.Join(resNames, ".")
}
return serviceInstanceId
}

// createResourceMap creates resource attribute map.
// User defined attributes (in explicitly set env var) have higher precedence.
func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.Instrumentation, ns corev1.Namespace, pod corev1.Pod, index int) map[string]string {
Expand All @@ -424,7 +436,6 @@ func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.I
res[k] = v
}
}

k8sResources := map[attribute.Key]string{}
k8sResources[semconv.K8SNamespaceNameKey] = ns.Name
k8sResources[semconv.K8SContainerNameKey] = pod.Spec.Containers[index].Name
Expand All @@ -433,6 +444,7 @@ func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.I
k8sResources[semconv.K8SPodNameKey] = pod.Name
k8sResources[semconv.K8SPodUIDKey] = string(pod.UID)
k8sResources[semconv.K8SNodeNameKey] = pod.Spec.NodeName
k8sResources[semconv.ServiceInstanceIDKey] = createServiceInstanceId(ns.Name, pod.Name, pod.Spec.Containers[index].Name)
i.addParentResourceLabels(ctx, otelinst.Spec.Resource.AddK8sUIDAttributes, ns, pod.ObjectMeta, k8sResources)
for k, v := range k8sResources {
if !existingRes[string(k)] && v != "" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/instrumentation/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ 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,service.version=latest",
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",
},
},
},
Expand Down Expand Up @@ -369,7 +369,7 @@ func TestSDKInjection(t *testing.T) {
},
{
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,service.version=latest",
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",
},
},
},
Expand Down

0 comments on commit 7e6e3e0

Please sign in to comment.