forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_test.go
234 lines (212 loc) · 6.91 KB
/
metrics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package telemetry
import (
"context"
"fmt"
"net/http"
"testing"
"time"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/contrib/config"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
"go.opentelemetry.io/collector/service/internal/promtest"
"go.opentelemetry.io/collector/service/internal/resource"
"go.opentelemetry.io/collector/service/telemetry/internal/otelinit"
)
const (
metricPrefix = "otelcol_"
otelPrefix = "otel_sdk_"
grpcPrefix = "grpc_"
httpPrefix = "http_"
counterName = "test_counter"
)
var testInstanceID = "test_instance_id"
func TestTelemetryInit(t *testing.T) {
type metricValue struct {
value float64
labels map[string]string
}
for _, tt := range []struct {
name string
disableHighCard bool
expectedMetrics map[string]metricValue
}{
{
name: "UseOpenTelemetryForInternalMetrics",
expectedMetrics: map[string]metricValue{
metricPrefix + otelPrefix + counterName: {
value: 13,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
metricPrefix + grpcPrefix + counterName: {
value: 11,
labels: map[string]string{
"net_sock_peer_addr": "",
"net_sock_peer_name": "",
"net_sock_peer_port": "",
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
metricPrefix + httpPrefix + counterName: {
value: 10,
labels: map[string]string{
"net_host_name": "",
"net_host_port": "",
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
"target_info": {
value: 0,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
},
},
{
name: "DisableHighCardinalityWithOtel",
disableHighCard: true,
expectedMetrics: map[string]metricValue{
metricPrefix + otelPrefix + counterName: {
value: 13,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
metricPrefix + grpcPrefix + counterName: {
value: 11,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
metricPrefix + httpPrefix + counterName: {
value: 10,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
"target_info": {
value: 0,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
},
},
} {
prom := promtest.GetAvailableLocalAddressPrometheus(t)
endpoint := fmt.Sprintf("http://%s:%d/metrics", *prom.Host, *prom.Port)
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{
Metrics: MetricsConfig{
Level: configtelemetry.LevelDetailed,
Readers: []config.MetricReader{{
Pull: &config.PullMetricReader{Exporter: config.MetricExporter{Prometheus: prom}},
}},
},
Traces: TracesConfig{
Processors: []config.SpanProcessor{
{
Batch: &config.BatchSpanProcessor{
Exporter: config.SpanExporter{
Console: config.Console{},
},
},
},
},
},
Resource: map[string]*string{
semconv.AttributeServiceInstanceID: &testInstanceID,
},
}
set := meterProviderSettings{
res: resource.New(component.NewDefaultBuildInfo(), cfg.Resource),
cfg: cfg.Metrics,
asyncErrorChannel: make(chan error),
}
mp, err := newMeterProvider(set, tt.disableHighCard)
require.NoError(t, err)
defer func() {
if prov, ok := mp.(interface{ Shutdown(context.Context) error }); ok {
require.NoError(t, prov.Shutdown(context.Background()))
}
}()
createTestMetrics(t, mp)
metrics := getMetricsFromPrometheus(t, endpoint)
require.Equal(t, len(tt.expectedMetrics), len(metrics))
for metricName, metricValue := range tt.expectedMetrics {
mf, present := metrics[metricName]
require.True(t, present, "expected metric %q was not present", metricName)
require.Len(t, mf.Metric, 1, "only one measure should exist for metric %q", metricName)
labels := make(map[string]string)
for _, pair := range mf.Metric[0].Label {
labels[pair.GetName()] = pair.GetValue()
}
require.Equal(t, metricValue.labels, labels, "labels for metric %q was different than expected", metricName)
require.InDelta(t, metricValue.value, mf.Metric[0].Counter.GetValue(), 0.01, "value for metric %q was different than expected", metricName)
}
})
}
}
func createTestMetrics(t *testing.T, mp metric.MeterProvider) {
// Creates a OTel Go counter
counter, err := mp.Meter("collector_test").Int64Counter(metricPrefix+otelPrefix+counterName, metric.WithUnit("ms"))
require.NoError(t, err)
counter.Add(context.Background(), 13)
grpcExampleCounter, err := mp.Meter(otelinit.GRPCInstrumentation).Int64Counter(metricPrefix + grpcPrefix + counterName)
require.NoError(t, err)
grpcExampleCounter.Add(context.Background(), 11, metric.WithAttributeSet(otelinit.GRPCUnacceptableKeyValues))
httpExampleCounter, err := mp.Meter(otelinit.HTTPInstrumentation).Int64Counter(metricPrefix + httpPrefix + counterName)
require.NoError(t, err)
httpExampleCounter.Add(context.Background(), 10, metric.WithAttributeSet(otelinit.HTTPUnacceptableKeyValues))
}
func getMetricsFromPrometheus(t *testing.T, endpoint string) map[string]*io_prometheus_client.MetricFamily {
client := &http.Client{
Timeout: 10 * time.Second,
}
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
require.NoError(t, err)
var rr *http.Response
maxRetries := 5
for i := 0; i < maxRetries; i++ {
rr, err = client.Do(req)
if err == nil && rr.StatusCode == http.StatusOK {
break
}
// skip sleep on last retry
if i < maxRetries-1 {
time.Sleep(2 * time.Second) // Wait before retrying
}
}
require.NoError(t, err, "failed to get metrics from Prometheus after %d attempts", maxRetries)
require.Equal(t, http.StatusOK, rr.StatusCode, "unexpected status code after %d attempts", maxRetries)
defer rr.Body.Close()
var parser expfmt.TextParser
parsed, err := parser.TextToMetricFamilies(rr.Body)
require.NoError(t, err)
return parsed
}