-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
219 lines (186 loc) · 5.14 KB
/
reporter.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package metrics
import (
"context"
"log"
"net/url"
"time"
client "github.com/influxdata/influxdb1-client"
"github.com/rcrowley/go-metrics"
)
var defaultReporter Reporter
// SetDefaultReporter sets the default reporter to be used for all metrics. It can be overwritten per Measurement.
func SetDefaultReporter(reporter Reporter) {
defaultReporter = reporter
}
// Reporter defines a metrics reporter. It is responsible for connection handling
// and sending data to it. It also holds the metrics registry all the metrics get registered to.
// Implementing the Reporter interface is useful for testing or changing the way the reporter behaves.
type Reporter interface {
Run()
Register(name string, metric Metric) error
Get(name string) (Metric, bool)
Tags() map[string]string
Stop()
}
type typeChecker func(m metric) bool
// NewReporter creates a new reporter which holds the influxDB connection and sends data to it.
func NewReporter(influxURL, database string, options ...ReporterOption) Reporter {
dbURL, err := url.Parse(influxURL)
if err != nil {
log.Printf("metrics.NewReporter: unable to parse InfluxDB url %s: %v", influxURL, err)
return nil
}
ctx, cancel := context.WithCancel(context.Background())
r := &reporter{
server: server{
URL: *dbURL,
DB: database,
},
registry: metrics.DefaultRegistry,
interval: 10 * time.Second,
ctx: ctx,
cancel: cancel,
}
for _, option := range options {
option(r)
}
return r
}
type server struct {
URL url.URL
DB string
User string
Pass string
}
// reporter implements a influxDB reporter. This is responsible for the influxDB connection
// and sending data to it. It also holds the metrics registry all the metrics get registered to.
type reporter struct {
registry metrics.Registry
client dbClient
server server
interval time.Duration
tags map[string]string
align bool
running bool
ctx context.Context
cancel context.CancelFunc
}
// Register registers a metric to the reporter. Data points from a registered
// metric will be collected via the AddPoints endpoint and then sent to influxDB.
//
// This function is only needed for custom metrics. Functions creating a metric
// in this package register themselves.
func (r *reporter) Register(name string, metric Metric) error {
return r.registry.Register(name, metric)
}
// Get returns a metric by name. Returns false if it does not exist or does not implement Metric.
func (r *reporter) Get(name string) (Metric, bool) {
m, ok := r.registry.Get(name).(Metric)
return m, ok
}
// Tags returns the tags. The return value should not be modified.
func (r *reporter) Tags() map[string]string {
return r.tags
}
// Run starts sending measurements regularly with given interval.
// This is a blocking call and is usually called with `go reporter.Run()`.
func (r *reporter) Run() {
if r.running {
log.Println("metrics.Reporter already running")
return
}
if err := r.open(); err != nil {
log.Printf("unable to reconnect InfluxDB client: %v", err)
return
}
r.run()
}
func (r *reporter) run() {
var (
pts []client.Point
intervalTicker = time.NewTicker(r.interval)
pingTicker = time.NewTicker(time.Second * 5)
)
for {
select {
case <-r.ctx.Done():
return
case <-intervalTicker.C:
pts = pts[:0]
pts = r.getPoints(pts)
if err := r.write(pts); err != nil {
log.Printf("unable to send metrics to InfluxDB: %v", err)
}
case <-pingTicker.C:
_, _, err := r.client.Ping()
if err != nil {
log.Printf("got error while sending a ping to InfluxDB: %v", err)
if err = r.open(); err != nil {
log.Printf("unable to reconnect InfluxDB client: %v", err)
}
}
}
}
}
func (r *reporter) getPoints(pts []client.Point) []client.Point {
r.registry.Each(func(name string, data interface{}) {
m, ok := data.(Metric)
if !ok {
pts = r.basicMetric(pts, name, data)
return
}
pts = m.AddPoints(pts)
})
return pts
}
func (r *reporter) basicMetric(pts []client.Point, name string, data interface{}) []client.Point {
var m Metric
switch metric := data.(type) {
case metrics.Counter:
m = newCounter(name, WithMetric(metric))
case metrics.Gauge:
m = newGauge(name, WithMetric(metric))
case metrics.GaugeFloat64:
m = newGaugeFloat64(name, WithMetric(metric))
case metrics.Histogram:
m = newHistogram(name, WithMetric(metric))
case metrics.Meter:
m = newMeter(name, WithMetric(metric))
case metrics.Timer:
m = newTimer(name, WithMetric(metric))
default:
return pts
}
return m.AddPoints(pts)
}
func (r *reporter) open() (err error) {
if r.client != nil {
return nil
}
r.client, err = client.NewClient(client.Config{
URL: r.server.URL,
Username: r.server.User,
Password: r.server.Pass,
})
return err
}
func (r *reporter) write(points []client.Point) error {
bps := client.BatchPoints{
Points: points,
Database: r.server.DB,
Time: r.getNow(),
}
_, err := r.client.Write(bps)
return err
}
func (r *reporter) getNow() time.Time {
now := time.Now()
if r.align {
now = now.Truncate(r.interval)
}
return now
}
// Stop stops the reporter. It should be discarded after and cannot be restartet.
func (r *reporter) Stop() {
r.cancel()
}