prommiddleware

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2024 License: MIT Imports: 9 Imported by: 0

README

prometheus-middleware Build Status Go Report Card

Prometheus middleware supports only gorilla/mux.

Installation

go get -u github.com/albertogg/prommiddleware

What you will get

You will get:

  • the HTTP request duration in seconds (http_request_duration_seconds) histogram partitioned by status status, method and HTTP path.
...
# HELP http_request_duration_seconds How long it took to process the request, partitioned by status code, method and HTTP path.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{status="200",method="get",path="/",le="0.3"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/",le="1"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/",le="2.5"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/",le="5"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/",le="+Inf"} 2
http_request_duration_seconds_sum{status="200",method="get",path="/"} 3.5256e-05
http_request_duration_seconds_count{status="200",method="get",path="/"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/metrics",le="0.3"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/metrics",le="1"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/metrics",le="2.5"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/metrics",le="5"} 2
http_request_duration_seconds_bucket{status="200",method="get",path="/metrics",le="+Inf"} 2
http_request_duration_seconds_sum{status="200",method="get",path="/metrics"} 0.001261767
http_request_duration_seconds_count{status="200",method="get",path="/metrics"} 2
...
  • HTTP request total (http_requests_total) partitioned by status status, method, and HTTP path.
...
# HELP http_requests_total How many HTTP requests processed, partitioned by status code, method and HTTP path.
# TYPE http_requests_total counter
http_requests_total{status="200",method="get",path="/"} 2
http_requests_total{status="200",method="get",path="/metrics"} 2
...

How to use it

  • Gorilla/Mux
reg := prometheus.NewRegistry()

pm := prommiddleware.New(Opts{Registry: reg})
if err != nil {
    panic(err)
}

r := mux.NewRouter()
r.Handle("/metrics", pm.Handler())
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    fmt.Fprintln(w, "ok")
})

r.Use(pm.InstrumentHandlerDuration)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingRegistry = errors.New("missing registry")

When the registry is not passed in the Opts

Functions

This section is empty.

Types

type Opts

type Opts struct {
	// A prometheus registry
	Registry *prometheus.Registry
	// Buckets specifies an custom buckets to be used in request histograpm.
	Buckets []float64
	// Labels specifies the label names that will be used
	Labels []string
}

Opts specifies options how to create new PrometheusMiddleware.

type PromMiddleware

type PromMiddleware struct {
	// contains filtered or unexported fields
}

PromMiddleware specifies the metrics that is going to be generated

func New

func New(opts Opts) (*PromMiddleware, error)

New creates a new PrometheusMiddleware instance

func (*PromMiddleware) Handler

func (p *PromMiddleware) Handler() http.Handler

Handler will return the handler for the metrics based on the registry setup.

func (*PromMiddleware) InstrumentHandlerDuration

func (p *PromMiddleware) InstrumentHandlerDuration(next http.Handler) http.Handler

InstrumentHandlerDuration is a middleware that wraps the http.Handler and it record how long the handler took to run, which path was called, and the status code. This method is going to be used with gorilla/mux.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL