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