Skip to content

A simple, configurable middleware for recording request metrics with Prometheus, in Go

License

Notifications You must be signed in to change notification settings

imbue11235/promware

Repository files navigation

promware Test Status Go Reference

A simple, configurable middleware for recording request metrics with Prometheus

🛠 Installation

Make sure to have Go installed (Version 1.16 or higher).

Install promware with go get:

$ go get -u github.com/imbue11235/promware

💻 Usage

With standard library

middleware := promware.Default()

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    fmt.Fprintln(w, "hello world")
})

mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.Handle("/", middleware(handler))

http.ListenAndServe("<addr>", mux)

Options

middleware := promware.New(
    // Skip depending on request data 
    promware.WithSkipFunc(func(r *http.Request) bool {
        return r.URL.Path == "/something"
    })
    	
    // Set subsystem
    promware.WithSubsystem("my-subsystem")
    
    // Set namespace 
    promware.WithNamespace("my-namespace")
    
    // Add request counter, set name
    promware.WithRequestCounter("total-requests")
    
    // Add latency histogram, set name and buckets
    promware.WithLatencyHistogram("request-latency", []float64{0.5, 1, 2})
)

📜 License

This project is licensed under the MIT license.

About

A simple, configurable middleware for recording request metrics with Prometheus, in Go

Topics

Resources

License

Stars

Watchers

Forks