fasthttpprom

package module
v1.0.17 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

fasthttp prometheus-middleware

Prometheus middleware for fasthttp

Exports metrics for request duration request_duration_seconds with http status code as code and http request method + endpoint/route as path f.e code="200",path="GET_/health", code="201",path="POST_/foo"

Example

using fasthttp/router

package main

import (
"log"

fasthttpprom "github.com/carousell/fasthttp-prometheus-middleware"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)

func main() {

	r := router.New()
	p := fasthttpprom.NewPrometheus("")
	p.Use(r)

	r.GET("/health", func(ctx *fasthttp.RequestCtx) {
		ctx.SetStatusCode(200)
		ctx.SetBody([]byte(`{"status": "pass"}`))
		log.Println(string(ctx.Request.URI().Path()))
	})

	log.Println("main is listening on ", "8080")
	log.Fatal(fasthttp.ListenAndServe(":"+"8080", p.Handler))

}

Example metrics for above code in /metrics endpoint

request_duration_seconds_bucket{code="200",path="GET_/health",le="0.01"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.02"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.04"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.06"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.08"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.1"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.15"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.25"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.4"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.6"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="0.8"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="1"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="1.5"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="2"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="3"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="5"} 25063
request_duration_seconds_bucket{code="200",path="GET_/health",le="+Inf"} 25063
request_duration_seconds_sum{code="200",path="GET_/health"} 0.14781658099999923
request_duration_seconds_count{code="200",path="GET_/health"} 25063

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListenerHandler

type ListenerHandler func(c *fasthttp.RequestCtx) string

ListenerHandler url label

type Prometheus

type Prometheus struct {
	MetricsPath string
	Handler     fasthttp.RequestHandler
	// contains filtered or unexported fields
}

Prometheus contains the metrics gathered by the instance and its path

func NewPrometheus

func NewPrometheus(subsystem string) *Prometheus

NewPrometheus generates a new set of metrics with a certain subsystem name using the default registry.

func NewPrometheusWithRegistry added in v1.0.17

func NewPrometheusWithRegistry(subsystem string, reg *prometheus.Registry) *Prometheus

NewPrometheusWithRegistry is like NewPrometheus but registers and serves metrics from the given registry.

func (*Prometheus) Custom

func (p *Prometheus) Custom(r *router.Router)

Custom adds the middleware to a fasthttp. If a listen address is set, /metrics is served only from that address and never from the application router.

func (*Prometheus) HandlerFunc

func (p *Prometheus) HandlerFunc() fasthttp.RequestHandler

HandlerFunc is onion or wraper to handler for fasthttp listenandserve

func (*Prometheus) SetListenAddress

func (p *Prometheus) SetListenAddress(address string)

SetListenAddress for exposing metrics on address. If not set, it will be exposed at the same address of api that is being used

func (*Prometheus) SetListenAddressWithRouter

func (p *Prometheus) SetListenAddressWithRouter(listenAddress string, r *router.Router)

SetListenAddressWithRouter for using a separate router to expose metrics. (this keeps things like GET /metrics out of your content's access log).

func (*Prometheus) SetMetricsPath

func (p *Prometheus) SetMetricsPath(r *router.Router)

SetMetricsPath registers the metrics handler on r. If a listen address is configured it also starts a server for r on that address.

func (*Prometheus) SetPathGrouping

func (p *Prometheus) SetPathGrouping(enabled bool)

SetPathGrouping labels metrics with the matched route pattern instead of the raw request path. It is enabled by default; disabling it on an internet-facing service allows unbounded label cardinality.

func (*Prometheus) Shutdown added in v1.0.17

func (p *Prometheus) Shutdown() error

Shutdown stops the separate metrics server, if one was started.

func (*Prometheus) Use

func (p *Prometheus) Use(r *router.Router)

Use adds the middleware to a fasthttp

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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