forked from pivotal-cf/cred-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datadog_client.go
137 lines (109 loc) · 2.96 KB
/
datadog_client.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
package datadog
import (
"bytes"
"cred-alert/net"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/lager"
)
var APIURL = "https://app.datadoghq.com"
type Series []Metric
type Metric struct {
Name string `json:"metric"`
Points []Point `json:"points"`
Type string `json:"type"`
Host string `json:"host"`
Tags []string `json:"tags"`
}
type Point struct {
Timestamp time.Time
Value float32
}
const GaugeMetricType string = "gauge"
const CounterMetricType string = "count"
func (p Point) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`[%d, %f]`, p.Timestamp.Unix(), p.Value)), nil
}
func (p *Point) UnmarshalJSON(data []byte) error {
var tuple []float64
if err := json.Unmarshal(data, &tuple); err != nil {
return err
}
p.Timestamp = time.Unix(int64(tuple[0]), 0)
p.Value = float32(tuple[1])
return nil
}
type request struct {
Series Series `json:"series"`
}
//go:generate counterfeiter . Client
type Client interface {
PublishSeries(logger lager.Logger, series Series)
BuildMetric(metricType string, metricName string, count float32, tags ...string) Metric
}
type client struct {
apiKey string
client net.Client
clock clock.Clock
}
func NewClient(apiKey string, httpClient net.Client, clock clock.Clock) Client {
return &client{
apiKey: apiKey,
client: httpClient,
clock: clock,
}
}
func (c *client) BuildMetric(metricType string, metricName string, count float32, tags ...string) Metric {
return Metric{
Name: metricName,
Type: metricType,
Points: []Point{
{Timestamp: time.Now(), Value: count},
},
Tags: tags,
}
}
func (c *client) PublishSeries(logger lager.Logger, series Series) {
var err error
payload, err := json.Marshal(request{
Series: series,
})
if err != nil {
logger.Error("failed", err)
return
}
req, err := http.NewRequest("POST", APIURL+"/api/v1/series", bytes.NewBuffer(payload))
if err != nil {
logger.Error("failed building request", err)
return
}
auth := url.Values{}
auth.Set("api_key", c.apiKey)
req.URL.RawQuery = auth.Encode()
req.Header.Set("Content-type", "application/json")
req.Header.Set("Content-length", strconv.Itoa(len(payload)))
maxAttempts := 3
for attempts := 0; attempts < maxAttempts; attempts++ {
var resp *http.Response
resp, err = c.client.Do(req)
if err != nil {
logger.Debug("failed error sending metric to datadog", lager.Data{"error": err, "attempts": attempts, "payload": payload})
continue
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusAccepted {
return
}
err = fmt.Errorf("unexpected status code from datadog, expected: %d, actual: %d", http.StatusAccepted, resp.StatusCode)
logger.Debug("failed: unexpected status code from datadog", lager.Data{"error": err, "payload": payload})
if attempts < (maxAttempts - 1) {
c.clock.Sleep(1 * time.Second)
}
}
logger.Error("failed publishing series", err, lager.Data{"payload": payload})
}