Skip to content

Commit 5e6f547

Browse files
committed
Push http metrics through statsd
Add a statsd http middleware for negroni Metrics: - "flagr.http.requests.count" > total amount of requests - "flagr.http.requests.duration" > duration of the requests (with nanosecond precision) Tags: - http status as "status" - http uri as "path" - http method as "method"
1 parent 2477c69 commit 5e6f547

File tree

18 files changed

+2111
-14
lines changed

18 files changed

+2111
-14
lines changed

.gitattributes

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# All files use LF as eol
2+
* text=auto eol=lf
3+
4+
# Remove vendor folder from the diff
5+
vendor/**/* -diff

Gopkg.lock

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,7 @@
132132
[[constraint]]
133133
name = "github.com/bsm/ratelimit"
134134
version = "2.0.0"
135+
136+
[[constraint]]
137+
name = "github.com/DataDog/datadog-go"
138+
version = "2.1.0"

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ Or try it on [https://try-flagr.herokuapp.com](https://try-flagr.herokuapp.com)
4141

4242
```
4343
curl --request POST \
44-
--url https://try-flagr.herokuapp.com/api/v1/evaluation \
45-
--header 'content-type: application/json' \
46-
--data '{
47-
"entityID": "127",
48-
"entityType": "user",
49-
"entityContext": {
50-
"state": "NY"
51-
},
52-
"flagID": 1,
53-
"enableDebug": true
54-
}'
44+
--url https://try-flagr.herokuapp.com/api/v1/evaluation \
45+
--header 'content-type: application/json' \
46+
--data '{
47+
"entityID": "127",
48+
"entityType": "user",
49+
"entityContext": {
50+
"state": "NY"
51+
},
52+
"flagID": 1,
53+
"enableDebug": true
54+
}'
5555
```
5656

5757

pkg/config/config.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/DataDog/datadog-go/statsd"
78
"github.com/caarlos0/env"
89
"github.com/evalphobia/logrus_sentry"
910
raven "github.com/getsentry/raven-go"
@@ -13,14 +14,16 @@ import (
1314

1415
// Global is the global dependency we can use, such as the new relic app instance
1516
var Global = struct {
16-
NewrelicApp newrelic.Application
17+
NewrelicApp newrelic.Application
18+
StatsdClient *statsd.Client
1719
}{}
1820

1921
func init() {
2022
env.Parse(&Config)
2123

2224
setupSentry()
2325
setupLogrus()
26+
setupStatsd()
2427
setupNewrelic()
2528
}
2629

@@ -45,6 +48,18 @@ func setupSentry() {
4548
}
4649
}
4750

51+
func setupStatsd() {
52+
if Config.StatsdEnabled {
53+
client, err := statsd.New(fmt.Sprintf("%s:%s", Config.StatsdHost, Config.StatsdPort))
54+
if err != nil {
55+
panic(fmt.Sprintf("unable to initialize statsd. %s", err))
56+
}
57+
client.Namespace = Config.StatsdPrefix
58+
59+
Global.StatsdClient = client
60+
}
61+
}
62+
4863
func setupNewrelic() {
4964
if Config.NewRelicEnabled {
5065
nCfg := newrelic.NewConfig(Config.NewRelicAppName, Config.NewRelicKey)

pkg/config/env.go

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ var Config = struct {
4747
NewRelicAppName string `env:"FLAGR_NEWRELIC_NAME" envDefault:"flagr"`
4848
NewRelicKey string `env:"FLAGR_NEWRELIC_KEY" envDefault:""`
4949

50+
// StatsdEnabled - enable statsd metrics for all the endpoints and DB operations
51+
StatsdEnabled bool `env:"FLAGR_STATSD_ENABLED" envDefault:"false"`
52+
StatsdHost string `env:"FLAGR_STATSD_HOST" envDefault:"127.0.0.1"`
53+
StatsdPort string `env:"FLAGR_STATSD_PORT" envDefault:"8125"`
54+
StatsdPrefix string `env:"FLAGR_STATSD_PREFIX" envDefault:"flagr."`
55+
5056
// EvalCacheRefreshTimeout - timeout of getting the flags data from DB into the in-memory evaluation cache
5157
EvalCacheRefreshTimeout time.Duration `env:"FLAGR_EVALCACHE_REFRESHTIMEOUT" envDefault:"59s"`
5258
// EvalCacheRefreshInterval - time interval of getting the flags data from DB into the in-memory evaluation cache

pkg/config/middleware.go

+29
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package config
33
import (
44
"net/http"
55
"os"
6+
"strconv"
67
"strings"
8+
"time"
79

10+
"github.com/DataDog/datadog-go/statsd"
811
jwtmiddleware "github.com/auth0/go-jwt-middleware"
912
"github.com/dgrijalva/jwt-go"
1013
"github.com/gohttp/pprof"
@@ -28,6 +31,10 @@ func SetupGlobalMiddleware(handler http.Handler) http.Handler {
2831
}))
2932
}
3033

34+
if Config.StatsdEnabled {
35+
n.Use(&statsdMiddleware{StatsdClient: Global.StatsdClient})
36+
}
37+
3138
if Config.NewRelicEnabled {
3239
n.Use(&negroninewrelic.Newrelic{Application: &Global.NewrelicApp})
3340
}
@@ -87,3 +94,25 @@ func (a *auth) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.Han
8794
}
8895
a.JWTMiddleware.HandlerWithNext(w, req, next)
8996
}
97+
98+
type statsdMiddleware struct {
99+
StatsdClient *statsd.Client
100+
}
101+
102+
func (s *statsdMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
103+
defer func(start time.Time) {
104+
response := w.(negroni.ResponseWriter)
105+
status := strconv.Itoa(response.Status())
106+
duration := float64(time.Since(start)) / float64(time.Millisecond)
107+
tags := []string{
108+
"status:" + status,
109+
"path:" + r.RequestURI,
110+
"method:" + r.Method,
111+
}
112+
113+
s.StatsdClient.Incr("http.requests.count", tags, 1)
114+
s.StatsdClient.TimeInMilliseconds("http.requests.duration", duration, tags, 1)
115+
}(time.Now())
116+
117+
next(w, r)
118+
}

pkg/handler/validate.go

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
)
99

1010
var validatePutDistributions = func(params distribution.PutDistributionsParams) *Error {
11-
1211
sum := int64(0)
1312
for _, d := range params.Body.Distributions {
1413
if d.Percent == nil {

vendor/github.com/DataDog/datadog-go/.travis.yml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/DataDog/datadog-go/CHANGELOG.md

+94
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/DataDog/datadog-go/LICENSE.txt

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/DataDog/datadog-go/README.md

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)