Debian Codegraph @

prometheus/promhttp/option.go

raw
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
19 "github.com/prometheus/client_golang/prometheus"
20 )
21
22 // Option are used to configure both handler (middleware) or round tripper.
23 type Option interface {
24 apply(*options)
25 }
26
27 // LabelValueFromCtx are used to compute the label value from request context.
28 // Context can be filled with values from request through middleware.
29 type LabelValueFromCtx func(ctx context.Context) string
30
31 // options store options for both a handler or round tripper.
32 type options struct {
33 extraMethods []string
34 getExemplarFn func(requestCtx context.Context) prometheus.Labels
35 extraLabelsFromCtx map[string]LabelValueFromCtx
36 }
37
38 func defaultOptions() *options {
39 return &options{
40 getExemplarFn: func(ctx context.Context) prometheus.Labels { return nil },
41 extraLabelsFromCtx: map[string]LabelValueFromCtx{},
42 }
43 }
44
45 func (o *options) emptyDynamicLabels() prometheus.Labels {
46 labels := prometheus.Labels{}
47
48 for label := range o.extraLabelsFromCtx {
49 labels[label] = ""
50 }
51
52 return labels
53 }
54
55 type optionApplyFunc func(*options)
56
57 func (o optionApplyFunc) apply(opt *options) { o(opt) }
58
59 // WithExtraMethods adds additional HTTP methods to the list of allowed methods.
60 // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods for the default list.
61 //
62 // See the example for ExampleInstrumentHandlerWithExtraMethods for example usage.
63 func WithExtraMethods(methods ...string) Option {
64 return optionApplyFunc(func(o *options) {
65 o.extraMethods = methods
66 })
67 }
68
69 // WithExemplarFromContext allows to inject function that will get exemplar from context that will be put to counter and histogram metrics.
70 // If the function returns nil labels or the metric does not support exemplars, no exemplar will be added (noop), but
71 // metric will continue to observe/increment.
72 func WithExemplarFromContext(getExemplarFn func(requestCtx context.Context) prometheus.Labels) Option {
73 return optionApplyFunc(func(o *options) {
74 o.getExemplarFn = getExemplarFn
75 })
76 }
77
78 // WithLabelFromCtx registers a label for dynamic resolution with access to context.
79 // See the example for ExampleInstrumentHandlerWithLabelResolver for example usage
80 func WithLabelFromCtx(name string, valueFn LabelValueFromCtx) Option {
81 return optionApplyFunc(func(o *options) {
82 o.extraLabelsFromCtx[name] = valueFn
83 })
84 }