Documentation ¶
Overview ¶
Package httpmetrics provides middleware for collecting metrics about http servers.
Metrics are prefixed with:
http.server.<method>.<normalized path> http.server.all
For example, a request to GET /apps/:foo/bars/:bar_id emits metrics prefixed with:
http.server.get.apps.foo.bars.bar-id http.server.all
For each unique path, and under the global all prefix, servers will report:
requests - counter of requests request-duration.ms - histogram of request durations in milliseconds response-statuses.<status code> - counter of response status codes, eg 200
Example ¶
This example demonstrates how to set up metrics and what will be collected on requests
package main import ( "fmt" "net/http" "net/http/httptest" "testing" "github.com/go-chi/chi" "github.com/heroku/x/go-kit/metrics/testmetrics" "github.com/heroku/x/hmiddleware/httpmetrics" ) func main() { // Create a new Metrics Provider provider := testmetrics.NewProvider(&testing.T{}) r := chi.NewRouter() // Use the Metrics Provider to create a new HTTP Handler r.Use(httpmetrics.New(provider)) // Metrics will be collected around http OK statuses // For all requests and for /foo requests r.Get("/foo", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })) // Metrics will be collected around http Bad Request statuses // For all requests and for /bar requests r.Get("/bar", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) })) server := httptest.NewServer(r) defer server.Close() // Metrics will be collected for each request req, _ := http.NewRequest("GET", server.URL+"/foo", nil) if _, err := http.DefaultClient.Do(req); err != nil { fmt.Println(err) } req, _ = http.NewRequest("GET", server.URL+"/bar", nil) if _, err := http.DefaultClient.Do(req); err != nil { fmt.Println(err) } provider.PrintCounterValue("http.server.get.bar.response-statuses.400") provider.PrintCounterValue("http.server.all.requests") provider.PrintCounterValue("http.server.all.response-statuses.200") provider.PrintCounterValue("http.server.get.foo.requests") provider.PrintCounterValue("http.server.get.foo.response-statuses.200") provider.PrintCounterValue("http.server.all.response-statuses.400") provider.PrintCounterValue("http.server.get.bar.requests") }
Output: http.server.get.bar.response-statuses.400: 1 http.server.all.requests: 2 http.server.all.response-statuses.200: 1 http.server.get.foo.requests: 1 http.server.get.foo.response-statuses.200: 1 http.server.all.response-statuses.400: 1 http.server.get.bar.requests: 1
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶ added in v0.0.17
New returns an HTTP middleware which captures request metrics and reports them to the given provider.
func NewOTEL ¶ added in v0.0.60
NewOTEL returns an HTTP middleware which captures HTTP request counts and latency annotated with attributes for method, route, status.
See https://opentelemetry.io/docs/specs/otel/metrics/semantic_conventions/http-metrics/
func NewServer
deprecated
Types ¶
This section is empty.