HTTP Handler Instrumentation
This outline describes how to instrument HTTP handlers using go-metrics
, capturing performance data for your HTTP endpoints.
HTTP Handler Instrumentation:
Motivation: Instrument HTTP handlers to capture performance data for your HTTP endpoints.
Usage:
1. Instrument the HTTP Handler:
import ( "net/http" "github.com/docker/go-metrics" ) func main() { // Create a new Registry registry := metrics.NewRegistry() // Instrument the HTTP handler http.Handle("/myendpoint", metrics.InstrumentedHandler(registry, "myendpoint", http.HandlerFunc(myHandler))) // Start the server http.ListenAndServe(":8080", nil) } func myHandler(w http.ResponseWriter, r *http.Request) { // Handle the request // ... }
2. Instrument the HTTP Handler with Custom Tags:
import ( "net/http" "github.com/docker/go-metrics" ) func main() { // Create a new Registry registry := metrics.NewRegistry() // Instrument the HTTP handler with custom tags http.Handle("/myendpoint", metrics.InstrumentedHandler(registry, "myendpoint", http.HandlerFunc(myHandler), metrics.WithTags("version", "v1"))) // Start the server http.ListenAndServe(":8080", nil) } func myHandler(w http.ResponseWriter, r *http.Request) { // Handle the request // ... }
3. Instrument the HTTP Handler with Custom Metrics:
import ( "net/http" "github.com/docker/go-metrics" ) func main() { // Create a new Registry registry := metrics.NewRegistry() // Instrument the HTTP handler with custom metrics http.Handle("/myendpoint", metrics.InstrumentedHandler(registry, "myendpoint", http.HandlerFunc(myHandler), metrics.WithMetrics("requests", "errors"))) // Start the server http.ListenAndServe(":8080", nil) } func myHandler(w http.ResponseWriter, r *http.Request) { // Handle the request // ... }
4. Instrument the HTTP Handler with Custom Metrics and Tags:
import ( "net/http" "github.com/docker/go-metrics" ) func main() { // Create a new Registry registry := metrics.NewRegistry() // Instrument the HTTP handler with custom metrics and tags http.Handle("/myendpoint", metrics.InstrumentedHandler(registry, "myendpoint", http.HandlerFunc(myHandler), metrics.WithMetrics("requests", "errors"), metrics.WithTags("version", "v1"))) // Start the server http.ListenAndServe(":8080", nil) } func myHandler(w http.ResponseWriter, r *http.Request) { // Handle the request // ... }