-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopentsdb.go
48 lines (40 loc) · 1.32 KB
/
opentsdb.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
package main
import (
"fmt"
"strings"
"github.com/arcticfoxnv/oolong/wirelesstag"
"github.com/bluebreezecf/opentsdb-goclient/client"
"github.com/bluebreezecf/opentsdb-goclient/config"
)
type OpenTSDB struct {
client client.Client
prefix string
}
func NewOpenTSDBClient(host string, port int, metricPrefix string) *OpenTSDB {
cfg := config.OpenTSDBConfig{OpentsdbHost: fmt.Sprintf("%s:%d", host, port)}
c, _ := client.NewClient(cfg)
return &OpenTSDB{
client: c,
prefix: metricPrefix,
}
}
func (c *OpenTSDB) prepareValue(tag *wirelesstag.Tag, valueType string, reading wirelesstag.Reading) client.DataPoint {
data := client.DataPoint{
Metric: fmt.Sprintf("%s.%s", c.prefix, valueType),
Timestamp: reading.Timestamp.Unix(),
Value: reading.Value,
Tags: make(map[string]string),
}
// For now, tag with both UUID and Name. We can use these to filter/display
// on dashboards
data.Tags["uuid"] = tag.UUID
data.Tags["name"] = strings.Replace(tag.Name, " ", "_", -1)
return data
}
func (c *OpenTSDB) PutValue(tag *wirelesstag.Tag, valueType string, reading wirelesstag.Reading) error {
// Put the data into the new structure for submitting to opentsdb
data := c.prepareValue(tag, valueType, reading)
// Submit value to opentsdb
_, err := c.client.Put([]client.DataPoint{data}, "summary")
return err
}