diff --git a/exporter/prometheusexporter/go.mod b/exporter/prometheusexporter/go.mod index 09d5996c2a73..ec86aab2397f 100644 --- a/exporter/prometheusexporter/go.mod +++ b/exporter/prometheusexporter/go.mod @@ -22,6 +22,7 @@ require ( go.opentelemetry.io/collector/semconv v0.96.1-0.20240306115632-b2693620eff6 go.opentelemetry.io/otel/metric v1.24.0 go.opentelemetry.io/otel/trace v1.24.0 + go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/exporter/prometheusexporter/package_test.go b/exporter/prometheusexporter/package_test.go new file mode 100644 index 000000000000..4848448dfc0c --- /dev/null +++ b/exporter/prometheusexporter/package_test.go @@ -0,0 +1,17 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package prometheusexporter + +import ( + "testing" + + "go.uber.org/goleak" +) + +// The IgnoreTopFunction call prevents catching the leak generated by opencensus +// defaultWorker.Start which at this time is part of the package's init call. +// See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information. +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start")) +} diff --git a/exporter/prometheusexporter/prometheus_test.go b/exporter/prometheusexporter/prometheus_test.go index 763f3a34fef2..1c3c17411e24 100644 --- a/exporter/prometheusexporter/prometheus_test.go +++ b/exporter/prometheusexporter/prometheus_test.go @@ -136,8 +136,10 @@ func TestPrometheusExporter_WithTLS(t *testing.T) { t.Cleanup(func() { require.NoError(t, exp.Shutdown(context.Background())) // trigger a get so that the server cleans up our keepalive socket - _, err = httpClient.Get("https://localhost:7777/metrics") + var resp *http.Response + resp, err = httpClient.Get("https://localhost:7777/metrics") require.NoError(t, err) + require.NoError(t, resp.Body.Close()) }) assert.NotNil(t, exp) @@ -195,8 +197,10 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) { t.Cleanup(func() { require.NoError(t, exp.Shutdown(context.Background())) // trigger a get so that the server cleans up our keepalive socket - _, err = http.Get("http://localhost:7777/metrics") + var resp *http.Response + resp, err = http.Get("http://localhost:7777/metrics") require.NoError(t, err) + require.NoError(t, resp.Body.Close()) }) assert.NotNil(t, exp) @@ -277,8 +281,10 @@ func TestPrometheusExporter_endToEnd(t *testing.T) { t.Cleanup(func() { require.NoError(t, exp.Shutdown(context.Background())) // trigger a get so that the server cleans up our keepalive socket - _, err = http.Get("http://localhost:7777/metrics") + var resp *http.Response + resp, err = http.Get("http://localhost:7777/metrics") require.NoError(t, err) + require.NoError(t, resp.Body.Close()) }) assert.NotNil(t, exp) @@ -354,8 +360,10 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) { t.Cleanup(func() { require.NoError(t, exp.Shutdown(context.Background())) // trigger a get so that the server cleans up our keepalive socket - _, err = http.Get("http://localhost:7777/metrics") + var resp *http.Response + resp, err = http.Get("http://localhost:7777/metrics") require.NoError(t, err) + require.NoError(t, resp.Body.Close()) }) assert.NotNil(t, exp) @@ -434,8 +442,10 @@ func TestPrometheusExporter_endToEndWithResource(t *testing.T) { t.Cleanup(func() { require.NoError(t, exp.Shutdown(context.Background())) // trigger a get so that the server cleans up our keepalive socket - _, err = http.Get("http://localhost:7777/metrics") + var resp *http.Response + resp, err = http.Get("http://localhost:7777/metrics") require.NoError(t, err, "Failed to perform a scrape") + require.NoError(t, resp.Body.Close()) }) assert.NotNil(t, exp)