From 700bcc7763c5305b1857a7287b85e2fd58648c2a Mon Sep 17 00:00:00 2001 From: dongjiang Date: Thu, 7 May 2026 21:59:13 +0800 Subject: [PATCH 1/3] add goleak checker Signed-off-by: dongjiang --- collector/collector_test.go | 24 ++++++++++++++++++++++++ collector/xfrm_test.go | 10 ---------- go.mod | 1 + 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 collector/collector_test.go diff --git a/collector/collector_test.go b/collector/collector_test.go new file mode 100644 index 0000000000..a3eeb92d77 --- /dev/null +++ b/collector/collector_test.go @@ -0,0 +1,24 @@ +// Copyright 2026 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package collector + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/collector/xfrm_test.go b/collector/xfrm_test.go index 9598e113bf..e6b0b4bc73 100644 --- a/collector/xfrm_test.go +++ b/collector/xfrm_test.go @@ -16,7 +16,6 @@ package collector import ( - "fmt" "io" "log/slog" "strings" @@ -134,15 +133,6 @@ func TestXfrmStats(t *testing.T) { reg := prometheus.NewRegistry() reg.MustRegister(&testXfrmCollector{xc: c}) - sink := make(chan prometheus.Metric) - go func() { - err = c.Update(sink) - if err != nil { - panic(fmt.Errorf("failed to update collector: %s", err)) - } - close(sink) - }() - err = testutil.GatherAndCompare(reg, strings.NewReader(testcase)) if err != nil { t.Fatal(err) diff --git a/go.mod b/go.mod index 31d63c20be..72d855f5c5 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/prometheus/exporter-toolkit v0.16.0 github.com/prometheus/procfs v0.20.1 github.com/safchain/ethtool v0.7.0 + go.uber.org/goleak v1.3.0 golang.org/x/sys v0.42.0 howett.net/plist v1.0.1 ) From ef8b8815e8448724ca3ed0151e30dcb6666dfa7a Mon Sep 17 00:00:00 2001 From: dongjiang Date: Thu, 7 May 2026 22:05:56 +0800 Subject: [PATCH 2/3] fix make common-check_license Signed-off-by: dongjiang --- collector/collector_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collector/collector_test.go b/collector/collector_test.go index a3eeb92d77..14c5b58d13 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -1,4 +1,4 @@ -// Copyright 2026 The Prometheus Authors +// Copyright The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at From 962d58a3524943e53e60ead8dbf3d81f73b2b6bb Mon Sep 17 00:00:00 2001 From: dongjiang Date: Fri, 8 May 2026 11:44:34 +0800 Subject: [PATCH 3/3] fix goleak check Signed-off-by: dongjiang --- collector/diskstats_linux_test.go | 5 +++++ collector/ethtool_linux_test.go | 5 +++++ collector/ipvs_linux_test.go | 11 +++++++++++ collector/mdadm_linux_test.go | 5 +++++ collector/watchdog_test.go | 5 +++++ 5 files changed, 31 insertions(+) diff --git a/collector/diskstats_linux_test.go b/collector/diskstats_linux_test.go index 08a5024c8a..465589cb22 100644 --- a/collector/diskstats_linux_test.go +++ b/collector/diskstats_linux_test.go @@ -20,6 +20,7 @@ import ( "io" "log/slog" "strings" + "sync" "testing" "github.com/prometheus/client_golang/prometheus" @@ -329,7 +330,10 @@ node_disk_written_bytes_total{device="vda"} 1.0938236928e+11 reg.MustRegister(c) sink := make(chan prometheus.Metric) + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() err = collector.Update(sink) if err != nil { panic(fmt.Errorf("failed to update collector: %s", err)) @@ -341,4 +345,5 @@ node_disk_written_bytes_total{device="vda"} 1.0938236928e+11 if err != nil { t.Fatal(err) } + wg.Wait() } diff --git a/collector/ethtool_linux_test.go b/collector/ethtool_linux_test.go index 84cca88897..cf703e10f4 100644 --- a/collector/ethtool_linux_test.go +++ b/collector/ethtool_linux_test.go @@ -24,6 +24,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" "syscall" "testing" @@ -389,7 +390,10 @@ node_network_supported_speed_bytes{device="eth0",duplex="half",mode="10baseT"} 1 reg.MustRegister(c) sink := make(chan prometheus.Metric) + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() err = collector.Update(sink) if err != nil { panic(fmt.Errorf("failed to update collector: %s", err)) @@ -401,4 +405,5 @@ node_network_supported_speed_bytes{device="eth0",duplex="half",mode="10baseT"} 1 if err != nil { t.Fatal(err) } + wg.Wait() } diff --git a/collector/ipvs_linux_test.go b/collector/ipvs_linux_test.go index b6870d8052..d563e13e19 100644 --- a/collector/ipvs_linux_test.go +++ b/collector/ipvs_linux_test.go @@ -24,6 +24,7 @@ import ( "net/http/httptest" "os" "strings" + "sync" "testing" "github.com/alecthomas/kingpin/v2" @@ -128,11 +129,15 @@ func TestIPVSCollector(t *testing.T) { } sink := make(chan prometheus.Metric) + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() err = collector.Update(sink) if err != nil { panic(fmt.Sprintf("failed to update collector: %v", err)) } + close(sink) }() for _, expected := range test.expects { got := (<-sink).Desc().String() @@ -140,6 +145,12 @@ func TestIPVSCollector(t *testing.T) { t.Fatalf("Expected '%s' but got '%s'", expected, got) } } + // Drain remaining metrics so the goroutine can finish. + go func() { + for range sink { + } + }() + wg.Wait() }) } } diff --git a/collector/mdadm_linux_test.go b/collector/mdadm_linux_test.go index 6aa216232e..186b066e5a 100644 --- a/collector/mdadm_linux_test.go +++ b/collector/mdadm_linux_test.go @@ -19,6 +19,7 @@ import ( "log/slog" "os" "strings" + "sync" "testing" "github.com/prometheus/client_golang/prometheus" @@ -278,7 +279,10 @@ func TestMdadmStats(t *testing.T) { reg.MustRegister(c) sink := make(chan prometheus.Metric) + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() err := collector.Update(sink) if err != nil { panic(err) @@ -290,4 +294,5 @@ func TestMdadmStats(t *testing.T) { if err != nil { t.Fatal(err) } + wg.Wait() } diff --git a/collector/watchdog_test.go b/collector/watchdog_test.go index 4fbcff7d53..a2ee0438b0 100644 --- a/collector/watchdog_test.go +++ b/collector/watchdog_test.go @@ -20,6 +20,7 @@ import ( "io" "log/slog" "strings" + "sync" "testing" "github.com/prometheus/client_golang/prometheus" @@ -76,7 +77,10 @@ func TestWatchdogStats(t *testing.T) { reg.MustRegister(&testWatchdogCollector{wc: c}) sink := make(chan prometheus.Metric) + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() err = c.Update(sink) if err != nil { panic(fmt.Errorf("failed to update collector: %s", err)) @@ -88,4 +92,5 @@ func TestWatchdogStats(t *testing.T) { if err != nil { t.Fatal(err) } + wg.Wait() }