Skip to content

Commit 96a5ad6

Browse files
authored
test(registry): Add goleak-based goroutine leak detection (#1797)
Signed-off-by: hyunuk <[email protected]>
1 parent 9b83d99 commit 96a5ad6

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/prometheus/client_model v0.6.1
1313
github.com/prometheus/common v0.63.0
1414
github.com/prometheus/procfs v0.16.0
15+
go.uber.org/goleak v1.2.0
1516
golang.org/x/sys v0.30.0
1617
google.golang.org/protobuf v1.36.6
1718
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
4646
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4747
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
4848
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
49+
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
50+
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
51+
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
52+
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
4953
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
5054
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
5155
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=
@@ -54,6 +58,8 @@ golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
5458
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
5559
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
5660
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
61+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
62+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
5763
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
5864
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
5965
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

prometheus/registry_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
dto "github.com/prometheus/client_model/go"
3939
"github.com/prometheus/common/expfmt"
40+
"go.uber.org/goleak"
4041
"google.golang.org/protobuf/proto"
4142
"google.golang.org/protobuf/types/known/timestamppb"
4243
)
@@ -1339,3 +1340,28 @@ func TestCheckMetricConsistency(t *testing.T) {
13391340
}
13401341
reg.Unregister(invalidCollector)
13411342
}
1343+
1344+
func TestGatherDoesNotLeakGoroutines(t *testing.T) {
1345+
// Use goleak to verify that no unexpected goroutines are leaked during the test.
1346+
defer goleak.VerifyNone(t)
1347+
1348+
// Create a new Prometheus registry without any default collectors.
1349+
reg := prometheus.NewRegistry()
1350+
1351+
// Register 100 simple Gauge metrics with distinct names and constant labels.
1352+
for i := 0; i < 100; i++ {
1353+
reg.MustRegister(prometheus.NewGauge(prometheus.GaugeOpts{
1354+
Name: "test_metric_" + string(rune(i)),
1355+
Help: "Test metric",
1356+
ConstLabels: prometheus.Labels{"id": string(rune(i))},
1357+
}))
1358+
}
1359+
1360+
// Call Gather repeatedly to simulate stress and check for potential goroutine leaks.
1361+
for i := 0; i < 1000; i++ {
1362+
_, err := reg.Gather()
1363+
if err != nil {
1364+
t.Fatalf("unexpected error from Gather: %v", err)
1365+
}
1366+
}
1367+
}

0 commit comments

Comments
 (0)