forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
111 lines (97 loc) · 3.36 KB
/
metrics.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
package metrics
import (
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics/dogstatsd"
"github.com/go-kit/kit/metrics/graphite"
"github.com/go-kit/kit/metrics/provider"
"github.com/go-kit/kit/metrics/statsd"
"github.com/NYTimes/gizmo/config"
)
// Type acts as an 'enum' type to represent
// the available metrics providers
type Type string
const (
// Statsd is used by config to indicate use of the statsdProvider.
Statsd Type = "statsd"
// DogStatsd is used by config to indicate use of the dogstatsdProvider.
DogStatsd Type = "dogstatsd"
// Prometheus is used by config to indicate use of the prometheusProvider.
Prometheus Type = "prometheus"
// Graphite is used by config to indicate use of the graphiteProvider.
Graphite Type = "graphite"
// Expvar is used by config to indicate use of the expvarProvider.
Expvar Type = "expvar"
// Discard is used by config to indicate use of the discardProvider.
Discard Type = "discard"
)
// Config can be used to configure and instantiate a new
// go-kit/kit/metrics/provider.Provider.
type Config struct {
// if empty, will server default to "expvar"
Type Type `envconfig:"METRICS_TYPE"`
// Prefix will be prefixed onto
// any metric name.
Prefix string `envconfig:"METRICS_PREFIX"`
// Namespace is used by prometheus.
Namespace string `envconfig:"METRICS_NAMESPACE"`
// Subsystem is used by prometheus.
Subsystem string `envconfig:"METRICS_SUBSYSTEM"`
// Used by statsd, graphite and dogstatsd
Interval time.Duration `envconfig:"METRICS_INTERVAL"`
// Used by statsd, graphite and dogstatsd.
Addr string `envconfig:"METRICS_ADDR"`
// Used by statsd, graphite and dogstatsd to dial a connection.
// If empty, will default to "udp".
Network string `envconfig:"METRICS_NETWORK"`
// Used by expvar only.
// if empty, will default to "/debug/vars"
Path string `envconfig:"METRICS_PATH"`
// Used by graphite only.
// If none provided, kit/log/NewNopLogger will be used.
Logger log.Logger
}
// LoadConfigFromEnv will attempt to load a Metrics object
// from environment variables.
func LoadConfigFromEnv() Config {
var mets Config
config.LoadEnvConfig(&mets)
return mets
}
// NewProvider will use the values in the Metrics config object
// to generate a new go-kit/metrics/provider.Provider implementation.
// If no type is given, a no-op implementation will be used.
func (cfg Config) NewProvider() provider.Provider {
if cfg.Logger == nil {
cfg.Logger = log.NewNopLogger()
}
if cfg.Path == "" {
cfg.Path = "/debug/vars"
}
if cfg.Interval == 0 {
cfg.Interval = time.Second * 30
}
switch cfg.Type {
case Statsd:
stsd := statsd.New(cfg.Prefix, cfg.Logger)
tick := time.NewTicker(cfg.Interval)
go stsd.SendLoop(tick.C, cfg.Network, cfg.Addr)
return provider.NewStatsdProvider(stsd, tick.Stop)
case DogStatsd:
stsd := dogstatsd.New(cfg.Prefix, cfg.Logger)
tick := time.NewTicker(cfg.Interval)
go stsd.SendLoop(tick.C, cfg.Network, cfg.Addr)
return provider.NewDogstatsdProvider(stsd, tick.Stop)
case Graphite:
grpht := graphite.New(cfg.Prefix, cfg.Logger)
tick := time.NewTicker(cfg.Interval)
go grpht.SendLoop(tick.C, cfg.Network, cfg.Addr)
return provider.NewGraphiteProvider(grpht, tick.Stop)
case Prometheus:
return provider.NewPrometheusProvider(cfg.Namespace, cfg.Subsystem)
case Expvar:
return provider.NewExpvarProvider()
default:
return provider.NewDiscardProvider()
}
}