Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a public registry interface and separate out HTTP exposition
General context and approch --------------------------- This is the first part of the long awaited wider refurbishment of `client_golang/prometheus/...`. After a lot of struggling, I decided to not go for one breaking big-bang, but cut things into smaller steps after all, mostly to keep the changes manageable and easy to review. I'm aiming for having the invasive breaking changes concentrated in as few steps as possible (ideally one). Some steps will not be breaking at all, but typically there will be breaking changes that only affect quite special cases so that 95+% of users will not be affected. This first step is an example for that, see details below. What's happening in this commit? -------------------------------- This step is about finally creating an exported registry interface. This could not be done by simply export the existing internal implementation because the interface would be _way_ to fat. This commit introduces a very lean `registry.Registry` interface. Most of the existing functionality that is not part of that interface is provided by utility functions (in `registry/util.go`). The top-level function that act on the default registry are mostly retained and kept in the `prometheus` package, to minimize breakage and for convenience. The general idea is here that the basic use case isn't involved with the `registry` package at all. Only if you are going for one of the advanced use cases, you need to import the `registry` package. The default registry is kept in the public variable `registry.Default`. This follows the example of the http package in the standard library (cf. `http.DefaultServeMux`, `http.DefaultClient`). Another important part in making the registry lean is the extraction of the HTTP exposition into its own package `promhttp`. Note that the package is not simply called `http` because that would collide with the `http` package from the standard library, which will usually be used in the same source file as the `promhttp` package. The following issues are fixed by this commit (some solved "on the fly" now that I was touching the code anyway and it would have been stupid to port the bugs): #46 #100 #170 #205 What future changes does this commit enable? -------------------------------------------- - The separation of the HTTP exposition allows the implementation of other exposition methods as known from other Prometheus client libraries, e.g. sending the metrics to Graphite. Cf. #197 - The public `Registry` interface allows to implement convenience tools for testing metric collection. Those tools can inspect the collected MetricFamily protobufs and compare them to expectation. Also, tests can use their own testing instance of a registry. Cf. #58 Notable non-goals of this commit -------------------------------- The following two issues are quite closely connected to the changes in this commit but the line has been drawn deliberately to address them in later steps of the refurbishment: - `InstrumentHandler` has many known problems. The plan is to create a saner way to conveniently intrument HTTP handlers in the new `promhttp` package and remove the old `InstrumentHandler` altogether. To keep breakage low for now, even the default handler to expose metrics is still using the old `InstrumentHandler`. Cf. #200 - There is work underway to make the whole handling of metric descriptors (`Desc`) more intuitive and transparent for the user (including an ability for less strict checking, cf. #47). That's quite invasive from the perspective of the internal code, namely the registry. I deliberately kept those changes out of this commit. An unfortunate side effect is that the fields of the current `Desc` had to be exported for the time being. This will be solved in a cleaner way with the upcoming `Desc` changes. Something that I have played with a lot is "streaming collection", i.e. allow an implementation of the `Registry` interface that collects metrics incrementally and serves them while doing so. As it has turned out, this has many many issues and makes the `Registry` interface very clunky. Eventually, I made the call that it is unlikely we will really implement streaming collection, and making the interface more clunky for something that might not even happen is really a big no-no. Note that the `Registry` interface only creates the in-memory representation of the metric family protobufs in one go. The serializaton onto the wire can still be handled in a streaming fashion. What are the breaking changes? ------------------------------ - Signature of functions pushing to Pushgateway has changed to allow arbitrary grouping (long planned anyway, and now that I worked on it anyway, I did it, cf. #100). Also, the PushCollectors and PushAddCollectors has been moved into the promhttp package, where the new Push and PushAdd functions for custom registries live, too. - `SetMetricFamilyInjectionHook` is gone. A registry with a MetricFamily injection hook has to be created now with `registry.NewWithInjectionHook`. - `PanicOnCollectError` is gone. This behavior can now be configured when creating a `promhttp.Handler`. - `EnableCollectChecks` is gone. A registry with those checks can now be created with `registry.NewTestRegistry` (it is only ever used to test custom Collectors). - `NewProcessCollector`, `NewGoCollector`, and `NewExpvarCollector` have been moved into their own package `collectors`.
- Loading branch information