Skip to content

Commit

Permalink
fix http_test errcheck (avoid duplicate metric registration)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Dec 31, 2024
1 parent 82ea14d commit 1849ee7
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions pkg/acquisition/modules/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -760,16 +761,16 @@ headers:
err = <-errChan
require.NoError(t, err)

assertMetrics(t, h.GetMetrics(), 2)
assertMetrics(t, reg, h.GetMetrics(), 2)

h.Server.Close()
tomb.Kill(nil)
err = tomb.Wait()
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
Expand Down

0 comments on commit 1849ee7

Please sign in to comment.