/xprometheus

Wrapper around Prometheus for xmux/xhandler

Primary LanguageGoMIT LicenseMIT

xprometheus

A Go library to transparently instrument HTTP handlers (including xhandler) Prometheus. It has two parts:

  • An xhandler handler that automatically instruments any request passed through it.

Limitations

To avoid clashing with Prometheus' own metrics handler, you must use a different namespace, as in the examples below.

Importing

import "github.com/atombender/xprometheus"

Example use

With wrapper mux

This will automatically remember the registered mux route and use that as the metrics label.

import (
  "github.com/atombender/xprometheus"
  "github.com/prometheus/client_golang/prometheus"
  "github.com/rs/xhandler"
	"github.com/rs/xmux"
)

func thingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  // ...
}

func main() {
  mux = xprometheus.NewMux(prometheus.SummaryOpts{
		Namespace: "prometheus",
    Subsystem: "http",
  })
  mux.GET("/api/v1/things", xhandler.HandlerFuncC(thingHandler))
	mux.Handle("GET", "/metrics", promhttp.Handler())

  log.Fatal(http.ListenAndServe("locahost:8080", mux)
}

With just the handler

This will not remember the registered mux route. Instead, the URL path is used as the metrics label. This means that /api/v1/things and /api/v1/things/123 will have two different metrics stored, rather than being grouped into one.

import (
  "github.com/atombender/xprometheus"
  "github.com/prometheus/client_golang/prometheus"
  "github.com/rs/xhandler"
	"github.com/rs/xmux"
)

func thingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  // ...
}

func main() {
  mux = xmux.New()
  mux.GET("/api/v1/things", xhandler.HandlerFuncC(thingHandler))
  mux.GET("/api/v1/things/:id", xhandler.HandlerFuncC(thingHandler))
	mux.Handle("GET", "/metrics", promhttp.Handler())

  chain := xhandler.Chain{}
  chain.UseC(xprometheus.InstrumentingHandlerWithOpts("", prometheus.SummaryOpts{
		Namespace: "prometheus",
    Subsystem: "http",
  }))

  log.Fatal(http.ListenAndServe("locahost:8080", chain.Handler(mux))
}

License

MIT. See LICENSE file.