metrics_receiver_protobuf_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package prometheusreceiver
  4. import (
  5. "math"
  6. "testing"
  7. dto "github.com/prometheus/prometheus/prompb/io/prometheus/client"
  8. "go.opentelemetry.io/collector/pdata/pmetric"
  9. )
  10. func TestScrapeViaProtobuf(t *testing.T) {
  11. mf := &dto.MetricFamily{
  12. Name: "test_counter",
  13. Type: dto.MetricType_COUNTER,
  14. Metric: []dto.Metric{
  15. {
  16. Label: []dto.LabelPair{
  17. {
  18. Name: "foo",
  19. Value: "bar",
  20. },
  21. },
  22. Counter: &dto.Counter{
  23. Value: 1234,
  24. },
  25. },
  26. },
  27. }
  28. buffer := prometheusMetricFamilyToProtoBuf(t, nil, mf)
  29. mf = &dto.MetricFamily{
  30. Name: "test_gauge",
  31. Type: dto.MetricType_GAUGE,
  32. Metric: []dto.Metric{
  33. {
  34. Gauge: &dto.Gauge{
  35. Value: 400.8,
  36. },
  37. },
  38. },
  39. }
  40. prometheusMetricFamilyToProtoBuf(t, buffer, mf)
  41. mf = &dto.MetricFamily{
  42. Name: "test_summary",
  43. Type: dto.MetricType_SUMMARY,
  44. Metric: []dto.Metric{
  45. {
  46. Summary: &dto.Summary{
  47. SampleCount: 1213,
  48. SampleSum: 456,
  49. Quantile: []dto.Quantile{
  50. {
  51. Quantile: 0.5,
  52. Value: 789,
  53. },
  54. {
  55. Quantile: 0.9,
  56. Value: 1011,
  57. },
  58. },
  59. },
  60. },
  61. },
  62. }
  63. prometheusMetricFamilyToProtoBuf(t, buffer, mf)
  64. mf = &dto.MetricFamily{
  65. Name: "test_histogram",
  66. Type: dto.MetricType_HISTOGRAM,
  67. Metric: []dto.Metric{
  68. {
  69. Histogram: &dto.Histogram{
  70. SampleCount: 1213,
  71. SampleSum: 456,
  72. Bucket: []dto.Bucket{
  73. {
  74. UpperBound: 0.5,
  75. CumulativeCount: 789,
  76. },
  77. {
  78. UpperBound: 10,
  79. CumulativeCount: 1011,
  80. },
  81. {
  82. UpperBound: math.Inf(1),
  83. CumulativeCount: 1213,
  84. },
  85. },
  86. },
  87. },
  88. },
  89. }
  90. prometheusMetricFamilyToProtoBuf(t, buffer, mf)
  91. expectations := []testExpectation{
  92. assertMetricPresent(
  93. "test_counter",
  94. compareMetricType(pmetric.MetricTypeSum),
  95. compareMetricUnit(""),
  96. []dataPointExpectation{{
  97. numberPointComparator: []numberPointComparator{
  98. compareDoubleValue(1234),
  99. },
  100. }},
  101. ),
  102. assertMetricPresent(
  103. "test_gauge",
  104. compareMetricType(pmetric.MetricTypeGauge),
  105. compareMetricUnit(""),
  106. []dataPointExpectation{{
  107. numberPointComparator: []numberPointComparator{
  108. compareDoubleValue(400.8),
  109. },
  110. }},
  111. ),
  112. assertMetricPresent(
  113. "test_summary",
  114. compareMetricType(pmetric.MetricTypeSummary),
  115. compareMetricUnit(""),
  116. []dataPointExpectation{{
  117. summaryPointComparator: []summaryPointComparator{
  118. compareSummary(1213, 456, [][]float64{{0.5, 789}, {0.9, 1011}}),
  119. },
  120. }},
  121. ),
  122. assertMetricPresent(
  123. "test_histogram",
  124. compareMetricType(pmetric.MetricTypeHistogram),
  125. compareMetricUnit(""),
  126. []dataPointExpectation{{
  127. histogramPointComparator: []histogramPointComparator{
  128. compareHistogram(1213, 456, []float64{0.5, 10}, []uint64{789, 222, 202}),
  129. },
  130. }},
  131. ),
  132. }
  133. targets := []*testData{
  134. {
  135. name: "target1",
  136. pages: []mockPrometheusResponse{
  137. {code: 200, useProtoBuf: true, buf: buffer.Bytes()},
  138. },
  139. validateFunc: func(t *testing.T, td *testData, result []pmetric.ResourceMetrics) {
  140. verifyNumValidScrapeResults(t, td, result)
  141. doCompare(t, "target1", td.attributes, result[0], expectations)
  142. },
  143. },
  144. }
  145. testComponent(t, targets, func(c *Config) {
  146. c.EnableProtobufNegotiation = true
  147. })
  148. }