-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
metrics: add options to disable metrics and to set Prometheus registerer #2116
Conversation
I made an implementation with generics too. But that seems too complex for 50 lines of code reuse. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed to not make this generic.
There now needs to be some way to set the Registerer
via a libp2p option.
p2p/metricshelper/registerer.go
Outdated
for _, c := range collectors { | ||
err := reg.Register(c) | ||
if err != nil { | ||
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably slightly safer to do use errors.Is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the returned error is an instance of the type
type AlreadyRegisteredError struct {
ExistingCollector, NewCollector Collector
}
Checking with errors.Is will not work since we do not know the existing collector, so I changed this to use errors.As
There is a tricky case here:
If an error is returned where we are registering a collector where a different collector with the same fully qualified name was registered before, this method will suppress it and the collector used as argument to the method will not be collected for metrics. In this example:
reg := prometheus.NewRegistry()
c1 := prometheus.NewCounter(
prometheus.CounterOpts{
Name: "counter",
},
)
c2 := prometheus.NewCounter(
prometheus.CounterOpts{
Name: "counter",
},
)
RegisterCollectors(reg, c1)
RegisterCollectors(reg, c2) // c2 is not registered
c1.Inc()
c2.Inc() // This will have no effect
I'm leaving it as it is because this situation doesn't arise anywhere in the code yet since we are using namespaces for our metrics
fixes: #2047