forked from apiaryio/heroku-datadog-drain-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
233 lines (207 loc) · 5.84 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"errors"
"regexp"
"sort"
"strconv"
"strings"
statsd "github.com/DataDog/datadog-go/statsd"
log "github.com/Sirupsen/logrus"
)
const sampleRate = 1.0
const (
routerMsg int = iota
scalingMsg
sampleMsg
metricsTag
releaseMsg
)
var routerMetricsKeys = []string{"dyno", "method", "status", "path", "host", "code", "desc", "at"}
var sampleMetricsKeys = []string{"source"}
var scalingMetricsKeys = []string{"mailer", "web"}
var customMetricsKeys = []string{"media_type", "output_type", "route"}
type Client struct {
*statsd.Client
ExcludedTags map[string]bool
}
var statusCode *regexp.Regexp = regexp.MustCompile(`^(?P<Family>\d)\d\d`)
func statsdClient(addr string) (*Client, error) {
c, err := statsd.New(addr)
return &Client{c, make(map[string]bool)}, err
}
func (c *Client) sendToStatsd(in chan *logMetrics) {
var data *logMetrics
var ok bool
for {
data, ok = <-in
if !ok { //Exit, channel was closed
return
}
if data.typ == routerMsg {
c.sendRouterMsg(data)
} else if data.typ == sampleMsg {
c.sendSampleMsg(data)
} else if data.typ == scalingMsg {
c.sendEvents(*data.app, "heroku", data.events, *data.tags)
c.sendScalingMsg(data)
} else if data.typ == metricsTag {
c.sendMetricsWithTags(data)
} else if data.typ == releaseMsg {
c.sendEvents(*data.app, "app", data.events, *data.tags)
} else {
log.WithField("type", data.typ).Warn("Unknown log message")
}
}
}
func (c *Client) sendEvents(app string, namespace string, events []string, tags []string) {
for _, v := range events {
event := statsd.NewEvent(namespace+"/api: "+app, v)
event.Tags = tags
c.Event(event)
log.WithFields(log.Fields{
"type": "event",
"app": app,
"value": v,
}).Info("Event sent")
}
}
func (c *Client) extractTags(tags []string, permittedTags []string, metrics map[string]logValue) []string {
for _, mk := range permittedTags {
if c.ExcludedTags[mk] {
continue
}
if v, ok := metrics[mk]; ok {
tags = append(tags, mk+":"+v.Val)
}
}
sort.Strings(tags)
return tags
}
func addStatusFamilyToTags(data *logMetrics, tags []string) []string {
if val, ok := data.metrics["status"]; ok {
match := statusCode.FindStringSubmatch(val.Val)
if len(match) > 1 {
tags = append(tags, "statusFamily:"+match[1]+"xx")
}
}
return tags
}
func (c *Client) sendRouterMsg(data *logMetrics) {
tags := c.extractTags(*data.tags, routerMetricsKeys, data.metrics)
tags = addStatusFamilyToTags(data, tags)
conn, err := strconv.ParseFloat(data.metrics["connect"].Val, 10)
if err != nil {
log.WithFields(log.Fields{
"type": "router",
"err": err,
"metric": "connect",
}).Info("Could not parse metric value")
return
}
serv, err := strconv.ParseFloat(data.metrics["service"].Val, 10)
if err != nil {
log.WithFields(log.Fields{
"type": "router",
"metric": "service",
"err": err,
}).Info("Could not parse metric value")
return
}
err = c.Histogram(*data.prefix+"heroku.router.request.connect", conn, tags, sampleRate)
if err != nil {
log.WithField("error", err).Info("Failed to send Histogram")
}
err = c.Histogram(*data.prefix+"heroku.router.request.service", serv, tags, sampleRate)
if err != nil {
log.WithField("error", err).Info("Failed to send Histogram")
}
if data.metrics["at"].Val == "error" {
err = c.Count(*data.prefix+"heroku.router.error", 1, tags, 0.1)
if err != nil {
log.WithField("error", err).Info("Failed to send Count")
}
}
}
func (c *Client) sendSampleMsg(data *logMetrics) {
tags := c.extractTags(*data.tags, sampleMetricsKeys, data.metrics)
for k, v := range data.metrics {
if strings.Index(k, "#") != -1 {
m := strings.Replace(strings.Split(k, "#")[1], "_", ".", -1)
vnum, err := strconv.ParseFloat(v.Val, 10)
if err == nil {
err = c.Gauge(*data.prefix+"heroku.dyno."+m, vnum, tags, sampleRate)
if err != nil {
log.WithField("error", err).Info("Failed to send Gauge")
}
} else {
log.WithFields(log.Fields{
"type": "sample",
"metric": k,
"err": err,
}).Info("Could not parse metric value")
}
}
}
}
func (c *Client) sendScalingMsg(data *logMetrics) {
tags := *data.tags
for _, mk := range scalingMetricsKeys {
if v, ok := data.metrics[mk]; ok {
vnum, err := strconv.ParseFloat(v.Val, 10)
if err == nil {
err = c.Gauge(*data.prefix+"heroku.dyno."+mk, vnum, tags, sampleRate)
if err != nil {
log.WithField("error", err).Info("Failed to send Gauge")
}
} else {
log.WithFields(log.Fields{
"type": "scaling",
"metric": mk,
"err": err,
}).Info("Could not parse metric value")
}
}
}
}
func (c *Client) sendMetric(metricType string, metricName string, value float64, tags []string) error {
switch metricType {
case "metric", "sample":
return c.Gauge(metricName, value, tags, sampleRate)
case "measure":
return c.Histogram(metricName, value, tags, sampleRate)
case "count":
return c.Count(metricName, int64(value), tags, sampleRate)
default:
return errors.New("Unknown metric type" + metricType)
}
}
func (c *Client) sendMetricsWithTags(data *logMetrics) {
tags := *data.tags
Tags:
for k, v := range data.metrics {
if strings.Index(k, "tag#") != -1 {
if _, err := strconv.Atoi(v.Val); err != nil {
m := strings.Replace(strings.Split(k, "tag#")[1], "_", ".", -1)
for _, mk := range customMetricsKeys {
if m == mk {
tags = append(tags, mk+":"+v.Val)
continue Tags
}
}
}
}
}
for k, v := range data.metrics {
if strings.Index(k, "#") != -1 {
if vnum, err := strconv.ParseFloat(v.Val, 10); err == nil {
keySplit := strings.Split(k, "#")
metricType := keySplit[0]
m := strings.Replace(keySplit[1], "_", ".", -1)
err = c.sendMetric(metricType, *data.prefix+"heroku.metric."+m, vnum, tags)
if err != nil {
log.WithField("error", err).Warning("Failed to send Gauge")
}
}
}
}
}