-
Notifications
You must be signed in to change notification settings - Fork 7
/
metrics.go
109 lines (85 loc) · 2.06 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
package ozone
import (
"context"
"os"
"strings"
"time"
"github.com/One-com/gone/log"
"github.com/One-com/gone/metric"
"github.com/One-com/gone/metric/sink/statsd"
"github.com/One-com/ozone/config"
)
// A metrics server implementing the gone/daemon. Server interface,
// but not using any file descriptors
type metricsService struct {
Addr string // statsd server to target.
Prefix string
Interval time.Duration // how often to push data to statsd
}
func loadMetricsConfig(cfg *config.MetricsConfig) (srv *metricsService, err error) {
if cfg == nil || cfg.Address == "" || cfg.Ident == "" {
return
}
app := cfg.Application
if app == "" {
app = "reverse-http-proxy"
}
ident := cfg.Ident
// guess my name if not set.
if len(ident) == 0 {
var e error
ident, e = os.Hostname()
if e != nil {
ident = "unknown"
} else {
parts := strings.Split(ident, ".")
ident = parts[0]
}
}
prefix := app + "." + ident
if cfg.Prefix != "" {
prefix = cfg.Prefix
}
srv = &metricsService{
Addr: cfg.Address,
Prefix: prefix,
Interval: cfg.Interval.Duration,
}
return
}
func (ms *metricsService) Description() string {
return "Metrics"
}
func (ms *metricsService) Serve(ctx context.Context) (err error) {
statsd_host := ms.Addr
statsd_interval := ms.Interval
if statsd_interval <= time.Second {
statsd_interval = time.Second
}
if statsd_host == "" {
log.Println("Not sending metrics")
return
}
var output statsd.Option
if statsd_host == "!" {
output = statsd.Output(os.Stdout)
} else {
output = statsd.Peer(statsd_host)
}
sink, err := statsd.New(
output,
statsd.Prefix(ms.Prefix),
statsd.Buffer(1432))
if err != nil {
log.ERROR("Error initializing statsd sink", "err", err)
return
}
log.Printf("Sending metrics (interval %s) for \"%s\" to %s\n", statsd_interval, ms.Prefix, statsd_host)
metric.SetDefaultOptions(metric.FlushInterval(statsd_interval))
// Activate draining metrics to statsd
metric.SetDefaultSink(sink)
metric.Start()
<-ctx.Done()
metric.Stop() // block until all have flushed
return
}