| 1 |
// Copyright 2022 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package promhttp |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"log" |
| 19 |
"net/http" |
| 20 |
|
| 21 |
"github.com/prometheus/client_golang/prometheus" |
| 22 |
) |
| 23 |
|
| 24 |
type key int |
| 25 |
|
| 26 |
const ( |
| 27 |
CtxResolverKey key = iota |
| 28 |
) |
| 29 |
|
| 30 |
func 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 |
|
| 73 |
func ExampleWithLabelFromCtx() { |
| 74 |
counter := prometheus.NewCounterVec( |
| 75 |
prometheus.CounterOpts{ |
| 76 |
Name: "api_requests_total", |
| 77 |
Help: "A counter for requests to the wrapped handler.", |
| 78 |
}, |
| 79 |
[]string{"code", "method", "myheader"}, |
| 80 |
) |
| 81 |
|
| 82 |
// duration is partitioned by the HTTP method, handler and request header |
| 83 |
// value. It uses custom buckets based on the expected request duration. |
| 84 |
// Beware to not have too high cardinality on the values of header. You |
| 85 |
// always should sanitize external inputs. |
| 86 |
duration := prometheus.NewHistogramVec( |
| 87 |
prometheus.HistogramOpts{ |
| 88 |
Name: "request_duration_seconds", |
| 89 |
Help: "A histogram of latencies for requests.", |
| 90 |
Buckets: []float64{.25, .5, 1, 2.5, 5, 10}, |
| 91 |
}, |
| 92 |
[]string{"handler", "method", "myheader"}, |
| 93 |
) |
| 94 |
|
| 95 |
// Create the handlers that will be wrapped by the middleware. |
| 96 |
pullHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 97 |
w.Write([]byte("Pull")) |
| 98 |
}) |
| 99 |
|
| 100 |
// Specify additional HTTP methods to be added to the label allow list. |
| 101 |
opts := WithLabelFromCtx("myheader", |
| 102 |
func(ctx context.Context) string { |
| 103 |
return ctx.Value(CtxResolverKey).(string) |
| 104 |
}, |
| 105 |
) |
| 106 |
|
| 107 |
// Instrument the handlers with all the metrics, injecting the "handler" |
| 108 |
// label by currying. |
| 109 |
pullChain := InstrumentHandlerDuration(duration.MustCurryWith(prometheus.Labels{"handler": "pull"}), |
| 110 |
InstrumentHandlerCounter(counter, pullHandler, opts), |
| 111 |
opts, |
| 112 |
) |
| 113 |
|
| 114 |
middleware := func(next http.HandlerFunc) http.HandlerFunc { |
| 115 |
return func(w http.ResponseWriter, r *http.Request) { |
| 116 |
ctx := context.WithValue(r.Context(), CtxResolverKey, r.Header.Get("x-my-header")) |
| 117 |
|
| 118 |
next(w, r.WithContext(ctx)) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
http.Handle("/metrics", Handler()) |
| 123 |
http.Handle("/pull", middleware(pullChain)) |
| 124 |
|
| 125 |
if err := http.ListenAndServe(":3000", nil); err != nil { |
| 126 |
log.Fatal(err) |
| 127 |
} |
| 128 |
} |