Skip to content

Commit

Permalink
update promethues api.
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Aug 30, 2023
1 parent ebf2290 commit 04a5522
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
10 changes: 1 addition & 9 deletions examples/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
76 changes: 58 additions & 18 deletions monitoring/prometheus/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,132 @@ 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
}

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
}

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
}

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
}

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
}

Expand Down

0 comments on commit 04a5522

Please sign in to comment.