Skip to content

Commit

Permalink
[chore][exporter/prometheusexporter] Enable goleak check (#30965)
Browse files Browse the repository at this point in the history
**Description:** 
This enables the `goleak` package for testing the Prometheus exporter.
This helps to ensure no goroutines are leaked by this component. This is
a test only change, the only updates are making sure all tests close the
response body of `Get` calls.

**Link to tracking Issue:**
#30438

**Testing:** 
All tests are passing, including added `goleak` test.
  • Loading branch information
crobert-1 authored Mar 25, 2024
1 parent 3a8275e commit e22c1f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions exporter/prometheusexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
go.opentelemetry.io/collector/semconv v0.96.1-0.20240322165517-15201f1e5967
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
)
Expand Down
17 changes: 17 additions & 0 deletions exporter/prometheusexporter/package_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}
20 changes: 15 additions & 5 deletions exporter/prometheusexporter/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e22c1f7

Please sign in to comment.