Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 156dc53

Browse files
author
Ladislav Prskavec
committed
feat: add support for router bytes metric
1 parent 9faf807 commit 156dc53

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

client.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package main
22

33
import (
4-
statsd "github.com/DataDog/datadog-go/statsd"
5-
log "github.com/Sirupsen/logrus"
6-
"strconv"
7-
"strings"
84
"errors"
95
"regexp"
106
"sort"
7+
"strconv"
8+
"strings"
9+
10+
statsd "github.com/DataDog/datadog-go/statsd"
11+
log "github.com/Sirupsen/logrus"
1112
)
1213

1314
const sampleRate = 1.0
@@ -56,7 +57,6 @@ func (c *Client) sendToStatsd(in chan *logMetrics) {
5657
"prefix": data.prefix,
5758
}).Debug("logMetrics received")
5859

59-
6060
if data.typ == routerMsg {
6161
c.sendRouterMsg(data)
6262
} else if data.typ == sampleMsg {
@@ -139,6 +139,20 @@ func (c *Client) sendRouterMsg(data *logMetrics) {
139139
return
140140
}
141141

142+
bytes, err := strconv.ParseFloat(data.metrics["bytes"].Val, 10)
143+
if err != nil {
144+
log.WithFields(log.Fields{
145+
"type": "router",
146+
"metric": "bytes",
147+
"err": err,
148+
}).Info("Could not parse metric value")
149+
return
150+
}
151+
// https://devcenter.heroku.com/articles/http-routing
152+
err = c.Histogram(*data.prefix+"heroku.router.response.bytes", bytes, tags, sampleRate)
153+
if err != nil {
154+
log.WithField("error", err).Info("Failed to send Histogram")
155+
}
142156
err = c.Histogram(*data.prefix+"heroku.router.request.connect", conn, tags, sampleRate)
143157
if err != nil {
144158
log.WithField("error", err).Info("Failed to send Histogram")
@@ -214,10 +228,14 @@ func (c *Client) sendScalingMsg(data *logMetrics) {
214228

215229
func (c *Client) sendMetric(metricType string, metricName string, value float64, tags []string) error {
216230
switch metricType {
217-
case "metric", "sample": return c.Gauge(metricName, value, tags, sampleRate)
218-
case "measure": return c.Histogram(metricName, value, tags, sampleRate)
219-
case "count": return c.Count(metricName, int64(value), tags, sampleRate)
220-
default: return errors.New("Unknown metric type"+metricType)
231+
case "metric", "sample":
232+
return c.Gauge(metricName, value, tags, sampleRate)
233+
case "measure":
234+
return c.Histogram(metricName, value, tags, sampleRate)
235+
case "count":
236+
return c.Count(metricName, int64(value), tags, sampleRate)
237+
default:
238+
return errors.New("Unknown metric type" + metricType)
221239
}
222240
}
223241

client_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var statsdTests = []struct {
1616
Expected []string
1717
}{
1818
{
19-
cnt: 2,
19+
cnt: 3,
2020
m: logMetrics{
2121
routerMsg,
2222
&app,
@@ -27,12 +27,14 @@ var statsdTests = []struct {
2727
"path": {"/foo", ""},
2828
"connect": {"1", "ms"},
2929
"service": {"37", "ms"},
30-
"status": {"401", ""},
30+
"status": {"401", ""},
31+
"bytes": {"244000", ""},
3132
"garbage": {"bar", ""},
3233
},
3334
events,
3435
},
3536
Expected: []string{
37+
"prefix.heroku.router.response.bytes:244000.000000|h|#at:info,status:401,tag1,tag2,statusFamily:4xx",
3638
"prefix.heroku.router.request.connect:1.000000|h|#at:info,status:401,tag1,tag2,statusFamily:4xx",
3739
"prefix.heroku.router.request.service:37.000000|h|#at:info,status:401,tag1,tag2,statusFamily:4xx",
3840
},
@@ -146,8 +148,7 @@ var statsdTests = []struct {
146148
&app,
147149
&tags,
148150
&prefix,
149-
map[string]logValue{
150-
},
151+
map[string]logValue{},
151152
[]string{
152153
"Release v1 created by foo@bar",
153154
},
@@ -156,7 +157,6 @@ var statsdTests = []struct {
156157
"_e{13,29}:app/api: test|Release v1 created by foo@bar|#tag1,tag2",
157158
},
158159
},
159-
160160
}
161161

162162
func TestStatsdClient(t *testing.T) {

server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ var fullTests = []struct {
1919
Expected []string
2020
}{
2121
{
22-
cnt: 2,
22+
cnt: 3,
2323
Req: `255 <158>1 2015-04-02T11:52:34.520012+00:00 host heroku router - at=info method=POST path="/users" host=myapp.com request_id=c1806361-2081-42e7-a8aa-92b6808eac8e fwd="24.76.242.18" dyno=web.1 connect=1ms service=37ms status=201 bytes=828`,
2424
Expected: []string{
25+
"heroku.router.response.bytes:828.000000|h|#at:info,dyno:web.1,host:myapp.com,method:POST,path:/users,status:201,statusFamily:2xx",
2526
"heroku.router.request.connect:1.000000|h|#at:info,dyno:web.1,host:myapp.com,method:POST,path:/users,status:201,statusFamily:2xx",
2627
"heroku.router.request.service:37.000000|h|#at:info,dyno:web.1,host:myapp.com,method:POST,path:/users,status:201,statusFamily:2xx",
2728
},

0 commit comments

Comments
 (0)