Documentation
¶
Overview ¶
Package prom exports servicekit's connection and HTTP metrics to Prometheus.
ConnMetrics implements http.ConnObserver, so one value set on WSConnConfig.Observer, SSEConnConfig.Observer or grpcws.ConnOptions.Observer counts every connection those serve. HTTPMetrics is middleware for the request side. Serve both with promhttp.Handler (or promhttp.HandlerFor a custom registry) on /metrics.
servicekit/http and servicekit/middleware do not import this package, so a server that doesn't use it links no Prometheus code. An app that already uses OpenTelemetry metrics can implement http.ConnObserver over its own meter instead.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DurationBuckets = prometheus.ExponentialBuckets(0.1, 4, 10)
DurationBuckets are the connection-lifetime histogram buckets, from 100ms to about 7 hours.
Functions ¶
This section is empty.
Types ¶
type ConnMetrics ¶
type ConnMetrics struct {
// contains filtered or unexported fields
}
ConnMetrics records connection and message counts for WebSocket, SSE and grpcws connections. Every series is labelled with transport ("ws", "sse", "grpcws") and mode (the grpcws stream kind, empty otherwise):
servicekit_connections_active gauge servicekit_connections_opened_total counter servicekit_connections_closed_total counter, plus reason servicekit_connection_duration_seconds histogram servicekit_messages_sent_total counter, plus kind servicekit_messages_received_total counter
reason is a gohttp.CloseReason, so a client dropped for not reading shows up as reason="write_timeout" and a grpcws CloseOnStreamEnd close as reason="server_close". Labels never carry per-connection values, so the series count stays small.
It is safe for concurrent use.
func NewConnMetrics ¶
func NewConnMetrics(reg prometheus.Registerer) *ConnMetrics
NewConnMetrics creates the metrics and registers them with reg, or with prometheus.DefaultRegisterer when reg is nil. It panics if they are already registered there, so create one per registry and share it.
func (*ConnMetrics) ConnClosed ¶
func (m *ConnMetrics) ConnClosed(info gohttp.ConnInfo, reason gohttp.CloseReason, lifetime time.Duration)
ConnClosed implements gohttp.ConnObserver.
func (*ConnMetrics) ConnOpened ¶
func (m *ConnMetrics) ConnOpened(info gohttp.ConnInfo)
ConnOpened implements gohttp.ConnObserver.
func (*ConnMetrics) MessageReceived ¶
func (m *ConnMetrics) MessageReceived(info gohttp.ConnInfo)
MessageReceived implements gohttp.ConnObserver.
func (*ConnMetrics) MessageSent ¶
func (m *ConnMetrics) MessageSent(info gohttp.ConnInfo, kind string)
MessageSent implements gohttp.ConnObserver.
type HTTPMetrics ¶
type HTTPMetrics struct {
// Route returns the route label for a request. Nil uses the mux template.
// It must return a small, fixed set of values.
Route func(r *http.Request) string
// contains filtered or unexported fields
}
HTTPMetrics is request middleware recording rate, errors and duration:
servicekit_http_requests_total counter method, route, code servicekit_http_request_duration_seconds histogram method, route servicekit_http_request_size_bytes histogram method, route servicekit_http_response_size_bytes histogram method, route
route is the matched gorilla/mux path template ("/v1/{gameId}/subscribe"), not the raw path, so ids in the URL don't create a series each. That needs the middleware installed with router.Use, inside the router, where the match is known. Outside a mux router every request is route="unmatched" unless HTTPMetrics.Route says otherwise.
A request the handler hijacks (a WebSocket upgrade) is counted with code "101" but left out of the duration and size histograms, since its duration is the connection's lifetime. ConnMetrics covers those.
func NewHTTPMetrics ¶
func NewHTTPMetrics(reg prometheus.Registerer) *HTTPMetrics
NewHTTPMetrics creates the metrics and registers them with reg, or with prometheus.DefaultRegisterer when reg is nil. It panics if they are already registered there.
func (*HTTPMetrics) Middleware ¶
func (m *HTTPMetrics) Middleware(next http.Handler) http.Handler
Middleware wraps next. Its signature matches mux.MiddlewareFunc.