fix: cache PrometheusMetrics to prevent duplicate registration panic#1
fix: cache PrometheusMetrics to prevent duplicate registration panic#1
Conversation
NewPrometheusMetrics now caches instances per namespace using sync.RWMutex with double-checked locking. Calling it multiple times with the same namespace returns the cached instance instead of panicking on duplicate Prometheus registration.
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughAdds a process-global cache for Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant NewPrometheusMetrics
participant Cache
participant PromRegistry as PrometheusRegistry
Caller->>NewPrometheusMetrics: request(namespace)
NewPrometheusMetrics->>Cache: RLock & lookup(namespace)
Cache-->>NewPrometheusMetrics: hit? (instance or nil)
alt cache hit
NewPrometheusMetrics-->>Caller: return cached instance
else cache miss
NewPrometheusMetrics->>Cache: RUnlock, Lock (write)
NewPrometheusMetrics->>Cache: re-check(namespace)
alt still missing
NewPrometheusMetrics->>PromRegistry: register vectors/gauges
NewPrometheusMetrics->>Cache: store new instance
end
NewPrometheusMetrics->>Cache: Unlock
NewPrometheusMetrics-->>Caller: return new instance
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR prevents panics from duplicate Prometheus collector registration by making NewPrometheusMetrics(namespace) return a cached, per-namespace singleton instance, and updates the public docs to reflect the new behavior.
Changes:
- Added a package-level cache (guarded by
sync.RWMutexwith double-checked locking) to reuse*prometheusMetricsinstances per namespace. - Updated
NewPrometheusMetricsdocumentation to state it’s safe to call multiple times with the same namespace. - Updated README API docs accordingly.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| README.md | Documents the new “safe to call multiple times” behavior for NewPrometheusMetrics. |
| metrics.go | Introduces a per-namespace cache + locking to avoid duplicate Prometheus registrations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Add tests: same namespace returns same instance, different namespaces return different instances, concurrent calls are safe - Document that cache is process-global and namespaces should be static
Summary
NewPrometheusMetrics(namespace)now caches instances per namespacesync.RWMutexwith double-checked locking — reads (cache hits) use read lock onlyTest plan
go test -race ./...passesmake lint— 0 issuesSummary by CodeRabbit
Documentation
Bug Fixes
Tests