Skip to content

Commit 9a7fa09

Browse files
authored
exporter/prometheus: Remove usage of hardcoded ports in tests (#40411)
#### Description Fixes #38925 Fixes #40327 Instead of using hardcoded ports, we're calling `testutil.GetAvailableLocalAddress()` instead Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent 0a81aa7 commit 9a7fa09

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

exporter/prometheusexporter/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/exporter/promet
33
go 1.23.0
44

55
require (
6+
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.127.0
67
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.127.0
78
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry v0.127.0
89
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus v0.127.0

exporter/prometheusexporter/prometheus_test.go

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,45 @@ import (
2222
conventions "go.opentelemetry.io/otel/semconv/v1.25.0"
2323

2424
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusexporter/internal/metadata"
25+
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
2526
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
2627
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry"
2728
)
2829

2930
func TestPrometheusExporter(t *testing.T) {
3031
tests := []struct {
31-
config *Config
32+
config func() *Config
3233
wantErr string
3334
wantStartErr string
3435
}{
3536
{
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+
}
4750
},
4851
},
4952
{
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+
}
5459
},
5560
wantStartErr: "listen tcp: address 88999: invalid port",
5661
},
5762
{
58-
config: &Config{},
63+
config: func() *Config { return &Config{} },
5964
wantErr: "expecting a non-blank address to run the Prometheus metrics handler",
6065
},
6166
}
@@ -65,7 +70,8 @@ func TestPrometheusExporter(t *testing.T) {
6570
for _, tt := range tests {
6671
// Run it a few times to ensure that shutdowns exit cleanly.
6772
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)
6975

7076
if tt.wantErr != "" {
7177
require.Error(t, err)
@@ -90,14 +96,15 @@ func TestPrometheusExporter(t *testing.T) {
9096
}
9197

9298
func TestPrometheusExporter_WithTLS(t *testing.T) {
99+
addr := testutil.GetAvailableLocalAddress(t)
93100
cfg := &Config{
94101
Namespace: "test",
95102
ConstLabels: map[string]string{
96103
"foo2": "bar2",
97104
"code2": "one2",
98105
},
99106
ServerConfig: confighttp.ServerConfig{
100-
Endpoint: "localhost:7777",
107+
Endpoint: addr,
101108
TLSSetting: &configtls.ServerConfig{
102109
Config: configtls.Config{
103110
CertFile: "./testdata/certs/server.crt",
@@ -146,7 +153,7 @@ func TestPrometheusExporter_WithTLS(t *testing.T) {
146153

147154
assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))
148155

149-
rsp, err := httpClient.Get("https://localhost:7777/metrics")
156+
rsp, err := httpClient.Get("https://" + addr + "/metrics")
150157
require.NoError(t, err, "Failed to perform a scrape")
151158

152159
assert.Equal(t, http.StatusOK, rsp.StatusCode, "Mismatched HTTP response status code")
@@ -168,14 +175,15 @@ func TestPrometheusExporter_WithTLS(t *testing.T) {
168175

169176
// See: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/4986
170177
func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) {
178+
addr := testutil.GetAvailableLocalAddress(t)
171179
cfg := &Config{
172180
Namespace: "test",
173181
ConstLabels: map[string]string{
174182
"foo1": "bar1",
175183
"code1": "one1",
176184
},
177185
ServerConfig: confighttp.ServerConfig{
178-
Endpoint: "localhost:7777",
186+
Endpoint: addr,
179187
},
180188
MetricExpiration: 120 * time.Minute,
181189
}
@@ -201,7 +209,7 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) {
201209
assert.NoError(t, exp.ConsumeMetrics(context.Background(), metricBuilder(int64(delta), "metric_2_", "cpu-exporter", "localhost:8080")))
202210
assert.NoError(t, exp.ConsumeMetrics(context.Background(), metricBuilder(int64(delta), "metric_2_", "cpu-exporter", "localhost:8081")))
203211

204-
res, err1 := http.Get("http://localhost:7777/metrics")
212+
res, err1 := http.Get("http://" + addr + "/metrics")
205213
require.NoError(t, err1, "Failed to perform a scrape")
206214

207215
assert.Equal(t, http.StatusOK, res.StatusCode, "Mismatched HTTP response status code")
@@ -231,7 +239,7 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) {
231239
exp.(*wrapMetricsExporter).exporter.collector.accumulator.(*lastValueAccumulator).metricExpiration = 1 * time.Millisecond
232240
time.Sleep(10 * time.Millisecond)
233241

234-
res, err := http.Get("http://localhost:7777/metrics")
242+
res, err := http.Get("http://" + addr + "/metrics")
235243
require.NoError(t, err, "Failed to perform a scrape")
236244

237245
assert.Equal(t, http.StatusOK, res.StatusCode, "Mismatched HTTP response status code")
@@ -241,14 +249,15 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) {
241249
}
242250

243251
func TestPrometheusExporter_endToEnd(t *testing.T) {
252+
addr := testutil.GetAvailableLocalAddress(t)
244253
cfg := &Config{
245254
Namespace: "test",
246255
ConstLabels: map[string]string{
247256
"foo1": "bar1",
248257
"code1": "one1",
249258
},
250259
ServerConfig: confighttp.ServerConfig{
251-
Endpoint: "localhost:7777",
260+
Endpoint: addr,
252261
},
253262
MetricExpiration: 120 * time.Minute,
254263
}
@@ -272,7 +281,7 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {
272281
for delta := 0; delta <= 20; delta += 10 {
273282
assert.NoError(t, exp.ConsumeMetrics(context.Background(), metricBuilder(int64(delta), "metric_2_", "cpu-exporter", "localhost:8080")))
274283

275-
res, err1 := http.Get("http://localhost:7777/metrics")
284+
res, err1 := http.Get("http://" + addr + "/metrics")
276285
require.NoError(t, err1, "Failed to perform a scrape")
277286

278287
assert.Equal(t, http.StatusOK, res.StatusCode, "Mismatched HTTP response status code")
@@ -298,7 +307,7 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {
298307
exp.(*wrapMetricsExporter).exporter.collector.accumulator.(*lastValueAccumulator).metricExpiration = 1 * time.Millisecond
299308
time.Sleep(10 * time.Millisecond)
300309

301-
res, err := http.Get("http://localhost:7777/metrics")
310+
res, err := http.Get("http://" + addr + "/metrics")
302311
require.NoError(t, err, "Failed to perform a scrape")
303312

304313
assert.Equal(t, http.StatusOK, res.StatusCode, "Mismatched HTTP response status code")
@@ -308,14 +317,15 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {
308317
}
309318

310319
func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {
320+
addr := testutil.GetAvailableLocalAddress(t)
311321
cfg := &Config{
312322
Namespace: "test",
313323
ConstLabels: map[string]string{
314324
"foo2": "bar2",
315325
"code2": "one2",
316326
},
317327
ServerConfig: confighttp.ServerConfig{
318-
Endpoint: "localhost:7777",
328+
Endpoint: addr,
319329
},
320330
SendTimestamps: true,
321331
MetricExpiration: 120 * time.Minute,
@@ -340,7 +350,7 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {
340350
for delta := 0; delta <= 20; delta += 10 {
341351
assert.NoError(t, exp.ConsumeMetrics(context.Background(), metricBuilder(int64(delta), "metric_2_", "node-exporter", "localhost:8080")))
342352

343-
res, err1 := http.Get("http://localhost:7777/metrics")
353+
res, err1 := http.Get("http://" + addr + "/metrics")
344354
require.NoError(t, err1, "Failed to perform a scrape")
345355

346356
assert.Equal(t, http.StatusOK, res.StatusCode, "Mismatched HTTP response status code")
@@ -366,7 +376,7 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {
366376
exp.(*wrapMetricsExporter).exporter.collector.accumulator.(*lastValueAccumulator).metricExpiration = 1 * time.Millisecond
367377
time.Sleep(10 * time.Millisecond)
368378

369-
res, err := http.Get("http://localhost:7777/metrics")
379+
res, err := http.Get("http://" + addr + "/metrics")
370380
require.NoError(t, err, "Failed to perform a scrape")
371381

372382
assert.Equal(t, http.StatusOK, res.StatusCode, "Mismatched HTTP response status code")
@@ -376,14 +386,15 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {
376386
}
377387

378388
func TestPrometheusExporter_endToEndWithResource(t *testing.T) {
389+
addr := testutil.GetAvailableLocalAddress(t)
379390
cfg := &Config{
380391
Namespace: "test",
381392
ConstLabels: map[string]string{
382393
"foo2": "bar2",
383394
"code2": "one2",
384395
},
385396
ServerConfig: confighttp.ServerConfig{
386-
Endpoint: "localhost:7777",
397+
Endpoint: addr,
387398
},
388399
SendTimestamps: true,
389400
MetricExpiration: 120 * time.Minute,
@@ -409,7 +420,7 @@ func TestPrometheusExporter_endToEndWithResource(t *testing.T) {
409420

410421
assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))
411422

412-
rsp, err := http.Get("http://localhost:7777/metrics")
423+
rsp, err := http.Get("http://" + addr + "/metrics")
413424
require.NoError(t, err, "Failed to perform a scrape")
414425

415426
assert.Equal(t, http.StatusOK, rsp.StatusCode, "Mismatched HTTP response status code")

0 commit comments

Comments
 (0)