promware

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: MIT Imports: 6 Imported by: 0

README ¶

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.

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func WithDefaultLatencyHistogram ¶

func WithDefaultLatencyHistogram() option

WithDefaultLatencyHistogram creates a latency histogram collector with the name `request_duration_seconds`

func WithDefaultRequestCounter ¶

func WithDefaultRequestCounter() option

WithDefaultRequestCounter creates a request counter with the default name `requests_total`

func WithLatencyHistogram ¶

func WithLatencyHistogram(name string, buckets []float64) option

WithLatencyHistogram creates a latency histogram collector with a given name

func WithNamespace ¶

func WithNamespace(namespace string) option

WithNamespace sets the namespace of the prometheus collectors

func WithRequestCounter ¶

func WithRequestCounter(name string) option

WithRequestCounter creates a new request counter collector with a given name

func WithSkipFunc ¶

func WithSkipFunc(skipFunc SkipFunc) option

func WithSubsystem ¶

func WithSubsystem(name string) option

WithSubsystem sets the subsystem of the prometheus collectors

Types ¶

type Middleware ¶

type Middleware func(next http.Handler) http.Handler

Middleware is responsible for creating a middleware handler func

func Default ¶

func Default() Middleware

Default creates a new middleware with default collectors attached

func New ¶

func New(options ...option) Middleware

New creates a new prometheus middleware from given options. Note: the middleware has no prometheus collectors attached per default. If you want the default collectors, use Default

type SkipFunc ¶

type SkipFunc func(r *http.Request) bool

SkipFunc allows for certain requests to be skipped based on information obtained from the http.Request, e.g. URLPath.

Directories ¶

Path Synopsis
examples
std command

Jump to

Keyboard shortcuts

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