Skip to content

Commit 2f73582

Browse files
Horiodinoswiatekm
andauthored
using opentelemetry.io* namespace for extracing resource based on annotations from k8s Api #2330 (#3010)
* using opentelemetry.io* namespace for extracing resource Signed-off-by: Horiodino <[email protected]> * refactor ReservedNamespace to OtelAnnotationNamespace Signed-off-by: Horiodino <[email protected]> * added changelog Signed-off-by: Horiodino <[email protected]> * Updated README: using opentelemetry.io* namespace for extracting resource Signed-off-by: Horiodino <[email protected]> * Update README.md Co-authored-by: Mikołaj Świątek <[email protected]> * Update README.md Co-authored-by: Mikołaj Świątek <[email protected]> --------- Signed-off-by: Horiodino <[email protected]> Co-authored-by: Mikołaj Świątek <[email protected]>
1 parent c839f73 commit 2f73582

File tree

5 files changed

+153
-5
lines changed

5 files changed

+153
-5
lines changed

.chloggen/extract-resources-ns.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
change_type: enhancement
2+
3+
component: instrumentation
4+
5+
note: "introduced ability to set Otel resource attributes based on annotations for instrumentation"
6+
7+
issues:
8+
- 2181
9+
10+
11+
subtext: |
12+
13+
resource.opentelemetry.io/your-key: "your-value"

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,25 @@ spec:
716716
EOF
717717
```
718718

719+
### Setting instrumentation resource attributes via namespace annotations
720+
721+
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.
722+
723+
```yaml
724+
apiVersion: v1
725+
kind: Pod
726+
metadata:
727+
name: example-pod
728+
annotations:
729+
resource.opentelemetry.io/service.name: "my-service"
730+
resource.opentelemetry.io/service.version: "1.0.0"
731+
resource.opentelemetry.io/environment: "production"
732+
spec:
733+
containers:
734+
- name: main-container
735+
image: your-image:tag
736+
```
737+
719738
## Compatibility matrix
720739

721740
### OpenTelemetry Operator vs. OpenTelemetry Collector

pkg/constants/env.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ const (
3131
AnnotationDefaultAutoInstrumentationApacheHttpd = InstrumentationPrefix + "default-auto-instrumentation-apache-httpd-image"
3232
AnnotationDefaultAutoInstrumentationNginx = InstrumentationPrefix + "default-auto-instrumentation-nginx-image"
3333

34-
EnvPodName = "OTEL_RESOURCE_ATTRIBUTES_POD_NAME"
35-
EnvPodUID = "OTEL_RESOURCE_ATTRIBUTES_POD_UID"
36-
EnvPodIP = "OTEL_POD_IP"
37-
EnvNodeName = "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME"
38-
EnvNodeIP = "OTEL_NODE_IP"
34+
EnvPodName = "OTEL_RESOURCE_ATTRIBUTES_POD_NAME"
35+
EnvPodUID = "OTEL_RESOURCE_ATTRIBUTES_POD_UID"
36+
EnvPodIP = "OTEL_POD_IP"
37+
EnvNodeName = "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME"
38+
EnvNodeIP = "OTEL_NODE_IP"
39+
OtelAnnotationNamespace = "resource.opentelemetry.io/"
3940

4041
FlagCRMetrics = "enable-cr-metrics"
4142
FlagApacheHttpd = "enable-apache-httpd-instrumentation"

pkg/instrumentation/sdk.go

+9
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,15 @@ func (i *sdkInjector) createResourceMap(ctx context.Context, otelinst v1alpha1.I
479479
res[string(k)] = v
480480
}
481481
}
482+
483+
for k, v := range pod.GetAnnotations() {
484+
if strings.HasPrefix(k, constants.OtelAnnotationNamespace) {
485+
key := strings.TrimSpace(strings.TrimPrefix(k, constants.OtelAnnotationNamespace))
486+
if _, ok := res[key]; !ok {
487+
res[key] = v
488+
}
489+
}
490+
}
482491
return res
483492
}
484493

pkg/instrumentation/sdk_test.go

+106
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,112 @@ func TestSDKInjection(t *testing.T) {
508508
},
509509
},
510510
},
511+
{
512+
name: "Resource attribute propagate",
513+
inst: v1alpha1.Instrumentation{
514+
Spec: v1alpha1.InstrumentationSpec{
515+
Exporter: v1alpha1.Exporter{
516+
Endpoint: "https://collector:4317",
517+
},
518+
Resource: v1alpha1.Resource{
519+
Attributes: map[string]string{
520+
"fromcr": "val",
521+
},
522+
},
523+
Propagators: []v1alpha1.Propagator{"jaeger"},
524+
Sampler: v1alpha1.Sampler{
525+
Type: "parentbased_traceidratio",
526+
Argument: "0.25",
527+
},
528+
},
529+
},
530+
pod: corev1.Pod{
531+
ObjectMeta: metav1.ObjectMeta{
532+
Annotations: map[string]string{
533+
"resource.opentelemetry.io/fromtest": "val",
534+
"resource.opentelemetry.io/foo": "test",
535+
},
536+
Namespace: "project1",
537+
Name: "app",
538+
},
539+
Spec: corev1.PodSpec{
540+
Containers: []corev1.Container{
541+
{
542+
Image: "app:latest",
543+
Env: []corev1.EnvVar{
544+
{
545+
Name: "OTEL_SERVICE_NAME",
546+
Value: "explicitly_set",
547+
},
548+
{
549+
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
550+
Value: "explicitly_set",
551+
},
552+
{
553+
Name: "OTEL_PROPAGATORS",
554+
Value: "b3",
555+
},
556+
{
557+
Name: "OTEL_TRACES_SAMPLER",
558+
Value: "always_on",
559+
},
560+
{
561+
Name: "OTEL_RESOURCE_ATTRIBUTES",
562+
Value: "foo=bar,k8s.container.name=other,service.version=explicitly_set,",
563+
},
564+
},
565+
},
566+
},
567+
},
568+
},
569+
expected: corev1.Pod{
570+
ObjectMeta: metav1.ObjectMeta{
571+
Namespace: "project1",
572+
Name: "app",
573+
Annotations: map[string]string{
574+
"resource.opentelemetry.io/fromtest": "val",
575+
"resource.opentelemetry.io/foo": "test",
576+
},
577+
},
578+
Spec: corev1.PodSpec{
579+
Containers: []corev1.Container{
580+
{
581+
Image: "app:latest",
582+
Env: []corev1.EnvVar{
583+
{
584+
Name: "OTEL_SERVICE_NAME",
585+
Value: "explicitly_set",
586+
},
587+
{
588+
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
589+
Value: "explicitly_set",
590+
},
591+
{
592+
Name: "OTEL_PROPAGATORS",
593+
Value: "b3",
594+
},
595+
{
596+
Name: "OTEL_TRACES_SAMPLER",
597+
Value: "always_on",
598+
},
599+
{
600+
Name: "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME",
601+
ValueFrom: &corev1.EnvVarSource{
602+
FieldRef: &corev1.ObjectFieldSelector{
603+
FieldPath: "spec.nodeName",
604+
},
605+
},
606+
},
607+
{
608+
Name: "OTEL_RESOURCE_ATTRIBUTES",
609+
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",
610+
},
611+
},
612+
},
613+
},
614+
},
615+
},
616+
},
511617
}
512618

513619
for _, test := range tests {

0 commit comments

Comments
 (0)