@@ -147,7 +147,6 @@ func TestSDKInjection(t *testing.T) {
147
147
Labels : map [string ]string {
148
148
"app.kubernetes.io/name" : "app-name" ,
149
149
"app.kubernetes.io/version" : "v1" ,
150
- "app.kubernetes.io/part-of" : "shop" ,
151
150
},
152
151
Annotations : map [string ]string {
153
152
"resource.opentelemetry.io/foo" : "bar" ,
@@ -157,7 +156,7 @@ func TestSDKInjection(t *testing.T) {
157
156
Containers : []corev1.Container {
158
157
{
159
158
Name : "application-name" ,
160
- Image : "app:latest " ,
159
+ Image : "app:v1@sha256:232a180dbcbcfa7250917507f3827d88a9ae89bb1cdd8fe3ac4db7b764ebb25a " ,
161
160
},
162
161
},
163
162
},
@@ -170,7 +169,6 @@ func TestSDKInjection(t *testing.T) {
170
169
Labels : map [string ]string {
171
170
"app.kubernetes.io/name" : "app-name" ,
172
171
"app.kubernetes.io/version" : "v1" ,
173
- "app.kubernetes.io/part-of" : "shop" ,
174
172
},
175
173
Annotations : map [string ]string {
176
174
"resource.opentelemetry.io/foo" : "bar" ,
@@ -188,7 +186,7 @@ func TestSDKInjection(t *testing.T) {
188
186
Containers : []corev1.Container {
189
187
{
190
188
Name : "application-name" ,
191
- Image : "app:latest " ,
189
+ Image : "app:v1@sha256:232a180dbcbcfa7250917507f3827d88a9ae89bb1cdd8fe3ac4db7b764ebb25a " ,
192
190
Env : []corev1.EnvVar {
193
191
{
194
192
Name : "OTEL_SERVICE_NAME" ,
@@ -228,7 +226,7 @@ func TestSDKInjection(t *testing.T) {
228
226
},
229
227
{
230
228
Name : "OTEL_RESOURCE_ATTRIBUTES" ,
231
- Value : "foo=bar,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=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),k8s.pod.uid=pod-uid,k8s.replicaset.name=my-replicaset,k8s.replicaset.uid=rsuid,service.instance.id=project1.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).application-name,service.version=latest " ,
229
+ Value : "foo=bar,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=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),k8s.pod.uid=pod-uid,k8s.replicaset.name=my-replicaset,k8s.replicaset.uid=rsuid,service.instance.id=project1.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).application-name,service.version=v1@sha256:232a180dbcbcfa7250917507f3827d88a9ae89bb1cdd8fe3ac4db7b764ebb25a " ,
232
230
},
233
231
},
234
232
},
@@ -385,7 +383,6 @@ func TestSDKInjection(t *testing.T) {
385
383
Labels : map [string ]string {
386
384
"app.kubernetes.io/name" : "app-name" ,
387
385
"app.kubernetes.io/version" : "v1" ,
388
- "app.kubernetes.io/part-of" : "shop" ,
389
386
},
390
387
Annotations : map [string ]string {
391
388
"resource.opentelemetry.io/foo" : "bar" ,
@@ -408,7 +405,6 @@ func TestSDKInjection(t *testing.T) {
408
405
Labels : map [string ]string {
409
406
"app.kubernetes.io/name" : "app-name" ,
410
407
"app.kubernetes.io/version" : "v1" ,
411
- "app.kubernetes.io/part-of" : "shop" ,
412
408
},
413
409
Annotations : map [string ]string {
414
410
"resource.opentelemetry.io/foo" : "bar" ,
@@ -466,7 +462,7 @@ func TestSDKInjection(t *testing.T) {
466
462
},
467
463
{
468
464
Name : "OTEL_RESOURCE_ATTRIBUTES" ,
469
- Value : "foo=bar,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=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),k8s.pod.uid=pod-uid,k8s.replicaset.name=my-replicaset,k8s.replicaset.uid=rsuid,service.instance.id=project1.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).application-name,service.namespace=shop,service. version=v1" ,
465
+ Value : "foo=bar,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=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),k8s.pod.uid=pod-uid,k8s.replicaset.name=my-replicaset,k8s.replicaset.uid=rsuid,service.instance.id=project1.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).application-name,service.version=v1" ,
470
466
},
471
467
},
472
468
},
@@ -503,7 +499,6 @@ func TestSDKInjection(t *testing.T) {
503
499
Labels : map [string ]string {
504
500
"app.kubernetes.io/name" : "not-used" ,
505
501
"app.kubernetes.io/version" : "not-used" ,
506
- "app.kubernetes.io/part-of" : "not-used" ,
507
502
},
508
503
},
509
504
Spec : corev1.PodSpec {
@@ -543,7 +538,6 @@ func TestSDKInjection(t *testing.T) {
543
538
Labels : map [string ]string {
544
539
"app.kubernetes.io/name" : "not-used" ,
545
540
"app.kubernetes.io/version" : "not-used" ,
546
- "app.kubernetes.io/part-of" : "not-used" ,
547
541
},
548
542
},
549
543
Spec : corev1.PodSpec {
@@ -2407,7 +2401,7 @@ func TestChooseServiceName(t *testing.T) {
2407
2401
index int
2408
2402
expectedServiceName string
2409
2403
useLabelsForResourceAttributes bool
2410
- labelValue string
2404
+ labelValue map [ string ] string
2411
2405
annotationValue string
2412
2406
}{
2413
2407
{
@@ -2436,7 +2430,7 @@ func TestChooseServiceName(t *testing.T) {
2436
2430
string (semconv .K8SPodNameKey ): "my-pod" ,
2437
2431
},
2438
2432
index : 0 ,
2439
- labelValue : " annotation" ,
2433
+ labelValue : map [ string ] string { "app.kubernetes.io/name" : " annotation"} ,
2440
2434
useLabelsForResourceAttributes : false ,
2441
2435
expectedServiceName : "my-pod" ,
2442
2436
},
@@ -2445,8 +2439,23 @@ func TestChooseServiceName(t *testing.T) {
2445
2439
resources : map [string ]string {
2446
2440
string (semconv .K8SPodNameKey ): "my-pod" ,
2447
2441
},
2448
- index : 0 ,
2449
- labelValue : "label" ,
2442
+ index : 0 ,
2443
+ labelValue : map [string ]string {
2444
+ "app.kubernetes.io/name" : "label" ,
2445
+ },
2446
+ useLabelsForResourceAttributes : true ,
2447
+ expectedServiceName : "label" ,
2448
+ },
2449
+ {
2450
+ name : "from pod label - useLabelsForResourceAttributes=true" ,
2451
+ resources : map [string ]string {
2452
+ string (semconv .K8SPodNameKey ): "my-pod" ,
2453
+ },
2454
+ index : 0 ,
2455
+ labelValue : map [string ]string {
2456
+ "app.kubernetes.io/instance" : "label" ,
2457
+ "app.kubernetes.io/name" : "secondary-label" ,
2458
+ },
2450
2459
useLabelsForResourceAttributes : true ,
2451
2460
expectedServiceName : "label" ,
2452
2461
},
@@ -2457,7 +2466,7 @@ func TestChooseServiceName(t *testing.T) {
2457
2466
},
2458
2467
index : 0 ,
2459
2468
annotationValue : "annotation" ,
2460
- labelValue : " label" ,
2469
+ labelValue : map [ string ] string { "app.kubernetes.io/name" : " label"} ,
2461
2470
useLabelsForResourceAttributes : false ,
2462
2471
expectedServiceName : "annotation" ,
2463
2472
},
@@ -2468,7 +2477,7 @@ func TestChooseServiceName(t *testing.T) {
2468
2477
},
2469
2478
index : 0 ,
2470
2479
annotationValue : "annotation" ,
2471
- labelValue : " label" ,
2480
+ labelValue : map [ string ] string { "app.kubernetes.io/name" : " label"} ,
2472
2481
useLabelsForResourceAttributes : true ,
2473
2482
expectedServiceName : "annotation" ,
2474
2483
},
@@ -2533,9 +2542,7 @@ func TestChooseServiceName(t *testing.T) {
2533
2542
t .Run (test .name , func (t * testing.T ) {
2534
2543
serviceName := chooseServiceName (corev1.Pod {
2535
2544
ObjectMeta : metav1.ObjectMeta {
2536
- Labels : map [string ]string {
2537
- "app.kubernetes.io/name" : test .labelValue ,
2538
- },
2545
+ Labels : test .labelValue ,
2539
2546
Annotations : map [string ]string {
2540
2547
"resource.opentelemetry.io/service.name" : test .annotationValue ,
2541
2548
},
@@ -2634,3 +2641,51 @@ func TestAppendIfNotSet(t *testing.T) {
2634
2641
})
2635
2642
}
2636
2643
}
2644
+
2645
+ func Test_parseServiceVersionFromImage (t * testing.T ) {
2646
+ tests := []struct {
2647
+ name string
2648
+ image string
2649
+ want string
2650
+ }{
2651
+ {
2652
+ name : "no version" ,
2653
+ image : "img" ,
2654
+ },
2655
+ {
2656
+ name : "with tag" ,
2657
+ image : "img:1" ,
2658
+ want : "1" ,
2659
+ },
2660
+ {
2661
+ name : "with tag and digest" ,
2662
+ image : "img:1@sha256:232a180dbcbcfa7250917507f3827d88a9ae89bb1cdd8fe3ac4db7b764ebb25a" ,
2663
+ want : "1@sha256:232a180dbcbcfa7250917507f3827d88a9ae89bb1cdd8fe3ac4db7b764ebb25a" ,
2664
+ },
2665
+ {
2666
+ name : "with digest" ,
2667
+ image : "img@sha256:232a180dbcbcfa7250917507f3827d88a9ae89bb1cdd8fe3ac4db7b764ebb25a" ,
2668
+ want : "sha256:232a180dbcbcfa7250917507f3827d88a9ae89bb1cdd8fe3ac4db7b764ebb25a" ,
2669
+ },
2670
+ {
2671
+ name : "with registry" ,
2672
+ image : "registry.io/img" ,
2673
+ },
2674
+ {
2675
+ name : "with port" ,
2676
+ image : "registry.io:8080/img" ,
2677
+ },
2678
+ {
2679
+ name : "latest" ,
2680
+ image : "img:latest" ,
2681
+ want : "latest" ,
2682
+ },
2683
+ }
2684
+ for _ , tt := range tests {
2685
+ t .Run (tt .name , func (t * testing.T ) {
2686
+ got , _ := parseServiceVersionFromImage (tt .image )
2687
+ // error is just for debugging
2688
+ assert .Equalf (t , tt .want , got , "parseServiceVersionFromImage(%v)" , tt .image )
2689
+ })
2690
+ }
2691
+ }
0 commit comments