-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathlibrato_reporter.go
68 lines (53 loc) · 1.39 KB
/
librato_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
type LibratoBulk struct {
Gauges []map[string]interface{} `json:"gauges"`
}
type LibratoReporter struct {
Credentials ReporterCredentials
}
// lifted from https://github.com/rcrowley/go-librato/blob/master/simple.go
// thanks!
type tbody map[string]tibody
type tibody []tmetric
type tmetric map[string]interface{}
var libratoReporterUA = func() string {
return fmt.Sprintf("groundcontrol/%s", VERSION)
}()
func NewLibratoReporter(creds ReporterCredentials) (h *LibratoReporter) {
return &LibratoReporter{Credentials: creds}
}
func (self *LibratoReporter) ReportHealth(h *Health) {
bulk := LibratoBulk{}
hmap := h.Map()
for k, v := range hmap {
bulk.Gauges = append(bulk.Gauges, map[string]interface{}{"name": k, "value": v, "source": "pi"})
}
b, _ := json.Marshal(bulk)
req, err := http.NewRequest(
"POST",
"https://metrics-api.librato.com/v1/metrics",
bytes.NewBuffer(b),
)
if nil != err {
log.Println("Error creating request", err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Set("User-Agent", libratoReporterUA)
req.SetBasicAuth(self.Credentials.User, self.Credentials.Key)
resp, err := http.DefaultClient.Do(req)
if nil != err {
log.Println("Error receiving response", err)
return
}
if resp.StatusCode != 200 {
log.Println("Error: Librato API Error: ", resp)
}
}