/
datadog_reporter.go
92 lines (81 loc) · 2.2 KB
/
datadog_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
package reporter
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
sharedapi "code.cloudfoundry.org/loggregator-tools/reliability/api"
)
type HTTP interface {
Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)
}
type DataDogReporter struct {
apiKey string
host string
instanceIndex string
client HTTP
}
func NewDataDogReporter(key, host, instanceIndex string, h HTTP) *DataDogReporter {
return &DataDogReporter{
apiKey: key,
host: host,
instanceIndex: instanceIndex,
client: h,
}
}
func (r *DataDogReporter) Report(t *TestResult) error {
resp, err := r.client.Post(
fmt.Sprintf("https://app.datadoghq.com/api/v1/series?api_key=%s", r.apiKey),
"application/json;charset=utf-8",
strings.NewReader(
buildPayload(r.host, r.instanceIndex, t.TestStartTime, t.ReceivedLogCount, t.Cycles, t.Delay),
),
)
if err != nil {
return err
}
defer resp.Body.Close()
log.Printf("datadog response status code: %d", resp.StatusCode)
if resp.StatusCode != http.StatusCreated &&
resp.StatusCode != http.StatusAccepted &&
resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code was %d", resp.StatusCode)
}
return nil
}
func buildPayload(host, instanceIndex string, t time.Time, msgCount, cycles uint64, delay time.Duration) string {
return fmt.Sprintf(`{
"series": [
{
"metric": "smoke_test.loggregator.msg_count",
"points": [[%[1]d, %[4]d]],
"type": "gauge",
"host": "%[2]s",
"tags": ["firehose-nozzle", "delay:%[3]d", "instance_index:%[6]s"]
},
{
"metric": "smoke_test.loggregator.cycles",
"points": [[%[1]d, %[5]d]],
"type": "gauge",
"host": "%[2]s",
"tags": ["firehose-nozzle", "delay:%[3]d", "instance_index:%[6]s"]
}
]
}`, t.Unix(), host, delay, msgCount, cycles, instanceIndex)
}
type TestResult struct {
ReceivedLogCount uint64
Delay time.Duration
Cycles uint64
TestStartTime time.Time
}
func NewTestResult(test *sharedapi.Test, count uint64) *TestResult {
return &TestResult{
Cycles: test.Cycles,
Delay: time.Duration(test.Delay),
ReceivedLogCount: count,
TestStartTime: test.StartTime,
}
}