Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/otelcol/receiver/prometheus/internal/starttimemetricadjuster_test.go
5443 views
1
// Copyright The OpenTelemetry Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
package internal
16
17
import (
18
"regexp"
19
"testing"
20
21
"github.com/stretchr/testify/assert"
22
"go.opentelemetry.io/collector/pdata/pcommon"
23
"go.opentelemetry.io/collector/pdata/pmetric"
24
"go.uber.org/zap"
25
)
26
27
func TestStartTimeMetricMatch(t *testing.T) {
28
const startTime = pcommon.Timestamp(123 * 1e9)
29
const currentTime = pcommon.Timestamp(126 * 1e9)
30
const matchBuilderStartTime = 124
31
32
tests := []struct {
33
name string
34
inputs pmetric.Metrics
35
startTimeMetricRegex *regexp.Regexp
36
expectedStartTime pcommon.Timestamp
37
expectedErr error
38
}{
39
{
40
name: "regexp_match_sum_metric",
41
inputs: metrics(
42
sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),
43
histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),
44
summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),
45
sumMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
46
sumMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),
47
),
48
startTimeMetricRegex: regexp.MustCompile("^.*_process_start_time_seconds$"),
49
expectedStartTime: timestampFromFloat64(matchBuilderStartTime),
50
},
51
{
52
name: "match_default_sum_start_time_metric",
53
inputs: metrics(
54
sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),
55
histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),
56
summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),
57
sumMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
58
sumMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),
59
),
60
expectedStartTime: timestampFromFloat64(matchBuilderStartTime + 1),
61
},
62
{
63
name: "regexp_match_gauge_metric",
64
inputs: metrics(
65
sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),
66
histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),
67
summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),
68
gaugeMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
69
gaugeMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),
70
),
71
startTimeMetricRegex: regexp.MustCompile("^.*_process_start_time_seconds$"),
72
expectedStartTime: timestampFromFloat64(matchBuilderStartTime),
73
},
74
{
75
name: "match_default_gauge_start_time_metric",
76
inputs: metrics(
77
sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),
78
histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),
79
summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),
80
gaugeMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
81
gaugeMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),
82
),
83
expectedStartTime: timestampFromFloat64(matchBuilderStartTime + 1),
84
},
85
{
86
name: "empty gauge start time metrics",
87
inputs: metrics(
88
gaugeMetric("process_start_time_seconds"),
89
),
90
expectedErr: errNoDataPointsStartTimeMetric,
91
},
92
{
93
name: "empty sum start time metrics",
94
inputs: metrics(
95
sumMetric("process_start_time_seconds"),
96
),
97
expectedErr: errNoDataPointsStartTimeMetric,
98
},
99
{
100
name: "unsupported type start time metric",
101
inputs: metrics(
102
histogramMetric("process_start_time_seconds"),
103
),
104
expectedErr: errUnsupportedTypeStartTimeMetric,
105
},
106
{
107
name: "regexp_nomatch",
108
inputs: metrics(
109
sumMetric("subprocess_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
110
),
111
startTimeMetricRegex: regexp.MustCompile("^.+_process_start_time_seconds$"),
112
expectedErr: errNoStartTimeMetrics,
113
},
114
{
115
name: "nomatch_default_start_time_metric",
116
inputs: metrics(
117
gaugeMetric("subprocess_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),
118
),
119
expectedErr: errNoStartTimeMetrics,
120
},
121
}
122
123
for _, tt := range tests {
124
t.Run(tt.name, func(t *testing.T) {
125
stma := NewStartTimeMetricAdjuster(zap.NewNop(), tt.startTimeMetricRegex)
126
if tt.expectedErr != nil {
127
assert.ErrorIs(t, stma.AdjustMetrics(tt.inputs), tt.expectedErr)
128
return
129
}
130
assert.NoError(t, stma.AdjustMetrics(tt.inputs))
131
for i := 0; i < tt.inputs.ResourceMetrics().Len(); i++ {
132
rm := tt.inputs.ResourceMetrics().At(i)
133
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
134
ilm := rm.ScopeMetrics().At(j)
135
for k := 0; k < ilm.Metrics().Len(); k++ {
136
metric := ilm.Metrics().At(k)
137
switch metric.Type() {
138
case pmetric.MetricTypeSum:
139
dps := metric.Sum().DataPoints()
140
for l := 0; l < dps.Len(); l++ {
141
assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())
142
}
143
case pmetric.MetricTypeSummary:
144
dps := metric.Summary().DataPoints()
145
for l := 0; l < dps.Len(); l++ {
146
assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())
147
}
148
case pmetric.MetricTypeHistogram:
149
dps := metric.Histogram().DataPoints()
150
for l := 0; l < dps.Len(); l++ {
151
assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())
152
}
153
}
154
}
155
}
156
}
157
})
158
}
159
}
160
161