-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
67 lines (58 loc) · 1.7 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
package main
import (
"time"
"github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api"
)
// InfluxConfig is the configuration for Influx/VictoriaMetrics.
type InfluxConfig struct {
Host string
AuthToken string `yaml:"auth_token"`
Org string
Bucket string
}
type InfluxWriter struct {
client influxdb2.Client
api api.WriteAPI
}
func NewInfluxWriter(config InfluxConfig) InfluxWriter {
client := influxdb2.NewClient(config.Host, config.AuthToken)
return InfluxWriter{
client: client,
api: client.WriteAPI(config.Org, config.Bucket),
}
}
func (i InfluxWriter) Close() {
i.client.Close()
}
func (i InfluxWriter) WriteData(annotations map[string]*Annotation, metrics []Metric) {
i.writeAnnotations(annotations)
i.writeMetrics(metrics)
}
func (i InfluxWriter) writeAnnotations(annotations map[string]*Annotation) {
for _, annotation := range annotations {
for _, event := range annotation.Events {
i.api.WritePoint(influxdb2.NewPointWithMeasurement("cpap").
AddTag("event", annotation.Name).
AddField("annotation", 0).
SetTime(event.Start.Add(-time.Second)))
i.api.WritePoint(influxdb2.NewPointWithMeasurement("cpap").
AddTag("event", annotation.Name).
AddField("annotation", 1).
SetTime(event.Start))
i.api.WritePoint(influxdb2.NewPointWithMeasurement("cpap").
AddTag("event", annotation.Name).
AddField("annotation", 0).
SetTime(event.End))
}
}
}
func (i InfluxWriter) writeMetrics(metrics []Metric) {
for _, metric := range metrics {
for _, point := range metric.Data {
i.api.WritePoint(influxdb2.NewPointWithMeasurement("cpap").
AddField(metric.Name, point.Datum).
SetTime(point.Time))
}
}
}