From 137aa57059c2ce8c7083d53eacb619ae27bf155c Mon Sep 17 00:00:00 2001 From: Horiodino Date: Tue, 4 Jun 2024 14:41:28 +0530 Subject: [PATCH 1/6] using opentelemetry.io* namespace for extracing resource Signed-off-by: Horiodino --- pkg/constants/env.go | 11 ++-- pkg/instrumentation/sdk.go | 9 +++ pkg/instrumentation/sdk_test.go | 106 ++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 5 deletions(-) diff --git a/pkg/constants/env.go b/pkg/constants/env.go index ed6d5e0f2e..244a417667 100644 --- a/pkg/constants/env.go +++ b/pkg/constants/env.go @@ -31,11 +31,12 @@ const ( AnnotationDefaultAutoInstrumentationApacheHttpd = InstrumentationPrefix + "default-auto-instrumentation-apache-httpd-image" AnnotationDefaultAutoInstrumentationNginx = InstrumentationPrefix + "default-auto-instrumentation-nginx-image" - EnvPodName = "OTEL_RESOURCE_ATTRIBUTES_POD_NAME" - EnvPodUID = "OTEL_RESOURCE_ATTRIBUTES_POD_UID" - EnvPodIP = "OTEL_POD_IP" - EnvNodeName = "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME" - EnvNodeIP = "OTEL_NODE_IP" + EnvPodName = "OTEL_RESOURCE_ATTRIBUTES_POD_NAME" + EnvPodUID = "OTEL_RESOURCE_ATTRIBUTES_POD_UID" + EnvPodIP = "OTEL_POD_IP" + EnvNodeName = "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME" + EnvNodeIP = "OTEL_NODE_IP" + ReservedNamespace = "resource.opentelemetry.io/" FlagCRMetrics = "enable-cr-metrics" FlagApacheHttpd = "enable-apache-httpd-instrumentation" diff --git a/pkg/instrumentation/sdk.go b/pkg/instrumentation/sdk.go index 3bda97aa38..ca81adf0e0 100644 --- a/pkg/instrumentation/sdk.go +++ b/pkg/instrumentation/sdk.go @@ -479,6 +479,15 @@ func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.I res[string(k)] = v } } + + for k, v := range pod.GetAnnotations() { + if strings.HasPrefix(k, constants.ReservedNamespace) { + key := strings.TrimSpace(strings.TrimPrefix(k, constants.ReservedNamespace)) + if _, ok := res[key]; !ok { + res[key] = v + } + } + } return res } diff --git a/pkg/instrumentation/sdk_test.go b/pkg/instrumentation/sdk_test.go index 9c8a6f3e1b..b93b15af47 100644 --- a/pkg/instrumentation/sdk_test.go +++ b/pkg/instrumentation/sdk_test.go @@ -484,6 +484,112 @@ func TestSDKInjection(t *testing.T) { }, }, }, + { + name: "Resource attribute propagate", + inst: v1alpha1.Instrumentation{ + Spec: v1alpha1.InstrumentationSpec{ + Exporter: v1alpha1.Exporter{ + Endpoint: "https://collector:4317", + }, + Resource: v1alpha1.Resource{ + Attributes: map[string]string{ + "fromcr": "val", + }, + }, + Propagators: []v1alpha1.Propagator{"jaeger"}, + Sampler: v1alpha1.Sampler{ + Type: "parentbased_traceidratio", + Argument: "0.25", + }, + }, + }, + pod: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "resource.opentelemetry.io/fromtest": "val", + "resource.opentelemetry.io/foo": "test", + }, + Namespace: "project1", + Name: "app", + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Image: "app:latest", + Env: []corev1.EnvVar{ + { + Name: "OTEL_SERVICE_NAME", + Value: "explicitly_set", + }, + { + Name: "OTEL_EXPORTER_OTLP_ENDPOINT", + Value: "explicitly_set", + }, + { + Name: "OTEL_PROPAGATORS", + Value: "b3", + }, + { + Name: "OTEL_TRACES_SAMPLER", + Value: "always_on", + }, + { + Name: "OTEL_RESOURCE_ATTRIBUTES", + Value: "foo=bar,k8s.container.name=other,service.version=explicitly_set,", + }, + }, + }, + }, + }, + }, + expected: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "project1", + Name: "app", + Annotations: map[string]string{ + "resource.opentelemetry.io/fromtest": "val", + "resource.opentelemetry.io/foo": "test", + }, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Image: "app:latest", + Env: []corev1.EnvVar{ + { + Name: "OTEL_SERVICE_NAME", + Value: "explicitly_set", + }, + { + Name: "OTEL_EXPORTER_OTLP_ENDPOINT", + Value: "explicitly_set", + }, + { + Name: "OTEL_PROPAGATORS", + Value: "b3", + }, + { + Name: "OTEL_TRACES_SAMPLER", + Value: "always_on", + }, + { + Name: "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME", + ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{ + FieldPath: "spec.nodeName", + }, + }, + }, + { + Name: "OTEL_RESOURCE_ATTRIBUTES", + Value: "foo=bar,k8s.container.name=other,service.version=explicitly_set,foo=test,fromcr=val,fromtest=val,k8s.namespace.name=project1,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=app", + }, + }, + }, + }, + }, + }, + }, } for _, test := range tests { From 5273b69fc875a63eb915826dd8dc586a28c0e60e Mon Sep 17 00:00:00 2001 From: Horiodino Date: Fri, 7 Jun 2024 15:38:40 +0530 Subject: [PATCH 2/6] refactor ReservedNamespace to OtelAnnotationNamespace Signed-off-by: Horiodino --- pkg/constants/env.go | 12 ++++++------ pkg/instrumentation/sdk.go | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/constants/env.go b/pkg/constants/env.go index 244a417667..45d0a82982 100644 --- a/pkg/constants/env.go +++ b/pkg/constants/env.go @@ -31,12 +31,12 @@ const ( AnnotationDefaultAutoInstrumentationApacheHttpd = InstrumentationPrefix + "default-auto-instrumentation-apache-httpd-image" AnnotationDefaultAutoInstrumentationNginx = InstrumentationPrefix + "default-auto-instrumentation-nginx-image" - EnvPodName = "OTEL_RESOURCE_ATTRIBUTES_POD_NAME" - EnvPodUID = "OTEL_RESOURCE_ATTRIBUTES_POD_UID" - EnvPodIP = "OTEL_POD_IP" - EnvNodeName = "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME" - EnvNodeIP = "OTEL_NODE_IP" - ReservedNamespace = "resource.opentelemetry.io/" + EnvPodName = "OTEL_RESOURCE_ATTRIBUTES_POD_NAME" + EnvPodUID = "OTEL_RESOURCE_ATTRIBUTES_POD_UID" + EnvPodIP = "OTEL_POD_IP" + EnvNodeName = "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME" + EnvNodeIP = "OTEL_NODE_IP" + OtelAnnotationNamespace = "resource.opentelemetry.io/" FlagCRMetrics = "enable-cr-metrics" FlagApacheHttpd = "enable-apache-httpd-instrumentation" diff --git a/pkg/instrumentation/sdk.go b/pkg/instrumentation/sdk.go index ca81adf0e0..ed6f70663d 100644 --- a/pkg/instrumentation/sdk.go +++ b/pkg/instrumentation/sdk.go @@ -481,8 +481,8 @@ func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.I } for k, v := range pod.GetAnnotations() { - if strings.HasPrefix(k, constants.ReservedNamespace) { - key := strings.TrimSpace(strings.TrimPrefix(k, constants.ReservedNamespace)) + if strings.HasPrefix(k, constants.OtelAnnotationNamespace) { + key := strings.TrimSpace(strings.TrimPrefix(k, constants.OtelAnnotationNamespace)) if _, ok := res[key]; !ok { res[key] = v } From c69bf0e4d0e83a6d098ae2b2f128b2cac0ecf8e1 Mon Sep 17 00:00:00 2001 From: Horiodino Date: Fri, 7 Jun 2024 22:26:03 +0530 Subject: [PATCH 3/6] added changelog Signed-off-by: Horiodino --- .chloggen/extract-resources-ns.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100755 .chloggen/extract-resources-ns.yaml diff --git a/.chloggen/extract-resources-ns.yaml b/.chloggen/extract-resources-ns.yaml new file mode 100755 index 0000000000..aa5515c57a --- /dev/null +++ b/.chloggen/extract-resources-ns.yaml @@ -0,0 +1,13 @@ +change_type: enhancement + +component: instrumentation + +note: "introduced ability to set Otel resource attributes based on annotations for instrumentation" + +issues: + - 2181 + + +subtext: | + + resource.opentelemetry.io/your-key: "your-value" From 166b3576c6876998aa776f79395ff89f2f2d24fb Mon Sep 17 00:00:00 2001 From: Horiodino Date: Wed, 26 Jun 2024 12:55:02 +0530 Subject: [PATCH 4/6] Updated README: using opentelemetry.io* namespace for extracting resource Signed-off-by: Horiodino --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 45d86dbb69..e56ad543fa 100644 --- a/README.md +++ b/README.md @@ -712,6 +712,25 @@ spec: EOF ``` +### using opentelemetry.io* namespace for extracing resource + +This example shows a pod configuration with OpenTelemetry annotations using the `resource.opentelemetry.io/` prefix. These annotations can be used to extract resource information for OpenTelemetry instrumentation. + +```yaml +apiVersion: v1 +kind: Pod +metadata: + name: example-pod + annotations: + resource.opentelemetry.io/service.name: "my-service" + resource.opentelemetry.io/service.version: "1.0.0" + resource.opentelemetry.io/environment: "production" +spec: + containers: + - name: main-container + image: your-image:tag + ``` + ## Compatibility matrix ### OpenTelemetry Operator vs. OpenTelemetry Collector From 304b8d21acc612bcb7690c04a86f8b231ae06330 Mon Sep 17 00:00:00 2001 From: Praful Khanduri <99384392+Horiodino@users.noreply.github.com> Date: Wed, 26 Jun 2024 18:19:15 +0530 Subject: [PATCH 5/6] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mikołaj Świątek --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e56ad543fa..da88c37879 100644 --- a/README.md +++ b/README.md @@ -714,7 +714,7 @@ EOF ### using opentelemetry.io* namespace for extracing resource -This example shows a pod configuration with OpenTelemetry annotations using the `resource.opentelemetry.io/` prefix. These annotations can be used to extract resource information for OpenTelemetry instrumentation. +This example shows a pod configuration with OpenTelemetry annotations using the `resource.opentelemetry.io/` prefix. These annotations can be used to add resource attributes to data produced by OpenTelemetry instrumentation. ```yaml apiVersion: v1 From 6fc11770a33fcdc95ccae0fdc0ab2c09d61f1a99 Mon Sep 17 00:00:00 2001 From: Praful Khanduri <99384392+Horiodino@users.noreply.github.com> Date: Wed, 26 Jun 2024 18:19:25 +0530 Subject: [PATCH 6/6] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mikołaj Świątek --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index da88c37879..f535097baf 100644 --- a/README.md +++ b/README.md @@ -712,7 +712,7 @@ spec: EOF ``` -### using opentelemetry.io* namespace for extracing resource +### Setting instrumentation resource attributes via namespace annotations This example shows a pod configuration with OpenTelemetry annotations using the `resource.opentelemetry.io/` prefix. These annotations can be used to add resource attributes to data produced by OpenTelemetry instrumentation.