Skip to content

Commit

Permalink
Merge 506c70a into fc3a224
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Jun 13, 2019
2 parents fc3a224 + 506c70a commit 7e3e975
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
3 changes: 3 additions & 0 deletions gateway/instrumentation_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

var applicationGCStats = debug.GCStats{}
var instrument = health.NewStream()
var instrumentationEnabled bool

// setupInstrumentation handles all the intialisation of the instrumentation handler
func setupInstrumentation() {
Expand All @@ -32,6 +33,8 @@ func setupInstrumentation() {
return
}

instrumentationEnabled = true

log.Info("Sending stats to: ", config.Global().StatsdConnectionString, " with prefix: ", config.Global().StatsdPrefix)
statsdSink, err := NewStatsDSink(config.Global().StatsdConnectionString,
&StatsDSinkOptions{Prefix: config.Global().StatsdPrefix})
Expand Down
40 changes: 26 additions & 14 deletions gateway/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,22 @@ func createMiddleware(mw TykMiddleware) func(http.Handler) http.Handler {
}

job := instrument.NewJob("MiddlewareCall")
meta := health.Kvs{
"from_ip": request.RealIP(r),
"method": r.Method,
"endpoint": r.URL.Path,
"raw_url": r.URL.String(),
"size": strconv.Itoa(int(r.ContentLength)),
"mw_name": mw.Name(),
}
meta := health.Kvs{}
eventName := mw.Name() + "." + "executed"
job.EventKv("executed", meta)
job.EventKv(eventName, meta)

if instrumentationEnabled {
meta = health.Kvs{
"from_ip": request.RealIP(r),
"method": r.Method,
"endpoint": r.URL.Path,
"raw_url": r.URL.String(),
"size": strconv.Itoa(int(r.ContentLength)),
"mw_name": mw.Name(),
}
job.EventKv("executed", meta)
job.EventKv(eventName, meta)
}

startTime := time.Now()
mw.Logger().WithField("ts", startTime.UnixNano()).Debug("Started")

Expand All @@ -104,16 +109,23 @@ func createMiddleware(mw TykMiddleware) func(http.Handler) http.Handler {
meta["error"] = err.Error()

finishTime := time.Since(startTime)
job.TimingKv("exec_time", finishTime.Nanoseconds(), meta)
job.TimingKv(eventName+".exec_time", finishTime.Nanoseconds(), meta)

if instrumentationEnabled {
job.TimingKv("exec_time", finishTime.Nanoseconds(), meta)
job.TimingKv(eventName+".exec_time", finishTime.Nanoseconds(), meta)
}

mw.Logger().WithError(err).WithField("code", errCode).WithField("ns", finishTime.Nanoseconds()).Debug("Finished")
return
}

finishTime := time.Since(startTime)
job.TimingKv("exec_time", finishTime.Nanoseconds(), meta)
job.TimingKv(eventName+".exec_time", finishTime.Nanoseconds(), meta)

if instrumentationEnabled {
job.TimingKv("exec_time", finishTime.Nanoseconds(), meta)
job.TimingKv(eventName+".exec_time", finishTime.Nanoseconds(), meta)
}

mw.Logger().WithField("code", errCode).WithField("ns", finishTime.Nanoseconds()).Debug("Finished")

// Special code, bypasses all other execution
Expand Down

0 comments on commit 7e3e975

Please sign in to comment.