starttimemetricadjuster_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package internal
  4. import (
  5. "regexp"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. "go.opentelemetry.io/collector/pdata/pmetric"
  10. "go.uber.org/zap"
  11. )
  12. func TestStartTimeMetricMatch(t *testing.T) {
  13. const startTime = pcommon.Timestamp(123 * 1e9)
  14. const currentTime = pcommon.Timestamp(126 * 1e9)
  15. const matchBuilderStartTime = 124
  16. tests := []struct {
  17. name string
  18. inputs pmetric.Metrics
  19. startTimeMetricRegex *regexp.Regexp
  20. expectedStartTime pcommon.Timestamp
  21. expectedErr error
  22. }{
  23. {
  24. name: "regexp_match_sum_metric",
  25. inputs: metrics(
  26. sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),
  27. histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),
  28. summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),
  29. sumMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
  30. sumMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),
  31. ),
  32. startTimeMetricRegex: regexp.MustCompile("^.*_process_start_time_seconds$"),
  33. expectedStartTime: timestampFromFloat64(matchBuilderStartTime),
  34. },
  35. {
  36. name: "match_default_sum_start_time_metric",
  37. inputs: metrics(
  38. sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),
  39. histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),
  40. summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),
  41. sumMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
  42. sumMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),
  43. ),
  44. expectedStartTime: timestampFromFloat64(matchBuilderStartTime + 1),
  45. },
  46. {
  47. name: "regexp_match_gauge_metric",
  48. inputs: metrics(
  49. sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),
  50. histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),
  51. summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),
  52. gaugeMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
  53. gaugeMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),
  54. ),
  55. startTimeMetricRegex: regexp.MustCompile("^.*_process_start_time_seconds$"),
  56. expectedStartTime: timestampFromFloat64(matchBuilderStartTime),
  57. },
  58. {
  59. name: "match_default_gauge_start_time_metric",
  60. inputs: metrics(
  61. sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),
  62. histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),
  63. summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),
  64. gaugeMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
  65. gaugeMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),
  66. ),
  67. expectedStartTime: timestampFromFloat64(matchBuilderStartTime + 1),
  68. },
  69. {
  70. name: "empty gauge start time metrics",
  71. inputs: metrics(
  72. gaugeMetric("process_start_time_seconds"),
  73. ),
  74. expectedErr: errNoDataPointsStartTimeMetric,
  75. },
  76. {
  77. name: "empty sum start time metrics",
  78. inputs: metrics(
  79. sumMetric("process_start_time_seconds"),
  80. ),
  81. expectedErr: errNoDataPointsStartTimeMetric,
  82. },
  83. {
  84. name: "unsupported type start time metric",
  85. inputs: metrics(
  86. histogramMetric("process_start_time_seconds"),
  87. ),
  88. expectedErr: errUnsupportedTypeStartTimeMetric,
  89. },
  90. {
  91. name: "regexp_nomatch",
  92. inputs: metrics(
  93. sumMetric("subprocess_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
  94. ),
  95. startTimeMetricRegex: regexp.MustCompile("^.+_process_start_time_seconds$"),
  96. expectedErr: errNoStartTimeMetrics,
  97. },
  98. {
  99. name: "nomatch_default_start_time_metric",
  100. inputs: metrics(
  101. gaugeMetric("subprocess_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
  102. ),
  103. expectedErr: errNoStartTimeMetrics,
  104. },
  105. }
  106. for _, tt := range tests {
  107. t.Run(tt.name, func(t *testing.T) {
  108. stma := NewStartTimeMetricAdjuster(zap.NewNop(), tt.startTimeMetricRegex)
  109. if tt.expectedErr != nil {
  110. assert.ErrorIs(t, stma.AdjustMetrics(tt.inputs), tt.expectedErr)
  111. return
  112. }
  113. assert.NoError(t, stma.AdjustMetrics(tt.inputs))
  114. for i := 0; i < tt.inputs.ResourceMetrics().Len(); i++ {
  115. rm := tt.inputs.ResourceMetrics().At(i)
  116. for j := 0; j < rm.ScopeMetrics().Len(); j++ {
  117. ilm := rm.ScopeMetrics().At(j)
  118. for k := 0; k < ilm.Metrics().Len(); k++ {
  119. metric := ilm.Metrics().At(k)
  120. switch metric.Type() {
  121. case pmetric.MetricTypeSum:
  122. dps := metric.Sum().DataPoints()
  123. for l := 0; l < dps.Len(); l++ {
  124. assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())
  125. }
  126. case pmetric.MetricTypeSummary:
  127. dps := metric.Summary().DataPoints()
  128. for l := 0; l < dps.Len(); l++ {
  129. assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())
  130. }
  131. case pmetric.MetricTypeHistogram:
  132. dps := metric.Histogram().DataPoints()
  133. for l := 0; l < dps.Len(); l++ {
  134. assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())
  135. }
  136. case pmetric.MetricTypeEmpty, pmetric.MetricTypeGauge, pmetric.MetricTypeExponentialHistogram:
  137. }
  138. }
  139. }
  140. }
  141. })
  142. }
  143. }