diff --git a/monitoring/monitoring.go b/monitoring/monitoring.go index 5cc3a3c..dc3e35d 100644 --- a/monitoring/monitoring.go +++ b/monitoring/monitoring.go @@ -117,6 +117,7 @@ func (l *listener) OnRunnableChange(started []e2e.Runnable) error { type opt struct { scrapeInterval time.Duration + customRegistry *prometheus.Registry } // WithScrapeInterval changes how often metrics are scrape by Prometheus. 5s by default. @@ -126,6 +127,15 @@ func WithScrapeInterval(interval time.Duration) func(*opt) { } } +// WithCustomRegistry allows injecting a custom registry to use for this process metrics. +// NOTE(bwplotka): Injected registry will be used as is, while the default registry +// will have prometheus.NewGoCollector() and prometheus.NewProcessCollector(..) registered. +func WithCustomRegistry(reg *prometheus.Registry) func(*opt) { + return func(o *opt) { + o.customRegistry = reg + } +} + type Option func(*opt) // Start deploys monitoring service which deploys Prometheus that monitors all registered InstrumentedServices @@ -137,11 +147,14 @@ func Start(env e2e.Environment, opts ...Option) (_ *Service, err error) { } // Expose metrics from the current process. - metrics := prometheus.NewRegistry() - metrics.MustRegister( - collectors.NewGoCollector(), - collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), - ) + metrics := opt.customRegistry + if metrics == nil { + metrics = prometheus.NewRegistry() + metrics.MustRegister( + collectors.NewGoCollector(), + collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), + ) + } m := http.NewServeMux() h := promhttp.HandlerFor(metrics, promhttp.HandlerOpts{})