Skip to content

Commit

Permalink
Merge pull request #112 from marceloboeira/feature/push-metrics-with-…
Browse files Browse the repository at this point in the history
…statsd

Push http metrics with statsd
  • Loading branch information
zhouzhuojie authored Apr 12, 2018
2 parents 2477c69 + 5e6f547 commit ce112c4
Show file tree
Hide file tree
Showing 18 changed files with 2,111 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# All files use LF as eol
* text=auto eol=lf

# Remove vendor folder from the diff
vendor/**/* -diff
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@
[[constraint]]
name = "github.com/bsm/ratelimit"
version = "2.0.0"

[[constraint]]
name = "github.com/DataDog/datadog-go"
version = "2.1.0"
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ Or try it on [https://try-flagr.herokuapp.com](https://try-flagr.herokuapp.com)

```
curl --request POST \
--url https://try-flagr.herokuapp.com/api/v1/evaluation \
--header 'content-type: application/json' \
--data '{
"entityID": "127",
"entityType": "user",
"entityContext": {
"state": "NY"
},
"flagID": 1,
"enableDebug": true
}'
--url https://try-flagr.herokuapp.com/api/v1/evaluation \
--header 'content-type: application/json' \
--data '{
"entityID": "127",
"entityType": "user",
"entityContext": {
"state": "NY"
},
"flagID": 1,
"enableDebug": true
}'
```


Expand Down
17 changes: 16 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/DataDog/datadog-go/statsd"
"github.com/caarlos0/env"
"github.com/evalphobia/logrus_sentry"
raven "github.com/getsentry/raven-go"
Expand All @@ -13,14 +14,16 @@ import (

// Global is the global dependency we can use, such as the new relic app instance
var Global = struct {
NewrelicApp newrelic.Application
NewrelicApp newrelic.Application
StatsdClient *statsd.Client
}{}

func init() {
env.Parse(&Config)

setupSentry()
setupLogrus()
setupStatsd()
setupNewrelic()
}

Expand All @@ -45,6 +48,18 @@ func setupSentry() {
}
}

func setupStatsd() {
if Config.StatsdEnabled {
client, err := statsd.New(fmt.Sprintf("%s:%s", Config.StatsdHost, Config.StatsdPort))
if err != nil {
panic(fmt.Sprintf("unable to initialize statsd. %s", err))
}
client.Namespace = Config.StatsdPrefix

Global.StatsdClient = client
}
}

func setupNewrelic() {
if Config.NewRelicEnabled {
nCfg := newrelic.NewConfig(Config.NewRelicAppName, Config.NewRelicKey)
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ var Config = struct {
NewRelicAppName string `env:"FLAGR_NEWRELIC_NAME" envDefault:"flagr"`
NewRelicKey string `env:"FLAGR_NEWRELIC_KEY" envDefault:""`

// StatsdEnabled - enable statsd metrics for all the endpoints and DB operations
StatsdEnabled bool `env:"FLAGR_STATSD_ENABLED" envDefault:"false"`
StatsdHost string `env:"FLAGR_STATSD_HOST" envDefault:"127.0.0.1"`
StatsdPort string `env:"FLAGR_STATSD_PORT" envDefault:"8125"`
StatsdPrefix string `env:"FLAGR_STATSD_PREFIX" envDefault:"flagr."`

// EvalCacheRefreshTimeout - timeout of getting the flags data from DB into the in-memory evaluation cache
EvalCacheRefreshTimeout time.Duration `env:"FLAGR_EVALCACHE_REFRESHTIMEOUT" envDefault:"59s"`
// EvalCacheRefreshInterval - time interval of getting the flags data from DB into the in-memory evaluation cache
Expand Down
29 changes: 29 additions & 0 deletions pkg/config/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package config
import (
"net/http"
"os"
"strconv"
"strings"
"time"

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

if Config.StatsdEnabled {
n.Use(&statsdMiddleware{StatsdClient: Global.StatsdClient})
}

if Config.NewRelicEnabled {
n.Use(&negroninewrelic.Newrelic{Application: &Global.NewrelicApp})
}
Expand Down Expand Up @@ -87,3 +94,25 @@ func (a *auth) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.Han
}
a.JWTMiddleware.HandlerWithNext(w, req, next)
}

type statsdMiddleware struct {
StatsdClient *statsd.Client
}

func (s *statsdMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
defer func(start time.Time) {
response := w.(negroni.ResponseWriter)
status := strconv.Itoa(response.Status())
duration := float64(time.Since(start)) / float64(time.Millisecond)
tags := []string{
"status:" + status,
"path:" + r.RequestURI,
"method:" + r.Method,
}

s.StatsdClient.Incr("http.requests.count", tags, 1)
s.StatsdClient.TimeInMilliseconds("http.requests.duration", duration, tags, 1)
}(time.Now())

next(w, r)
}
1 change: 0 additions & 1 deletion pkg/handler/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

var validatePutDistributions = func(params distribution.PutDistributionsParams) *Error {

sum := int64(0)
for _, d := range params.Body.Distributions {
if d.Percent == nil {
Expand Down
11 changes: 11 additions & 0 deletions vendor/github.com/DataDog/datadog-go/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions vendor/github.com/DataDog/datadog-go/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/DataDog/datadog-go/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions vendor/github.com/DataDog/datadog-go/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ce112c4

Please sign in to comment.