Skip to content

Commit 5a08906

Browse files
committed
validate expected metric in the end of request
1 parent 14f989d commit 5a08906

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

receiver/prometheusremotewritereceiver/receiver_test.go

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,19 @@ func TestTargetInfoWithMultipleRequests(t *testing.T) {
527527
assert.NoError(t, prwReceiver.Shutdown(context.Background()))
528528
}()
529529

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.
531533
requests := []*writev2.Request{
532534
{
533535
Symbols: []string{
534536
"",
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
541543
},
542544
Timeseries: []writev2.TimeSeries{
543545
{
@@ -549,10 +551,10 @@ func TestTargetInfoWithMultipleRequests(t *testing.T) {
549551
{
550552
Symbols: []string{
551553
"",
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
556558
},
557559
Timeseries: []writev2.TimeSeries{
558560
{
@@ -565,16 +567,12 @@ func TestTargetInfoWithMultipleRequests(t *testing.T) {
565567
}
566568

567569
client := &http.Client{}
568-
569-
// Send both requests
570570
for _, req := range requests {
571-
// Marshal and compress
572571
pBuf := proto.NewBuffer(nil)
573572
err := pBuf.Marshal(req)
574573
assert.NoError(t, err)
575574
compressedBody := snappy.Encode(nil, pBuf.Bytes())
576575

577-
// Create and send request
578576
httpReq, err := http.NewRequest(
579577
http.MethodPost,
580578
"http://0.0.0.0:9090/api/v1/write",
@@ -594,33 +592,31 @@ func TestTargetInfoWithMultipleRequests(t *testing.T) {
594592
assert.Equal(t, http.StatusNoContent, resp.StatusCode, string(body))
595593
}
596594

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]))
626622
}

0 commit comments

Comments
 (0)