@@ -22,40 +22,45 @@ import (
22
22
conventions "go.opentelemetry.io/otel/semconv/v1.25.0"
23
23
24
24
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusexporter/internal/metadata"
25
+ "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
25
26
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
26
27
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry"
27
28
)
28
29
29
30
func TestPrometheusExporter (t * testing.T ) {
30
31
tests := []struct {
31
- config * Config
32
+ config func () * Config
32
33
wantErr string
33
34
wantStartErr string
34
35
}{
35
36
{
36
- config : & Config {
37
- Namespace : "test" ,
38
- ConstLabels : map [string ]string {
39
- "foo0" : "bar0" ,
40
- "code0" : "one0" ,
41
- },
42
- ServerConfig : confighttp.ServerConfig {
43
- Endpoint : "localhost:8999" ,
44
- },
45
- SendTimestamps : false ,
46
- MetricExpiration : 60 * time .Second ,
37
+ config : func () * Config {
38
+ return & Config {
39
+ Namespace : "test" ,
40
+ ConstLabels : map [string ]string {
41
+ "foo0" : "bar0" ,
42
+ "code0" : "one0" ,
43
+ },
44
+ ServerConfig : confighttp.ServerConfig {
45
+ Endpoint : testutil .GetAvailableLocalAddress (t ),
46
+ },
47
+ SendTimestamps : false ,
48
+ MetricExpiration : 60 * time .Second ,
49
+ }
47
50
},
48
51
},
49
52
{
50
- config : & Config {
51
- ServerConfig : confighttp.ServerConfig {
52
- Endpoint : "localhost:88999" ,
53
- },
53
+ config : func () * Config {
54
+ return & Config {
55
+ ServerConfig : confighttp.ServerConfig {
56
+ Endpoint : "localhost:88999" ,
57
+ },
58
+ }
54
59
},
55
60
wantStartErr : "listen tcp: address 88999: invalid port" ,
56
61
},
57
62
{
58
- config : & Config {},
63
+ config : func () * Config { return & Config {} },
59
64
wantErr : "expecting a non-blank address to run the Prometheus metrics handler" ,
60
65
},
61
66
}
@@ -65,7 +70,8 @@ func TestPrometheusExporter(t *testing.T) {
65
70
for _ , tt := range tests {
66
71
// Run it a few times to ensure that shutdowns exit cleanly.
67
72
for j := 0 ; j < 3 ; j ++ {
68
- exp , err := factory .CreateMetrics (context .Background (), set , tt .config )
73
+ cfg := tt .config ()
74
+ exp , err := factory .CreateMetrics (context .Background (), set , cfg )
69
75
70
76
if tt .wantErr != "" {
71
77
require .Error (t , err )
@@ -90,14 +96,15 @@ func TestPrometheusExporter(t *testing.T) {
90
96
}
91
97
92
98
func TestPrometheusExporter_WithTLS (t * testing.T ) {
99
+ addr := testutil .GetAvailableLocalAddress (t )
93
100
cfg := & Config {
94
101
Namespace : "test" ,
95
102
ConstLabels : map [string ]string {
96
103
"foo2" : "bar2" ,
97
104
"code2" : "one2" ,
98
105
},
99
106
ServerConfig : confighttp.ServerConfig {
100
- Endpoint : "localhost:7777" ,
107
+ Endpoint : addr ,
101
108
TLSSetting : & configtls.ServerConfig {
102
109
Config : configtls.Config {
103
110
CertFile : "./testdata/certs/server.crt" ,
@@ -146,7 +153,7 @@ func TestPrometheusExporter_WithTLS(t *testing.T) {
146
153
147
154
assert .NoError (t , exp .ConsumeMetrics (context .Background (), md ))
148
155
149
- rsp , err := httpClient .Get ("https://localhost:7777 /metrics" )
156
+ rsp , err := httpClient .Get ("https://" + addr + " /metrics" )
150
157
require .NoError (t , err , "Failed to perform a scrape" )
151
158
152
159
assert .Equal (t , http .StatusOK , rsp .StatusCode , "Mismatched HTTP response status code" )
@@ -168,14 +175,15 @@ func TestPrometheusExporter_WithTLS(t *testing.T) {
168
175
169
176
// See: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/4986
170
177
func TestPrometheusExporter_endToEndMultipleTargets (t * testing.T ) {
178
+ addr := testutil .GetAvailableLocalAddress (t )
171
179
cfg := & Config {
172
180
Namespace : "test" ,
173
181
ConstLabels : map [string ]string {
174
182
"foo1" : "bar1" ,
175
183
"code1" : "one1" ,
176
184
},
177
185
ServerConfig : confighttp.ServerConfig {
178
- Endpoint : "localhost:7777" ,
186
+ Endpoint : addr ,
179
187
},
180
188
MetricExpiration : 120 * time .Minute ,
181
189
}
@@ -201,7 +209,7 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) {
201
209
assert .NoError (t , exp .ConsumeMetrics (context .Background (), metricBuilder (int64 (delta ), "metric_2_" , "cpu-exporter" , "localhost:8080" )))
202
210
assert .NoError (t , exp .ConsumeMetrics (context .Background (), metricBuilder (int64 (delta ), "metric_2_" , "cpu-exporter" , "localhost:8081" )))
203
211
204
- res , err1 := http .Get ("http://localhost:7777 /metrics" )
212
+ res , err1 := http .Get ("http://" + addr + " /metrics" )
205
213
require .NoError (t , err1 , "Failed to perform a scrape" )
206
214
207
215
assert .Equal (t , http .StatusOK , res .StatusCode , "Mismatched HTTP response status code" )
@@ -231,7 +239,7 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) {
231
239
exp .(* wrapMetricsExporter ).exporter .collector .accumulator .(* lastValueAccumulator ).metricExpiration = 1 * time .Millisecond
232
240
time .Sleep (10 * time .Millisecond )
233
241
234
- res , err := http .Get ("http://localhost:7777 /metrics" )
242
+ res , err := http .Get ("http://" + addr + " /metrics" )
235
243
require .NoError (t , err , "Failed to perform a scrape" )
236
244
237
245
assert .Equal (t , http .StatusOK , res .StatusCode , "Mismatched HTTP response status code" )
@@ -241,14 +249,15 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) {
241
249
}
242
250
243
251
func TestPrometheusExporter_endToEnd (t * testing.T ) {
252
+ addr := testutil .GetAvailableLocalAddress (t )
244
253
cfg := & Config {
245
254
Namespace : "test" ,
246
255
ConstLabels : map [string ]string {
247
256
"foo1" : "bar1" ,
248
257
"code1" : "one1" ,
249
258
},
250
259
ServerConfig : confighttp.ServerConfig {
251
- Endpoint : "localhost:7777" ,
260
+ Endpoint : addr ,
252
261
},
253
262
MetricExpiration : 120 * time .Minute ,
254
263
}
@@ -272,7 +281,7 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {
272
281
for delta := 0 ; delta <= 20 ; delta += 10 {
273
282
assert .NoError (t , exp .ConsumeMetrics (context .Background (), metricBuilder (int64 (delta ), "metric_2_" , "cpu-exporter" , "localhost:8080" )))
274
283
275
- res , err1 := http .Get ("http://localhost:7777 /metrics" )
284
+ res , err1 := http .Get ("http://" + addr + " /metrics" )
276
285
require .NoError (t , err1 , "Failed to perform a scrape" )
277
286
278
287
assert .Equal (t , http .StatusOK , res .StatusCode , "Mismatched HTTP response status code" )
@@ -298,7 +307,7 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {
298
307
exp .(* wrapMetricsExporter ).exporter .collector .accumulator .(* lastValueAccumulator ).metricExpiration = 1 * time .Millisecond
299
308
time .Sleep (10 * time .Millisecond )
300
309
301
- res , err := http .Get ("http://localhost:7777 /metrics" )
310
+ res , err := http .Get ("http://" + addr + " /metrics" )
302
311
require .NoError (t , err , "Failed to perform a scrape" )
303
312
304
313
assert .Equal (t , http .StatusOK , res .StatusCode , "Mismatched HTTP response status code" )
@@ -308,14 +317,15 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {
308
317
}
309
318
310
319
func TestPrometheusExporter_endToEndWithTimestamps (t * testing.T ) {
320
+ addr := testutil .GetAvailableLocalAddress (t )
311
321
cfg := & Config {
312
322
Namespace : "test" ,
313
323
ConstLabels : map [string ]string {
314
324
"foo2" : "bar2" ,
315
325
"code2" : "one2" ,
316
326
},
317
327
ServerConfig : confighttp.ServerConfig {
318
- Endpoint : "localhost:7777" ,
328
+ Endpoint : addr ,
319
329
},
320
330
SendTimestamps : true ,
321
331
MetricExpiration : 120 * time .Minute ,
@@ -340,7 +350,7 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {
340
350
for delta := 0 ; delta <= 20 ; delta += 10 {
341
351
assert .NoError (t , exp .ConsumeMetrics (context .Background (), metricBuilder (int64 (delta ), "metric_2_" , "node-exporter" , "localhost:8080" )))
342
352
343
- res , err1 := http .Get ("http://localhost:7777 /metrics" )
353
+ res , err1 := http .Get ("http://" + addr + " /metrics" )
344
354
require .NoError (t , err1 , "Failed to perform a scrape" )
345
355
346
356
assert .Equal (t , http .StatusOK , res .StatusCode , "Mismatched HTTP response status code" )
@@ -366,7 +376,7 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {
366
376
exp .(* wrapMetricsExporter ).exporter .collector .accumulator .(* lastValueAccumulator ).metricExpiration = 1 * time .Millisecond
367
377
time .Sleep (10 * time .Millisecond )
368
378
369
- res , err := http .Get ("http://localhost:7777 /metrics" )
379
+ res , err := http .Get ("http://" + addr + " /metrics" )
370
380
require .NoError (t , err , "Failed to perform a scrape" )
371
381
372
382
assert .Equal (t , http .StatusOK , res .StatusCode , "Mismatched HTTP response status code" )
@@ -376,14 +386,15 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {
376
386
}
377
387
378
388
func TestPrometheusExporter_endToEndWithResource (t * testing.T ) {
389
+ addr := testutil .GetAvailableLocalAddress (t )
379
390
cfg := & Config {
380
391
Namespace : "test" ,
381
392
ConstLabels : map [string ]string {
382
393
"foo2" : "bar2" ,
383
394
"code2" : "one2" ,
384
395
},
385
396
ServerConfig : confighttp.ServerConfig {
386
- Endpoint : "localhost:7777" ,
397
+ Endpoint : addr ,
387
398
},
388
399
SendTimestamps : true ,
389
400
MetricExpiration : 120 * time .Minute ,
@@ -409,7 +420,7 @@ func TestPrometheusExporter_endToEndWithResource(t *testing.T) {
409
420
410
421
assert .NoError (t , exp .ConsumeMetrics (context .Background (), md ))
411
422
412
- rsp , err := http .Get ("http://localhost:7777 /metrics" )
423
+ rsp , err := http .Get ("http://" + addr + " /metrics" )
413
424
require .NoError (t , err , "Failed to perform a scrape" )
414
425
415
426
assert .Equal (t , http .StatusOK , rsp .StatusCode , "Mismatched HTTP response status code" )
0 commit comments