chiprometheus

package module
v0.0.0-...-2ea12fb Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

chi-prometheus

Prometheus middleware for chi.

This is a port of negroni-prometheus middleware (written by Rene Zbinden).

Why

Logging v. instrumentation

Instead of logging request times, it is considered best practice to provide an endpoint for instrumentation tools (like prometheus).

Installation

go get github.com/766b/chi-prometheus

Usage

Take a look at the example.

What do you get

An endpoint with the following information (stripped output):

# HELP chi_request_duration_milliseconds How long it took to process the request, partitioned by status code, method and HTTP path.
# TYPE chi_request_duration_milliseconds histogram
chi_request_duration_milliseconds_bucket{code="OK",method="GET",path="/metrics",service="serviceName",le="300"} 1
chi_request_duration_milliseconds_bucket{code="OK",method="GET",path="/metrics",service="serviceName",le="1200"} 1
chi_request_duration_milliseconds_bucket{code="OK",method="GET",path="/metrics",service="serviceName",le="5000"} 1
chi_request_duration_milliseconds_bucket{code="OK",method="GET",path="/metrics",service="serviceName",le="+Inf"} 1
chi_request_duration_milliseconds_sum{code="OK",method="GET",path="/metrics",service="serviceName"} 2.003123
chi_request_duration_milliseconds_count{code="OK",method="GET",path="/metrics",service="serviceName"} 1
chi_request_duration_milliseconds_bucket{code="OK",method="GET",path="/ok",service="serviceName",le="300"} 0
chi_request_duration_milliseconds_bucket{code="OK",method="GET",path="/ok",service="serviceName",le="1200"} 0
chi_request_duration_milliseconds_bucket{code="OK",method="GET",path="/ok",service="serviceName",le="5000"} 2
chi_request_duration_milliseconds_bucket{code="OK",method="GET",path="/ok",service="serviceName",le="+Inf"} 2
chi_request_duration_milliseconds_sum{code="OK",method="GET",path="/ok",service="serviceName"} 4747.529026
chi_request_duration_milliseconds_count{code="OK",method="GET",path="/ok",service="serviceName"} 2
# HELP chi_requests_total How many HTTP requests processed, partitioned by status code, method and HTTP path.
# TYPE chi_requests_total counter
chi_requests_total{code="OK",method="GET",path="/metrics",service="serviceName"} 1
chi_requests_total{code="OK",method="GET",path="/ok",service="serviceName"} 2

Pattern Middleware Usage

Take a look at the example.

What do you get

An endpoint with the following information (stripped output):

# HELP chi_pattern_request_duration_milliseconds How long it took to process the request, partitioned by status code, method and HTTP path (with patterns).
# TYPE chi_pattern_request_duration_milliseconds histogram
chi_pattern_request_duration_milliseconds_bucket{code="OK",method="GET",path="/metrics",service="test_service",le="300"} 1
chi_pattern_request_duration_milliseconds_bucket{code="OK",method="GET",path="/metrics",service="test_service",le="1200"} 1
chi_pattern_request_duration_milliseconds_bucket{code="OK",method="GET",path="/metrics",service="test_service",le="5000"} 1
chi_pattern_request_duration_milliseconds_bucket{code="OK",method="GET",path="/metrics",service="test_service",le="+Inf"} 1
chi_pattern_request_duration_milliseconds_sum{code="OK",method="GET",path="/metrics",service="test_service"} 0.975926
chi_pattern_request_duration_milliseconds_count{code="OK",method="GET",path="/metrics",service="test_service"} 1
chi_pattern_request_duration_milliseconds_bucket{code="OK",method="GET",path="/users/{firstName}",service="test_service",le="300"} 0
chi_pattern_request_duration_milliseconds_bucket{code="OK",method="GET",path="/users/{firstName}",service="test_service",le="1200"} 0
chi_pattern_request_duration_milliseconds_bucket{code="OK",method="GET",path="/users/{firstName}",service="test_service",le="5000"} 2
chi_pattern_request_duration_milliseconds_bucket{code="OK",method="GET",path="/users/{firstName}",service="test_service",le="+Inf"} 2
chi_pattern_request_duration_milliseconds_sum{code="OK",method="GET",path="/users/{firstName}",service="test_service"} 4755.052755
chi_pattern_request_duration_milliseconds_count{code="OK",method="GET",path="/users/{firstName}",service="test_service"} 2
# HELP chi_pattern_requests_total How many HTTP requests processed, partitioned by status code, method and HTTP path (with patterns).
# TYPE chi_pattern_requests_total counter
chi_pattern_requests_total{code="OK",method="GET",path="/metrics",service="test_service"} 1
chi_pattern_requests_total{code="OK",method="GET",path="/users/{firstName}",service="test_service"} 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(name string, buckets ...float64) func(next http.Handler) http.Handler

NewMiddleware returns a new prometheus Middleware handler.

func NewPatternMiddleware

func NewPatternMiddleware(name string, buckets ...float64) func(next http.Handler) http.Handler

NewPatternMiddleware returns a new prometheus Middleware handler that groups requests by the chi routing pattern. EX: /users/{firstName} instead of /users/bob

Types

type Middleware

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

Middleware is a handler that exposes prometheus metrics for the number of requests, the latency and the response size, partitioned by status code, method and HTTP path.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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