-
Notifications
You must be signed in to change notification settings - Fork 38
/
options.go
160 lines (142 loc) · 4.85 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package ginprom
import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type PrometheusOption func(*Prometheus)
// Path is an option allowing to set the metrics path when initializing with New.
func Path(path string) PrometheusOption {
return func(p *Prometheus) {
p.MetricsPath = path
}
}
// Ignore is used to disable instrumentation on some routes.
func Ignore(paths ...string) PrometheusOption {
return func(p *Prometheus) {
p.Ignored.Lock()
defer p.Ignored.Unlock()
for _, path := range paths {
p.Ignored.values[path] = true
}
}
}
// BucketSize is used to define the default bucket size when initializing with
// New.
func BucketSize(b []float64) PrometheusOption {
return func(p *Prometheus) {
p.BucketsSize = b
}
}
// Subsystem is an option allowing to set the subsystem when initializing
// with New.
func Subsystem(sub string) PrometheusOption {
return func(p *Prometheus) {
p.Subsystem = sub
}
}
// Namespace is an option allowing to set the namespace when initializing
// with New.
func Namespace(ns string) PrometheusOption {
return func(p *Prometheus) {
p.Namespace = ns
}
}
// Token is an option allowing to set the bearer token in prometheus
// with New.
// Example: ginprom.New(ginprom.Token("your_custom_token"))
func Token(token string) PrometheusOption {
return func(p *Prometheus) {
p.Token = token
}
}
// RequestCounterMetricName is an option allowing to set the request counter metric name.
func RequestCounterMetricName(reqCntMetricName string) PrometheusOption {
return func(p *Prometheus) {
p.RequestCounterMetricName = reqCntMetricName
}
}
// RequestDurationMetricName is an option allowing to set the request duration metric name.
func RequestDurationMetricName(reqDurMetricName string) PrometheusOption {
return func(p *Prometheus) {
p.RequestDurationMetricName = reqDurMetricName
}
}
// RequestSizeMetricName is an option allowing to set the request size metric name.
func RequestSizeMetricName(reqSzMetricName string) PrometheusOption {
return func(p *Prometheus) {
p.RequestSizeMetricName = reqSzMetricName
}
}
// ResponseSizeMetricName is an option allowing to set the response size metric name.
func ResponseSizeMetricName(resDurMetricName string) PrometheusOption {
return func(p *Prometheus) {
p.ResponseSizeMetricName = resDurMetricName
}
}
// Engine is an option allowing to set the gin engine when intializing with New.
// Example:
// r := gin.Default()
// p := ginprom.New(Engine(r))
func Engine(e *gin.Engine) PrometheusOption {
return func(p *Prometheus) {
p.Engine = e
}
}
// Registry is an option allowing to set a *prometheus.Registry with New.
// Use this option if you want to use a custom Registry instead of a global one that prometheus
// client uses by default
// Example:
// r := gin.Default()
// p := ginprom.New(Registry(r))
func Registry(r *prometheus.Registry) PrometheusOption {
return func(p *Prometheus) {
p.Registry = r
}
}
// HandlerNameFunc is an option allowing to set the HandlerNameFunc with New.
// Use this option if you want to override the default behavior (i.e. using
// (*gin.Context).HandlerName). This is useful when wanting to group different
// functions under the same "handler" label or when using gin with decorated handlers
// Example:
// r := gin.Default()
// p := ginprom.New(HandlerNameFunc(func (c *gin.Context) string { return "my handler" }))
func HandlerNameFunc(f func(c *gin.Context) string) PrometheusOption {
return func(p *Prometheus) {
p.HandlerNameFunc = f
}
}
// HandlerOpts is an option allowing to set the promhttp.HandlerOpts.
// Use this option if you want to override the default zero value.
func HandlerOpts(opts promhttp.HandlerOpts) PrometheusOption {
return func(p *Prometheus) {
p.HandlerOpts = opts
}
}
// RequestPathFunc is an option allowing to set the RequestPathFunc with New.
// Use this option if you want to override the default behavior (i.e. using
// (*gin.Context).FullPath). This is useful when wanting to group different requests
// under the same "path" label or when wanting to process unknown routes (the default
// (*gin.Context).FullPath return an empty string for unregistered routes). Note that
// requests for which f returns the empty string are ignored.
// To specifically ignore certain paths, see the Ignore option.
// Example:
//
// r := gin.Default()
// p := ginprom.New(RequestPathFunc(func (c *gin.Context) string {
// if fullpath := c.FullPath(); fullpath != "" {
// return fullpath
// }
// return "<unknown>"
// }))
func RequestPathFunc(f func(c *gin.Context) string) PrometheusOption {
return func(p *Prometheus) {
p.RequestPathFunc = f
}
}
func CustomCounterLabels(labels []string, f func(c *gin.Context) map[string]string) PrometheusOption {
return func(p *Prometheus) {
p.customCounterLabelsProvider = f
p.customCounterLabels = labels
}
}