On 9 June 2016 at 14:29, Keegan Carruthers-Smith
The first style is most straight forward but it is most suitable for
self-contained programs or within a limited, controlled code-base.
The second approach is more flexible. It defers the use of global
state (the global registration) and avoids code execution in `init()`
and is thus more suitable for modularized approaches where the user of
a package decides what to register. It also allows to create metrics
on the fly during collection time.
You can also strike a balance between the two styles: Nothing forces
you to register metrics in `init()`, so you can call `Register()`
later, e.g. upon instantiation of an instrumented object, which gives
you the opportunity to parametrize the metrics with fixed labels and
such. And your metrics don't have to be global vars, although it often
makes sense.
In principle, you only need to consider implementing `Collector` if
you create metrics on the fly during collection. Within the Prometheus
codebase, we do this occasionally, and while we are on it, it's often
easier to stuff in all the normal metrics, too, and then have just one
`Register()` call (but more LOCs in `Collect()` and `Describe()`).
The Go client library will soon export a Registry interface so that
you can register with different registries (for separate exposure or
test purposes) and avoid global state altogether. In which case you
either have to parametrize the registry to register with in your
constructor, or go the other way and implement Collector so that the
user of your package can register the whole thing at will.
Some people just hate global state. Others embrace it and happily have
loads of metrics as global vars.
https://peter.bourgon.org/go-best-practices-2016/#logging-and-instrumentation
is an interesting read in this context.
Hope this is not too convoluted and makes at least a bit of sense.
Brian's answer is simpler, and depending on your concrete scenario, it
might be sufficient.
I'll try to add more textbook-style documentation after I'm done with
the current set of (pretty fundamental) changes of the Go client (cf.
exported Registry above).
--
Björn Rabenstein, Engineer
http://soundcloud.com/brabenstein
SoundCloud Ltd. | Rheinsberger Str. 76/77, 10115 Berlin, Germany
Managing Director: Alexander Ljung | Incorporated in England & Wales
with Company No. 6343600 | Local Branch Office | AG Charlottenburg |
HRB 110657B