From 1849ee769c0a472f25bcaf402e280fa30bbfd460 Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 31 Dec 2024 15:50:00 +0100 Subject: [PATCH] fix http_test errcheck (avoid duplicate metric registration) --- pkg/acquisition/modules/http/http_test.go | 49 ++++++++++++----------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/pkg/acquisition/modules/http/http_test.go b/pkg/acquisition/modules/http/http_test.go index c147842466b..c76f311d669 100644 --- a/pkg/acquisition/modules/http/http_test.go +++ b/pkg/acquisition/modules/http/http_test.go @@ -218,7 +218,7 @@ func TestGetName(t *testing.T) { assert.Equal(t, "http", h.GetName()) } -func SetupAndRunHTTPSource(t *testing.T, h *HTTPSource, config []byte, metricLevel int) (chan types.Event, *tomb.Tomb) { +func SetupAndRunHTTPSource(t *testing.T, h *HTTPSource, config []byte, metricLevel int) (chan types.Event, *prometheus.Registry, *tomb.Tomb) { ctx := context.Background() subLogger := log.WithFields(log.Fields{ "type": "http", @@ -230,17 +230,18 @@ func SetupAndRunHTTPSource(t *testing.T, h *HTTPSource, config []byte, metricLev err = h.StreamingAcquisition(ctx, out, &tomb) require.NoError(t, err) + testRegistry := prometheus.NewPedanticRegistry() for _, metric := range h.GetMetrics() { - err = prometheus.Register(metric) + err = testRegistry.Register(metric) require.NoError(t, err) } - return out, &tomb + return out, testRegistry, &tomb } func TestStreamingAcquisitionWrongHTTPMethod(t *testing.T) { h := &HTTPSource{} - _, tomb := SetupAndRunHTTPSource(t, h, []byte(` + _, _, tomb:= SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -263,7 +264,7 @@ basic_auth: func TestStreamingAcquisitionUnknownPath(t *testing.T) { h := &HTTPSource{} - _, tomb := SetupAndRunHTTPSource(t, h, []byte(` + _, _, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -286,7 +287,7 @@ basic_auth: func TestStreamingAcquisitionBasicAuth(t *testing.T) { h := &HTTPSource{} - _, tomb := SetupAndRunHTTPSource(t, h, []byte(` + _, _, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -319,7 +320,7 @@ basic_auth: func TestStreamingAcquisitionBadHeaders(t *testing.T) { h := &HTTPSource{} - _, tomb := SetupAndRunHTTPSource(t, h, []byte(` + _, _, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -347,7 +348,7 @@ headers: func TestStreamingAcquisitionMaxBodySize(t *testing.T) { h := &HTTPSource{} - _, tomb := SetupAndRunHTTPSource(t, h, []byte(` + _, _, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -376,7 +377,7 @@ max_body_size: 5`), 0) func TestStreamingAcquisitionSuccess(t *testing.T) { h := &HTTPSource{} - out, tomb := SetupAndRunHTTPSource(t, h, []byte(` + out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -402,7 +403,7 @@ headers: err = <-errChan require.NoError(t, err) - assertMetrics(t, h.GetMetrics(), 1) + assertMetrics(t, reg, h.GetMetrics(), 1) h.Server.Close() tomb.Kill(nil) @@ -412,7 +413,7 @@ headers: func TestStreamingAcquisitionCustomStatusCodeAndCustomHeaders(t *testing.T) { h := &HTTPSource{} - out, tomb := SetupAndRunHTTPSource(t, h, []byte(` + out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -442,7 +443,7 @@ custom_headers: err = <-errChan require.NoError(t, err) - assertMetrics(t, h.GetMetrics(), 1) + assertMetrics(t, reg, h.GetMetrics(), 1) h.Server.Close() tomb.Kill(nil) @@ -503,7 +504,7 @@ func assertEvents(out chan types.Event, expected []string, errChan chan error) { func TestStreamingAcquisitionTimeout(t *testing.T) { h := &HTTPSource{} - _, tomb := SetupAndRunHTTPSource(t, h, []byte(` + _, _, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -539,7 +540,7 @@ timeout: 1s`), 0) func TestStreamingAcquisitionTLSHTTPRequest(t *testing.T) { h := &HTTPSource{} - _, tomb := SetupAndRunHTTPSource(t, h, []byte(` + _, _, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 auth_type: mtls @@ -564,7 +565,7 @@ tls: func TestStreamingAcquisitionTLSWithHeadersAuthSuccess(t *testing.T) { h := &HTTPSource{} - out, tomb := SetupAndRunHTTPSource(t, h, []byte(` + out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -610,7 +611,7 @@ tls: err = <-errChan require.NoError(t, err) - assertMetrics(t, h.GetMetrics(), 0) + assertMetrics(t, reg, h.GetMetrics(), 0) h.Server.Close() tomb.Kill(nil) @@ -620,7 +621,7 @@ tls: func TestStreamingAcquisitionMTLS(t *testing.T) { h := &HTTPSource{} - out, tomb := SetupAndRunHTTPSource(t, h, []byte(` + out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -668,7 +669,7 @@ tls: err = <-errChan require.NoError(t, err) - assertMetrics(t, h.GetMetrics(), 0) + assertMetrics(t, reg, h.GetMetrics(), 0) h.Server.Close() tomb.Kill(nil) @@ -678,7 +679,7 @@ tls: func TestStreamingAcquisitionGzipData(t *testing.T) { h := &HTTPSource{} - out, tomb := SetupAndRunHTTPSource(t, h, []byte(` + out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -721,7 +722,7 @@ headers: err = <-errChan require.NoError(t, err) - assertMetrics(t, h.GetMetrics(), 2) + assertMetrics(t, reg, h.GetMetrics(), 2) h.Server.Close() tomb.Kill(nil) @@ -731,7 +732,7 @@ headers: func TestStreamingAcquisitionNDJson(t *testing.T) { h := &HTTPSource{} - out, tomb := SetupAndRunHTTPSource(t, h, []byte(` + out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(` source: http listen_addr: 127.0.0.1:8080 path: /test @@ -760,7 +761,7 @@ headers: err = <-errChan require.NoError(t, err) - assertMetrics(t, h.GetMetrics(), 2) + assertMetrics(t, reg, h.GetMetrics(), 2) h.Server.Close() tomb.Kill(nil) @@ -768,8 +769,8 @@ headers: require.NoError(t, err) } -func assertMetrics(t *testing.T, metrics []prometheus.Collector, expected int) { - promMetrics, err := prometheus.DefaultGatherer.Gather() +func assertMetrics(t *testing.T, reg *prometheus.Registry, metrics []prometheus.Collector, expected int) { + promMetrics, err := reg.Gather() require.NoError(t, err) isExist := false