@@ -527,17 +527,19 @@ func TestTargetInfoWithMultipleRequests(t *testing.T) {
527
527
assert .NoError (t , prwReceiver .Shutdown (context .Background ()))
528
528
}()
529
529
530
- // Define with two different metrics, one is target_info and the other is a normal metric
530
+ // Define two different metrics, one is target_info and the other is a normal metric.
531
+ // In the end, we expect that we have 1 final metric. All labels from the target_info metric should be translated to resource attributes and
532
+ // the normal metric should be translated as usual.
531
533
requests := []* writev2.Request {
532
534
{
533
535
Symbols : []string {
534
536
"" ,
535
- "job" , "production/service_a" ,
536
- "instance" , "host1" ,
537
- "machine_type" , "n1-standard-1" ,
538
- "cloud_provider" , "gcp" ,
539
- "region" , "us-central1" ,
540
- "__name__" , "target_info" ,
537
+ "job" , "production/service_a" , // 1, 2
538
+ "instance" , "host1" , // 3, 4
539
+ "machine_type" , "n1-standard-1" , // 5, 6
540
+ "cloud_provider" , "gcp" , // 7, 8
541
+ "region" , "us-central1" , // 9, 10
542
+ "__name__" , "target_info" , // 11, 12
541
543
},
542
544
Timeseries : []writev2.TimeSeries {
543
545
{
@@ -549,10 +551,10 @@ func TestTargetInfoWithMultipleRequests(t *testing.T) {
549
551
{
550
552
Symbols : []string {
551
553
"" ,
552
- "job" , "production/service_a" ,
553
- "instance" , "host1" ,
554
- "__name__" , "normal_metric" ,
555
- "foo" , "bar" ,
554
+ "job" , "production/service_a" , // 1, 2
555
+ "instance" , "host1" , // 3, 4
556
+ "__name__" , "normal_metric" , // 5, 6
557
+ "foo" , "bar" , // 7, 8
556
558
},
557
559
Timeseries : []writev2.TimeSeries {
558
560
{
@@ -565,16 +567,12 @@ func TestTargetInfoWithMultipleRequests(t *testing.T) {
565
567
}
566
568
567
569
client := & http.Client {}
568
-
569
- // Send both requests
570
570
for _ , req := range requests {
571
- // Marshal and compress
572
571
pBuf := proto .NewBuffer (nil )
573
572
err := pBuf .Marshal (req )
574
573
assert .NoError (t , err )
575
574
compressedBody := snappy .Encode (nil , pBuf .Bytes ())
576
575
577
- // Create and send request
578
576
httpReq , err := http .NewRequest (
579
577
http .MethodPost ,
580
578
"http://0.0.0.0:9090/api/v1/write" ,
@@ -594,33 +592,31 @@ func TestTargetInfoWithMultipleRequests(t *testing.T) {
594
592
assert .Equal (t , http .StatusNoContent , resp .StatusCode , string (body ))
595
593
}
596
594
597
- // Verify final metrics
598
- // expectedMetrics := func() pmetric.Metrics {
599
- // metrics := pmetric.NewMetrics()
600
- // rm := metrics.ResourceMetrics().AppendEmpty()
601
- // attrs := rm.Resource().Attributes()
602
- // attrs.PutStr("service.namespace", "production")
603
- // attrs.PutStr("service.name", "service_a")
604
- // attrs.PutStr("service.instance.id", "host1")
605
- // attrs.PutStr("machine.type", "n1-standard-1")
606
- // attrs.PutStr("cloud.provider", "gcp")
607
- // attrs.PutStr("region", "us-central1")
608
-
609
- // sm := rm.ScopeMetrics().AppendEmpty()
610
- // sm.Scope().SetName("OpenTelemetry Collector")
611
- // sm.Scope().SetVersion("latest")
612
-
613
- // m1 := sm.Metrics().AppendEmpty()
614
- // m1.SetName("normal_metric")
615
- // m1.SetUnit("")
616
- // m1.SetDescription("")
617
- // dp1 := m1.SetEmptyGauge().DataPoints().AppendEmpty()
618
- // dp1.SetDoubleValue(2.0)
619
- // dp1.SetTimestamp(pcommon.Timestamp(2 * int64(time.Millisecond)))
620
- // dp1.Attributes().PutStr("foo", "bar")
621
-
622
- // return metrics
623
- // }()
624
-
625
- // assert.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, mockConsumer.metrics[0]))
595
+ expectedMetrics := func () pmetric.Metrics {
596
+ metrics := pmetric .NewMetrics ()
597
+ rm := metrics .ResourceMetrics ().AppendEmpty ()
598
+ attrs := rm .Resource ().Attributes ()
599
+ attrs .PutStr ("service.namespace" , "production" )
600
+ attrs .PutStr ("service.name" , "service_a" )
601
+ attrs .PutStr ("service.instance.id" , "host1" )
602
+ attrs .PutStr ("machine.type" , "n1-standard-1" )
603
+ attrs .PutStr ("cloud.provider" , "gcp" )
604
+ attrs .PutStr ("region" , "us-central1" )
605
+
606
+ sm := rm .ScopeMetrics ().AppendEmpty ()
607
+ sm .Scope ().SetName ("OpenTelemetry Collector" )
608
+ sm .Scope ().SetVersion ("latest" )
609
+ m1 := sm .Metrics ().AppendEmpty ()
610
+ m1 .SetName ("normal_metric" )
611
+ m1 .SetUnit ("" )
612
+ m1 .SetDescription ("" )
613
+ dp1 := m1 .SetEmptyGauge ().DataPoints ().AppendEmpty ()
614
+ dp1 .SetDoubleValue (2.0 )
615
+ dp1 .SetTimestamp (pcommon .Timestamp (2 * int64 (time .Millisecond )))
616
+ dp1 .Attributes ().PutStr ("foo" , "bar" )
617
+
618
+ return metrics
619
+ }()
620
+
621
+ assert .NoError (t , pmetrictest .CompareMetrics (expectedMetrics , mockConsumer .metrics [0 ]))
626
622
}
0 commit comments