-
Notifications
You must be signed in to change notification settings - Fork 90
/
options.go
51 lines (41 loc) · 1.07 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
package expmetric
// Options describes the optional configuration for the expmetric middleware.
type Options struct {
MetricName string `json:"metric_name" yaml:"MetricName"`
Resolution int `json:"resolution" yaml:"Resolution"`
AvgDiv int64 `json:"avg_div" yaml:"AvgDiv"`
}
func (opts Options) avgEnabled() bool {
return opts.AvgDiv > 0
}
type Option func(*Options)
// MetricName sets the name of the metric.
func MetricName(name string) Option {
return func(opts *Options) {
opts.MetricName = name
}
}
// Resolution sets the counter's rate resolution.
func Resolution(n int) Option {
return func(opts *Options) {
opts.Resolution = n
}
}
// Avg sets the dividend. If more than zero then an "$metric_name_avg" expvar is registered.
func Avg(n int64) Option {
return func(opts *Options) {
opts.AvgDiv = n
}
}
func applyOptions(options []Option) (opts Options) {
for _, fn := range options {
if fn == nil {
continue
}
fn(&opts)
}
if opts.Resolution <= 0 {
opts.Resolution = 20 // Default value under the hoods of the ratecounter pkg.
}
return
}