From 04a5522ec2dc1ce7395c14022cbb62d2b951392e Mon Sep 17 00:00:00 2001 From: Googol Lee Date: Wed, 30 Aug 2023 18:07:13 +0200 Subject: [PATCH] update promethues api. --- examples/prometheus_test.go | 10 +---- monitoring/prometheus/metric.go | 76 +++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 27 deletions(-) diff --git a/examples/prometheus_test.go b/examples/prometheus_test.go index 5c3676b..0038c6f 100644 --- a/examples/prometheus_test.go +++ b/examples/prometheus_test.go @@ -28,7 +28,7 @@ func HandlerAddCounter(ctx espresso.Context) error { } func LaunchWithPrometheus() (addr string, cancel func()) { - server, _ := espresso.New(prometheus.New("/metrics")) + server, _ := espresso.New(prometheus.New()) server.HandleFunc(HandlerAddCounter) @@ -81,16 +81,8 @@ func ExampleMonitoringWithPrometheus() { // 200 text/plain; version=0.0.4; charset=utf-8 # HELP mycounter // # TYPE mycounter counter // mycounter 0 - // # HELP promhttp_metric_handler_errors_total Total number of internal errors encountered by the promhttp metric handler. - // # TYPE promhttp_metric_handler_errors_total counter - // promhttp_metric_handler_errors_total{cause="encoding"} 0 - // promhttp_metric_handler_errors_total{cause="gathering"} 0 // 200 text/plain; version=0.0.4; charset=utf-8 # HELP mycounter // # TYPE mycounter counter // mycounter 100 - // # HELP promhttp_metric_handler_errors_total Total number of internal errors encountered by the promhttp metric handler. - // # TYPE promhttp_metric_handler_errors_total counter - // promhttp_metric_handler_errors_total{cause="encoding"} 0 - // promhttp_metric_handler_errors_total{cause="gathering"} 0 } diff --git a/monitoring/prometheus/metric.go b/monitoring/prometheus/metric.go index 0bd036d..ff28eba 100644 --- a/monitoring/prometheus/metric.go +++ b/monitoring/prometheus/metric.go @@ -8,25 +8,23 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -var DefaultRegistry = prometheus.NewRegistry() - type GaugeOpts = prometheus.GaugeOpts func NewGauge(opt GaugeOpts) prometheus.Gauge { ret := prometheus.NewGauge(opt) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } func NewGaugeFunc(opt GaugeOpts, fn func() float64) prometheus.GaugeFunc { ret := prometheus.NewGaugeFunc(opt, fn) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } func NewGaugeVec(opt GaugeOpts, labels []string) *prometheus.GaugeVec { ret := prometheus.NewGaugeVec(opt, labels) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } @@ -34,19 +32,19 @@ type CounterOpts = prometheus.CounterOpts func NewCounter(opt CounterOpts) prometheus.Counter { ret := prometheus.NewCounter(opt) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } func NewCounterFunc(opt CounterOpts, fn func() float64) prometheus.CounterFunc { ret := prometheus.NewCounterFunc(opt, fn) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } func NewCounterVec(opt CounterOpts, labels []string) *prometheus.CounterVec { ret := prometheus.NewCounterVec(opt, labels) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } @@ -54,13 +52,13 @@ type SummaryOpts = prometheus.SummaryOpts func NewSummary(opt SummaryOpts) prometheus.Summary { ret := prometheus.NewSummary(opt) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } func NewSummaryVec(opt SummaryOpts, labels []string) *prometheus.SummaryVec { ret := prometheus.NewSummaryVec(opt, labels) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } @@ -68,13 +66,13 @@ type HistogramOpts = prometheus.HistogramOpts func NewHistogram(opt HistogramOpts) prometheus.Histogram { ret := prometheus.NewHistogram(opt) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } func NewHistogramVec(opt HistogramOpts, labels []string) *prometheus.HistogramVec { ret := prometheus.NewHistogramVec(opt, labels) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } @@ -82,18 +80,60 @@ type UntypedOpts = prometheus.UntypedOpts func NewUntypedFunc(opt UntypedOpts, fn func() float64) prometheus.UntypedFunc { ret := prometheus.NewUntypedFunc(opt, fn) - DefaultRegistry.MustRegister(ret) + prometheus.MustRegister(ret) return ret } -func New(path string) espresso.ServerOption { - handler := promhttp.HandlerFor(DefaultRegistry, promhttp.HandlerOpts{ - Registry: DefaultRegistry, - }) +type Prometheus struct { + path string + registry prometheus.Registerer + gatherers prometheus.Gatherers +} + +type Option func(*Prometheus) + +func WithPath(path string) Option { + return func(p *Prometheus) { + if path == "" { + return + } + + p.path = path + } +} + +func WithRegistry(registry *prometheus.Registry) Option { + return func(p *Prometheus) { + if registry == nil { + return + } + + p.registry = registry + } +} + +func WithGatherers(gatherer ...prometheus.Gatherer) Option { + return func(p *Prometheus) { + p.gatherers = gatherer + } +} + +func New(opts ...Option) espresso.ServerOption { + p := Prometheus{ + path: "/metrics", + registry: prometheus.DefaultRegisterer, + gatherers: []prometheus.Gatherer{prometheus.DefaultGatherer}, + } + + for _, opt := range opts { + opt(&p) + } + + handler := promhttp.InstrumentMetricHandler(p.registry, promhttp.HandlerFor(p.gatherers, promhttp.HandlerOpts{})) return func(s *espresso.Server) error { s.HandleFunc(func(ctx espresso.Context) error { - if err := ctx.Endpoint(http.MethodGet, path).End(); err != nil { + if err := ctx.Endpoint(http.MethodGet, p.path).End(); err != nil { return err }