util_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"
  4. import (
  5. "testing"
  6. "time"
  7. "github.com/prometheus/common/model"
  8. "github.com/prometheus/prometheus/model/labels"
  9. "github.com/prometheus/prometheus/model/textparse"
  10. "github.com/prometheus/prometheus/scrape"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "go.opentelemetry.io/collector/pdata/pcommon"
  14. "go.opentelemetry.io/collector/pdata/pmetric"
  15. )
  16. var testMetadata = map[string]scrape.MetricMetadata{
  17. "counter_test": {Metric: "counter_test", Type: textparse.MetricTypeCounter, Help: "", Unit: ""},
  18. "counter_test2": {Metric: "counter_test2", Type: textparse.MetricTypeCounter, Help: "", Unit: ""},
  19. "gauge_test": {Metric: "gauge_test", Type: textparse.MetricTypeGauge, Help: "", Unit: ""},
  20. "gauge_test2": {Metric: "gauge_test2", Type: textparse.MetricTypeGauge, Help: "", Unit: ""},
  21. "hist_test": {Metric: "hist_test", Type: textparse.MetricTypeHistogram, Help: "", Unit: ""},
  22. "hist_test2": {Metric: "hist_test2", Type: textparse.MetricTypeHistogram, Help: "", Unit: ""},
  23. "ghist_test": {Metric: "ghist_test", Type: textparse.MetricTypeGaugeHistogram, Help: "", Unit: ""},
  24. "summary_test": {Metric: "summary_test", Type: textparse.MetricTypeSummary, Help: "", Unit: ""},
  25. "summary_test2": {Metric: "summary_test2", Type: textparse.MetricTypeSummary, Help: "", Unit: ""},
  26. "unknown_test": {Metric: "unknown_test", Type: textparse.MetricTypeUnknown, Help: "", Unit: ""},
  27. "poor_name": {Metric: "poor_name", Type: textparse.MetricTypeGauge, Help: "", Unit: ""},
  28. "poor_name_count": {Metric: "poor_name_count", Type: textparse.MetricTypeCounter, Help: "", Unit: ""},
  29. "scrape_foo": {Metric: "scrape_foo", Type: textparse.MetricTypeCounter, Help: "", Unit: ""},
  30. "example_process_start_time_seconds": {Metric: "example_process_start_time_seconds",
  31. Type: textparse.MetricTypeGauge, Help: "", Unit: ""},
  32. "process_start_time_seconds": {Metric: "process_start_time_seconds",
  33. Type: textparse.MetricTypeGauge, Help: "", Unit: ""},
  34. "subprocess_start_time_seconds": {Metric: "subprocess_start_time_seconds",
  35. Type: textparse.MetricTypeGauge, Help: "", Unit: ""},
  36. }
  37. func TestTimestampFromMs(t *testing.T) {
  38. assert.Equal(t, pcommon.Timestamp(0), timestampFromMs(0))
  39. assert.Equal(t, pcommon.NewTimestampFromTime(time.UnixMilli(1662679535432)), timestampFromMs(1662679535432))
  40. }
  41. func TestTimestampFromFloat64(t *testing.T) {
  42. assert.Equal(t, pcommon.Timestamp(0), timestampFromFloat64(0))
  43. // Because of float64 conversion, we check only that we are within 100ns error.
  44. assert.InEpsilon(t, uint64(1662679535040000000), uint64(timestampFromFloat64(1662679535.040)), 100)
  45. }
  46. func TestConvToMetricType(t *testing.T) {
  47. tests := []struct {
  48. name string
  49. mtype textparse.MetricType
  50. want pmetric.MetricType
  51. wantMonotonic bool
  52. }{
  53. {
  54. name: "textparse.counter",
  55. mtype: textparse.MetricTypeCounter,
  56. want: pmetric.MetricTypeSum,
  57. wantMonotonic: true,
  58. },
  59. {
  60. name: "textparse.gauge",
  61. mtype: textparse.MetricTypeGauge,
  62. want: pmetric.MetricTypeGauge,
  63. wantMonotonic: false,
  64. },
  65. {
  66. name: "textparse.unknown",
  67. mtype: textparse.MetricTypeUnknown,
  68. want: pmetric.MetricTypeGauge,
  69. wantMonotonic: false,
  70. },
  71. {
  72. name: "textparse.histogram",
  73. mtype: textparse.MetricTypeHistogram,
  74. want: pmetric.MetricTypeHistogram,
  75. wantMonotonic: true,
  76. },
  77. {
  78. name: "textparse.summary",
  79. mtype: textparse.MetricTypeSummary,
  80. want: pmetric.MetricTypeSummary,
  81. wantMonotonic: true,
  82. },
  83. {
  84. name: "textparse.metric_type_info",
  85. mtype: textparse.MetricTypeInfo,
  86. want: pmetric.MetricTypeSum,
  87. wantMonotonic: false,
  88. },
  89. {
  90. name: "textparse.metric_state_set",
  91. mtype: textparse.MetricTypeStateset,
  92. want: pmetric.MetricTypeSum,
  93. wantMonotonic: false,
  94. },
  95. {
  96. name: "textparse.metric_gauge_hostogram",
  97. mtype: textparse.MetricTypeGaugeHistogram,
  98. want: pmetric.MetricTypeEmpty,
  99. wantMonotonic: false,
  100. },
  101. }
  102. for _, tt := range tests {
  103. tt := tt
  104. t.Run(tt.name, func(t *testing.T) {
  105. got, monotonic := convToMetricType(tt.mtype)
  106. require.Equal(t, got.String(), tt.want.String())
  107. require.Equal(t, monotonic, tt.wantMonotonic)
  108. })
  109. }
  110. }
  111. func TestGetBoundary(t *testing.T) {
  112. tests := []struct {
  113. name string
  114. mtype pmetric.MetricType
  115. labels labels.Labels
  116. wantValue float64
  117. wantErr error
  118. }{
  119. {
  120. name: "cumulative histogram with bucket label",
  121. mtype: pmetric.MetricTypeHistogram,
  122. labels: labels.FromStrings(model.BucketLabel, "0.256"),
  123. wantValue: 0.256,
  124. },
  125. {
  126. name: "gauge histogram with bucket label",
  127. mtype: pmetric.MetricTypeHistogram,
  128. labels: labels.FromStrings(model.BucketLabel, "11.71"),
  129. wantValue: 11.71,
  130. },
  131. {
  132. name: "summary with bucket label",
  133. mtype: pmetric.MetricTypeSummary,
  134. labels: labels.FromStrings(model.BucketLabel, "11.71"),
  135. wantErr: errEmptyQuantileLabel,
  136. },
  137. {
  138. name: "summary with quantile label",
  139. mtype: pmetric.MetricTypeSummary,
  140. labels: labels.FromStrings(model.QuantileLabel, "92.88"),
  141. wantValue: 92.88,
  142. },
  143. {
  144. name: "gauge histogram mismatched with bucket label",
  145. mtype: pmetric.MetricTypeSummary,
  146. labels: labels.FromStrings(model.BucketLabel, "11.71"),
  147. wantErr: errEmptyQuantileLabel,
  148. },
  149. {
  150. name: "other data types without matches",
  151. mtype: pmetric.MetricTypeGauge,
  152. labels: labels.FromStrings(model.BucketLabel, "11.71"),
  153. wantErr: errNoBoundaryLabel,
  154. },
  155. }
  156. for _, tt := range tests {
  157. tt := tt
  158. t.Run(tt.name, func(t *testing.T) {
  159. value, err := getBoundary(tt.mtype, tt.labels)
  160. if tt.wantErr != nil {
  161. assert.ErrorIs(t, err, tt.wantErr)
  162. return
  163. }
  164. assert.NoError(t, err)
  165. assert.Equal(t, value, tt.wantValue)
  166. })
  167. }
  168. }