httpmetrics

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

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

func New(p metrics.Provider) func(http.Handler) http.Handler

New returns an HTTP middleware which captures request metrics and reports them to the given provider.

func NewOTEL added in v0.0.60

func NewOTEL(p metrics.Provider) func(http.Handler) http.Handler

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

func NewServer(p metrics.Provider, next http.Handler) http.Handler

NewServer returns an http.Handler which calls next for each request and reports metrics to the given provider.

Deprecated: NewServer is awkward to use since it doesn't follow the normal pattern for middleware. Use New instead.

Types

This section is empty.

Jump to

Keyboard shortcuts

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