weaveworks / common

Libraries used in multiple Weave projects
Other
129 stars 92 forks source link

Allow users to manually register metrics #289

Closed thampiotr closed 1 year ago

thampiotr commented 1 year ago

As discussed in https://github.com/weaveworks/common/issues/285 - this PR adds ability to manually register metrics, for example, when users restart the Server, but want to reuse existing and already registered metrics.

It does so by introducing a new, alternative constructor function for the server so a user can pick the preferred implementation:

// New makes a new Server. It will panic if the metrics cannot be registered.
func New(cfg Config) (*Server, error) {
    metrics := NewServerMetrics(cfg)
    metrics.MustRegister(cfg.registererOrDefault())
    return newServer(cfg, metrics)
}

// NewWithMetrics makes a new Server using the provided Metrics. It will not attempt to register the metrics,
// the user is responsible for doing so.
func NewWithMetrics(cfg Config, metrics *Metrics) (*Server, error) {
    return newServer(cfg, metrics)
}

Fixes https://github.com/weaveworks/common/issues/285