Skip to content

Commit

Permalink
kprom: use Registerer and Gatherer interface instead of concreate reg…
Browse files Browse the repository at this point in the history
…istry type.

This allows the user of the plugin to:
- pass prometheus.DefaultRegisterer and prometheus.Defaultgatherer
- pass a testing a custom Registerer implementation (e.g. for testing purposes)
  • Loading branch information
simon0191 committed Jul 5, 2021
1 parent 10e3f44 commit eaf9ebe
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions plugin/kprom/kprom.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,28 @@ type Metrics struct {
//
// This is useful if you want the Metrics type to create its own registry for
// you to add additional metrics to.
func (m *Metrics) Registry() *prometheus.Registry {
func (m *Metrics) Registry() prometheus.Registerer {
return m.cfg.reg
}

// Handler returns an http.Handler providing prometheus metrics.
func (m *Metrics) Handler() http.Handler {
return promhttp.HandlerFor(m.cfg.reg, m.cfg.handlerOpts)
return promhttp.HandlerFor(m.cfg.gatherer, m.cfg.handlerOpts)
}

type cfg struct {
reg *prometheus.Registry
reg prometheus.Registerer
gatherer prometheus.Gatherer

handlerOpts promhttp.HandlerOpts
goCollectors bool
}

type RegistererGatherer interface {
prometheus.Registerer
prometheus.Gatherer
}

// Opt applies options to further tune how prometheus metrics are gathered or
// which metrics to use.
type Opt interface {
Expand All @@ -96,11 +102,25 @@ type opt struct{ fn func(*cfg) }

func (o opt) apply(c *cfg) { o.fn(c) }

// Registry sets the registry to add metrics to, rather than a new registry.
func Registry(reg *prometheus.Registry) Opt {
// Registry sets the registerer and gatherer to add metrics to, rather than a new registry.
// Use this option if you want to configure both Gatherer and Registerer with the same object.
func Registry(rg RegistererGatherer) Opt {
return opt{func(c *cfg) {
c.reg = rg
c.gatherer = rg
}}
}

// Registry sets the registerer to add metrics to, rather than a new registry.
func Registerer(reg prometheus.Registerer) Opt {
return opt{func(c *cfg) { c.reg = reg }}
}

// Registry sets the gatherer to add metrics to, rather than a new registry.
func Gatherer(gatherer prometheus.Gatherer) Opt {
return opt{func(c *cfg) { c.gatherer = gatherer }}
}

// GoCollectors adds the prometheus.NewProcessCollector and
// prometheus.NewGoCollector collectors the the Metric's registry.
func GoCollectors() Opt {
Expand All @@ -119,8 +139,10 @@ func HandlerOpts(opts promhttp.HandlerOpts) Opt {
// NewMetrics returns a new Metrics that adds prometheus metrics to the
// registry under the given namespace.
func NewMetrics(namespace string, opts ...Opt) *Metrics {
var regGatherer RegistererGatherer = prometheus.NewRegistry()
cfg := cfg{
reg: prometheus.NewRegistry(),
reg: regGatherer,
gatherer: regGatherer,
}
for _, opt := range opts {
opt.apply(&cfg)
Expand Down

0 comments on commit eaf9ebe

Please sign in to comment.