Skip to content

Commit e29c095

Browse files
committed
enhancement: added recognizeUserDefinedValues recognizes user defined values assigned with opentelemetry.io/ prefix
1 parent 1e49ab0 commit e29c095

File tree

1 file changed

+37
-73
lines changed

1 file changed

+37
-73
lines changed

pkg/instrumentation/sdk.go

+37-73
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"unsafe"
2424

2525
"github.com/go-logr/logr"
26+
2627
"go.opentelemetry.io/otel/attribute"
2728
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
2829
appsv1 "k8s.io/api/apps/v1"
@@ -401,91 +402,54 @@ func chooseServiceVersion(pod corev1.Pod, index int) string {
401402
return tag
402403
}
403404

405+
// create/reserve namespace
404406

405-
/*
406-
Desire for a Reserved Namespace:
407-
There's a desire to reserve a specific namespace, in this case, .opentelemetry.io/, for special instructions or configurations related to OpenTelemetry.
408-
409-
User-Defined Values in the Namespace:
410-
Users are expected to set values within this reserved namespace, such as 'resource.opentelemetry.io/service.name' or 'resource.opentelemetry.io/my.attribute: some-value' on a pod or deployment.
411-
412-
Recognition by OpenTelemetry Operator:
413-
The OpenTelemetry Operator should be designed to recognize and understand these user-defined values within the reserved namespace.
414-
415-
Purpose of Recognition:
416-
Upon recognizing these values, the OpenTelemetry Operator is expected to perform certain actions or configurations based on the provided instructions.
417-
For example, if a user sets 'resource.opentelemetry.io/service.name' on a pod, the operator should understand that this is an instruction to set the service name for that specific pod.
418-
419-
Dynamic Configuration Based on Annotations:
420-
Annotations on Kubernetes resources (e.g., pods or deployments) are used as a means of providing configuration instructions.
421-
If an annotation like 'resource.opentelemetry.io/my.attribute: some-value' is set, the OpenTelemetry Operator should interpret this as an instruction to set a specific attribute (e.g., my.attribute) with a particular value (some-value) for the associated resource.
422-
423-
424-
425-
TODO
426-
according to the issue the end result should be something like this:
427-
for example, if I was to set 'resource.opentelemetry.io/service.name' on a pod, then use instrumentation CRDs to add instrumentation, the value of that annotation would be propagated to the instrumentation.
428-
429-
the above example is doing that in the following way: if a user sets 'resource.opentelemetry.io/service.name' on a pod, the operator should understand that this is an instruction to set the service name for that specific pod.
430-
then the operator should interpret this as an instruction to set a specific attribute (e.g., my.attribute) with a particular value (some-value) for the associated resource.
431-
and finally the operator should propagate the value of that annotation to the instrumentation.
432-
433-
in simple words the operator should understand that this is an instruction to set the service name for that specific pod and then propagate the value of that annotation to the instrumentation.
434-
propogate means that the operator should interpret this as an instruction to set a specific attribute (e.g., my.attribute) with a particular value (some-value) for the associated resource.
435-
436-
*/
437-
438-
/*
439-
Add a new namespace as a reserved namespace (e.g., *.opentelemetry.io/*).
440-
Add a new function to the operator to recognize and understand user-defined values within the reserved namespace.
441-
Add a new function to the operator to perform certain actions or configurations based on the provided instructions.
442-
Add a new function to the operator to set the service name for that specific pod.
443-
Add a new function to the operator to set a specific attribute (e.g., my.attribute) with a particular value (some-value) for the associated resource.
444-
Add a new function to the operator to interpret this as an instruction to set a specific attribute (e.g., my.attribute) with a particular value (some-value) for the associated resource.
445-
In the createResourceMap function, add code to parse the reserved namespace and set the associated values as resource attributes.
446-
*/
447-
448-
// create/reserve namespace
449-
450-
func (i *sdkInjector) createReservedNamespace(ctx context.Context) (corev1.Namespace, error) {
451-
ns := corev1.Namespace{}
452-
ns.Name = "opentelemetry.io"
453-
err := i.client.Create(ctx, &ns)
407+
func (i *sdkInjector) createReservedNamespace(ctx context.Context) (bool, error) {
408+
err := i.client.Get(ctx, types.NamespacedName{Name: constants.ReservedNamespace}, &corev1.Namespace{})
454409
if err != nil {
455-
return ns, err
410+
if apierrors.IsNotFound(err) {
411+
ns := corev1.Namespace{
412+
ObjectMeta: metav1.ObjectMeta{
413+
Name: constants.ReservedNamespace,
414+
},
415+
}
416+
err = i.client.Create(ctx, &ns)
417+
if err != nil {
418+
return true, err
419+
}
420+
if err != nil {
421+
return false, err
422+
}
423+
}
456424
}
457-
return ns, nil
425+
return true, nil
458426
}
459427

460-
// Add a new function to the operator to recognize and understand user-defined values within the reserved namespace.
461-
462-
func (i *sdkInjector) recognizeUserDefinedValues(ctx context.Context, ns corev1.Namespace) (corev1.Namespace, error) {
463-
// what we need to do here is to recognize and understand user-defined values within the reserved namespace.
464-
// what kind of user-defined values? the ones that are set on a pod or deployment.
465-
// how do we do that? we need to use annotations on Kubernetes resources (e.g., pods or deployments) as a means of providing configuration instructions.
466-
// what kind of annotations? the ones that are set on a pod or deployment.
467-
// we will use recognise if the user set the value as open-telemetry.io/attribute: some-value
468-
// then we will use the value of that annotation to set a specific attribute (e.g., my.attribute) with a particular value (some-value) for the associated resource.
428+
// recognizeUserDefinedValues recognizes user defined values assigned with opentelemetry.io/ prefix
429+
func (i *sdkInjector) recognizeUserDefinedValues(ctx context.Context, ns corev1.Namespace, pod corev1.Pod, deployment appsv1.Deployment, index int) (corev1.Namespace, error) {
469430

470-
// check for the namespace is created or not if the namespace is not created then create it else throw an error
431+
existingRes := map[string]bool{}
432+
existingResourceEnvIdx := getIndexOfEnv(pod.Spec.Containers[index].Env, constants.EnvOTELResourceAttrs)
471433

472-
err := i.client.Get(ctx, types.NamespacedName{Name: ns.Name}, &ns)
473-
if err != nil {
474-
return ns, err
434+
if existingResourceEnvIdx > -1 {
435+
existingResArr := strings.Split(pod.Spec.Containers[index].Env[existingResourceEnvIdx].Value, ",")
436+
for _, kv := range existingResArr {
437+
keyValueArr := strings.Split(strings.TrimSpace(kv), "=")
438+
if len(keyValueArr) != 2 {
439+
continue
440+
}
441+
existingRes[keyValueArr[0]] = true
442+
}
475443
}
476444

477-
// Get the all pods in the givennamespace
478-
479-
480-
// TODO - use the annotations to recognize and understand user-defined values within the reserved namespace
481-
// TODO - use the annotations to set the specific attribute with a particular value for the associated resource
482-
445+
for key, value := range existingRes {
446+
if strings.HasPrefix(key, "opentelemetry.io/") {
447+
// now if it has the prefix opentelemetry.io/ then we need to check if the attribute name is already present in the resource attributes
448+
}
449+
}
483450
return ns, nil
484-
485451
}
486452

487-
488-
489453
// createResourceMap creates resource attribute map.
490454
// User defined attributes (in explicitly set env var) have higher precedence.
491455
func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.Instrumentation, ns corev1.Namespace, pod corev1.Pod, index int) map[string]string {

0 commit comments

Comments
 (0)