Skip to content

Commit 8f437c4

Browse files
committed
[tunnel] guard RegisterAgentMetrics with sync.Once to prevent double-registration
1 parent abc339f commit 8f437c4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pkg/tunnel/metrics/metrics.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package metrics
22

33
import (
4+
"sync"
45
"time"
56

67
"github.com/prometheus/client_golang/prometheus"
@@ -163,14 +164,18 @@ func init() {
163164
metrics.Registry.MustRegister(TunnelBytesByProtocol)
164165
}
165166

167+
var registerAgentOnce sync.Once
168+
166169
// RegisterAgentMetrics registers agent-only metrics (info and uptime) with the
167170
// controller-runtime metrics registry. This must be called explicitly by agent
168171
// processes — server processes (tunnelproxy) should NOT call this, as they
169172
// re-export agent metrics via the AgentScraper/ReexportCollector path instead.
170173
func RegisterAgentMetrics() {
171-
TunnelAgentInfo.WithLabelValues(build.BuildVersion, build.BuildDate, build.CommitHash).Set(1)
172-
metrics.Registry.MustRegister(TunnelAgentInfo)
173-
metrics.Registry.MustRegister(TunnelAgentUptimeSeconds)
174+
registerAgentOnce.Do(func() {
175+
TunnelAgentInfo.WithLabelValues(build.BuildVersion, build.BuildDate, build.CommitHash).Set(1)
176+
metrics.Registry.MustRegister(TunnelAgentInfo)
177+
metrics.Registry.MustRegister(TunnelAgentUptimeSeconds)
178+
})
174179
}
175180

176181
// ProtocolFromIPHeader returns a protocol label from the IP next-header/protocol byte.

0 commit comments

Comments
 (0)