Hi! This question is about best practices for testing applications that use 
client_golang to instrument with custom metrics and avoiding the dreaded 
"duplicate metrics collector registration attempted".

Let's say I have an HTTP handler for an app that keeps a counter. The 
counter can be page hits, orders submitted, whatever. Let's say my handler, 
a struct with ServeHTTP attached, refers to this counter either as a 
package level variable or as a a struct member. I've seen the same behavior 
either way. Suppose I keep the typical patterns: use promauto, register my 
Collectors in New, or register my collectors at package initialization.

When I put my handler under test, I typically create a handler per test. 
This causes me to bump into "duplicate metrics collector registration 
attempted".

Some ways I have worked around this are to create a single httptest.Server 
with my handler in TestMain, or attach a RegisterMetrics to defer collector 
registration where I'm wiring up my dependencies (often main's main func) 
and just not register the metrics under test. The former makes it harder to 
inject mocks with different behaviors, the latter makes it hard to test my 
metrics.

So, the questions are:
1) What are some good ways to deal with metric registration under test?
2) Is testing metrics a typical practice, or do you just trust them? In the 
contrived example, it may be reasonably safe to just trust it, but I'm 
confident we have seen those cases where there is some tricky behavior 
around our metrics and we want to be sure we have it right.
3) When testing metrics, such as in an exporter concept where that is in 
fact the key behavior, is it typical to test the metrics endpoint with an 
http call (say, with httptest.Server) or do you look at the values held by 
the collectors?

Thanks!
Victor

-- 
You received this message because you are subscribed to the Google Groups 
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to prometheus-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-users/3632f922-7287-4516-8d53-5587e8af6ef1%40googlegroups.com.

Reply via email to