Skip to content

Commit 7f02cb1

Browse files
committed
test(registry): Add goleak-based goroutine leak detection
Signed-off-by: hyunuk <nnhope@hotmail.com>
1 parent 34eaefd commit 7f02cb1

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/modern-go/reflect2 v1.0.2 // indirect
2424
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2525
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
26+
go.uber.org/goleak v1.2.0 // indirect
2627
golang.org/x/net v0.35.0 // indirect
2728
golang.org/x/oauth2 v0.25.0 // indirect
2829
golang.org/x/text v0.22.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ 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=
4951
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
5052
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
5153
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=

prometheus/registry_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/prometheus/common/expfmt"
4040
"google.golang.org/protobuf/proto"
4141
"google.golang.org/protobuf/types/known/timestamppb"
42+
"go.uber.org/goleak"
4243
)
4344

4445
// uncheckedCollector wraps a Collector but its Describe method yields no Desc.
@@ -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)