gitlab.com/gitlab-org/labkit@v1.21.0/metrics/examples_test.go (about)

     1  package metrics_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"gitlab.com/gitlab-org/labkit/metrics"
     9  )
    10  
    11  func ExampleNewHandlerFactory() {
    12  	// Tell prometheus to include a "route" label for the http metrics
    13  	newMetricHandlerFunc := metrics.NewHandlerFactory(metrics.WithLabels("route"))
    14  
    15  	http.Handle("/foo",
    16  		newMetricHandlerFunc(
    17  			http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    18  				fmt.Fprintf(w, "Hello foo")
    19  			}),
    20  			metrics.WithLabelValues(map[string]string{"route": "/foo"}), // Add instrumentation with a custom label of route="/foo"
    21  		))
    22  
    23  	http.Handle("/bar",
    24  		newMetricHandlerFunc(
    25  			http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    26  				fmt.Fprintf(w, "Hello bar")
    27  			}),
    28  			metrics.WithLabelValues(map[string]string{"route": "/bar"}), // Add instrumentation with a custom label of route="/bar"
    29  		))
    30  
    31  	log.Fatal(http.ListenAndServe(":8080", nil))
    32  }