-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.go
69 lines (58 loc) · 1.24 KB
/
setup.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
package metrics
import (
"fmt"
"github.com/scalescape/go-metrics/common"
"github.com/scalescape/go-metrics/prometheus"
"github.com/scalescape/go-metrics/statsd"
)
type Option func(c *common.Config)
func WithAddress(addr string) Option {
return func(c *common.Config) {
c.Address = addr
}
}
func WithServiceName(s string) Option {
return func(c *common.Config) {
c.ServiceID = s
}
}
func WithPprof() Option {
return func(c *common.Config) {
c.EnablePprof = true
}
}
func WithKind(kind common.Kind) Option {
return func(c *common.Config) {
c.Kind = kind
}
}
var DefaultConfig = &common.Config{
Address: ":9100",
ServiceID: "go-service",
Kind: common.Prometheus,
}
func Setup(opts ...Option) (Observer, error) {
cfg := DefaultConfig
for _, o := range opts {
o(cfg)
}
var reporter reporter
var err error
switch cfg.Kind {
case common.Prometheus:
reporter, err = prometheus.Setup(*cfg)
if err != nil {
return Observer{}, fmt.Errorf("unable to setup prometheus: %w", err)
}
case common.Statsd:
reporter, err = statsd.Setup(*cfg)
if err != nil {
return Observer{}, fmt.Errorf("unable to setup influx: %w", err)
}
}
observer := Observer{
Config: *cfg,
reporter: reporter,
}
return observer, nil
}