-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
71 lines (60 loc) · 1.74 KB
/
config.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
package metrics
import (
"fmt"
"net/http"
"sort"
"strings"
)
var Options singleton
type singleton struct{}
type option func(*configuration)
type configuration struct {
Name string
Description string
Labels map[string]string
Exporter Exporter
Buckets []uint64
}
func (singleton) Description(value string) option {
return func(this *configuration) { this.Description = value }
}
func (singleton) Label(key, value string) option {
return func(this *configuration) { this.Labels[key] = value }
}
func (singleton) Exporter(value Exporter) option {
return func(this *configuration) { this.Exporter = value }
}
func (singleton) Bucket(values ...uint64) option {
return func(this *configuration) {
this.Buckets = append(this.Buckets, values...)
sort.Slice(this.Buckets, func(i, j int) bool { return this.Buckets[i] < this.Buckets[j] })
}
}
func (singleton) apply(options ...option) option {
return func(this *configuration) {
this.Labels = map[string]string{}
for _, item := range Options.defaults(options...) {
item(this)
}
}
}
func (singleton) defaults(options ...option) []option {
return append([]option{
Options.Exporter(nop{}),
}, options...)
}
///////////////////////////////////////////////////////////////////////////////
func (this configuration) RenderLabels() (result string) {
if len(this.Labels) == 0 {
return ""
}
for key, value := range this.Labels {
result += fmt.Sprintf(`%s="%s", `, key, value)
}
result = strings.TrimSuffix(result, ", ")
return fmt.Sprintf("{ %s }", result)
}
///////////////////////////////////////////////////////////////////////////////
type nop struct{}
func (nop) Add(...Metric) {}
func (nop) ServeHTTP(http.ResponseWriter, *http.Request) {}