-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
61 lines (52 loc) · 1.64 KB
/
build.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
package bprometheus
import (
"container/list"
"github.com/go-masonry/mortar/interfaces/monitor"
"github.com/prometheus/client_golang/prometheus"
)
type promConfig struct {
namespace string
predefinedCollectors []prometheus.Collector
}
type promBuilder struct {
ll *list.List
}
// PrometheusBuilder defines Prometheus builder
type PrometheusBuilder interface {
monitor.Builder
// SetNamespace allows to set a default Prometheus Namespace
SetNamespace(namespace string) PrometheusBuilder
// AddPredefinedCollectors allows to register predefined Collectors to the same Prometheus Registry
// *** Actual registration will occur only when the `monitor.BricksReporter.Connect(ctx)` is called ***
// *** Any error returned during registration will fail the `Connect` method.
AddPredefinedCollectors(collectors ...prometheus.Collector) PrometheusBuilder
}
// Builder creates a builder to create Prometheus client
func Builder() PrometheusBuilder {
return &promBuilder{
ll: list.New(),
}
}
func (s *promBuilder) SetNamespace(namespace string) PrometheusBuilder {
s.ll.PushBack(func(cfg *promConfig) {
cfg.namespace = namespace
})
return s
}
func (s *promBuilder) AddPredefinedCollectors(collectors ...prometheus.Collector) PrometheusBuilder {
s.ll.PushBack(func(cfg *promConfig) {
cfg.predefinedCollectors = append(cfg.predefinedCollectors, collectors...)
})
return s
}
func (s *promBuilder) Build() monitor.BricksReporter {
cfg := &promConfig{}
if s != nil {
for e := s.ll.Front(); e != nil; e = e.Next() {
f := e.Value.(func(config *promConfig))
f(cfg)
}
}
return newPromWrapper(cfg)
}
var _ monitor.Builder = (*promBuilder)(nil)