MCPcopy
hub / github.com/prometheus/client_golang / ExampleWithExtraMethods

Function ExampleWithExtraMethods

prometheus/promhttp/option_test.go:30–71  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28)
29
30func ExampleWithExtraMethods() {
31 counter := prometheus.NewCounterVec(
32 prometheus.CounterOpts{
33 Name: "api_requests_total",
34 Help: "A counter for requests to the wrapped handler.",
35 },
36 []string{"code", "method"},
37 )
38
39 // duration is partitioned by the HTTP method and handler. It uses custom
40 // buckets based on the expected request duration.
41 duration := prometheus.NewHistogramVec(
42 prometheus.HistogramOpts{
43 Name: "request_duration_seconds",
44 Help: "A histogram of latencies for requests.",
45 Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
46 },
47 []string{"handler", "method"},
48 )
49
50 // Create the handlers that will be wrapped by the middleware.
51 pullHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52 w.Write([]byte("Pull"))
53 })
54
55 // Specify additional HTTP methods to be added to the label allow list.
56 opts := WithExtraMethods("CUSTOM_METHOD")
57
58 // Instrument the handlers with all the metrics, injecting the "handler"
59 // label by currying.
60 pullChain := InstrumentHandlerDuration(duration.MustCurryWith(prometheus.Labels{"handler": "pull"}),
61 InstrumentHandlerCounter(counter, pullHandler, opts),
62 opts,
63 )
64
65 http.Handle("/metrics", Handler())
66 http.Handle("/pull", pullChain)
67
68 if err := http.ListenAndServe(":3000", nil); err != nil {
69 log.Fatal(err)
70 }
71}
72
73func ExampleWithLabelFromCtx() {
74 counter := prometheus.NewCounterVec(

Callers

nothing calls this directly

Calls 9

MustCurryWithMethod · 0.95
NewCounterVecFunction · 0.92
NewHistogramVecFunction · 0.92
WithExtraMethodsFunction · 0.85
InstrumentHandlerCounterFunction · 0.85
HandlerFunction · 0.85
HandleMethod · 0.80
WriteMethod · 0.65

Tested by

no test coverage detected