Skip to content
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

Merged
merged 2 commits into from
Feb 21, 2023

Conversation

sukunrt
Copy link
Member

@sukunrt sukunrt commented Feb 19, 2023

fixes: #2047

@sukunrt
Copy link
Member Author

sukunrt commented Feb 19, 2023

I made an implementation with generics too. But that seems too complex for 50 lines of code reuse.
It's in my branch: sukunrt:metrics-registerer-generic

Copy link
Contributor

@marten-seemann marten-seemann left a 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.

for _, c := range collectors {
err := reg.Register(c)
if err != nil {
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
Copy link
Contributor

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

Copy link
Member Author

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

@sukunrt sukunrt marked this pull request as ready for review February 20, 2023 06:45
@marten-seemann marten-seemann changed the title metrics: provided a WithRegisterer option for metrics metrics: add options to disable metrics and to set Prometheus registerer Feb 21, 2023
@marten-seemann marten-seemann merged commit a491074 into libp2p:master Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Metrics should be able to accept a prometheus registerer parameter
3 participants