forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrumentation_handlers.go
83 lines (68 loc) · 2.56 KB
/
instrumentation_handlers.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
package main
import (
"net/http"
"os"
"runtime/debug"
"strconv"
"time"
"github.com/gocraft/health"
"github.com/TykTechnologies/tyk/config"
)
var applicationGCStats = debug.GCStats{}
var instrument = health.NewStream()
// setupInstrumentation handles all the intialisation of the instrumentation handler
func setupInstrumentation(arguments map[string]interface{}) {
switch {
case arguments["--log-instrumentation"] == true:
case os.Getenv("TYK_INSTRUMENTATION") == "1":
default:
return
}
if config.Global.StatsdConnectionString == "" {
log.Error("Instrumentation is enabled, but no connectionstring set for statsd")
return
}
log.Info("Sending stats to: ", config.Global.StatsdConnectionString, " with prefix: ", config.Global.StatsdPrefix)
statsdSink, err := NewStatsDSink(config.Global.StatsdConnectionString,
&StatsDSinkOptions{Prefix: config.Global.StatsdPrefix})
if err != nil {
log.Fatal("Failed to start StatsD check: ", err)
}
log.Info("StatsD instrumentation sink started")
instrument.AddSink(statsdSink)
MonitorApplicationInstrumentation()
}
// InstrumentationMW will set basic instrumentation events, variables and timers on API jobs
func InstrumentationMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
job := instrument.NewJob("SystemAPICall")
next.ServeHTTP(w, r)
job.EventKv("called", health.Kvs{
"from_ip": requestIP(r),
"method": r.Method,
"endpoint": r.URL.Path,
"raw_url": r.URL.String(),
"size": strconv.Itoa(int(r.ContentLength)),
})
job.Complete(health.Success)
})
}
func MonitorApplicationInstrumentation() {
log.Info("Starting application monitoring...")
go func() {
job := instrument.NewJob("GCActivity")
job_rl := instrument.NewJob("Load")
metadata := health.Kvs{"host": hostDetails.Hostname}
applicationGCStats.PauseQuantiles = make([]time.Duration, 5)
for {
debug.ReadGCStats(&applicationGCStats)
job.GaugeKv("pauses_quantile_min", float64(applicationGCStats.PauseQuantiles[0].Nanoseconds()), metadata)
job.GaugeKv("pauses_quantile_25", float64(applicationGCStats.PauseQuantiles[1].Nanoseconds()), metadata)
job.GaugeKv("pauses_quantile_50", float64(applicationGCStats.PauseQuantiles[2].Nanoseconds()), metadata)
job.GaugeKv("pauses_quantile_75", float64(applicationGCStats.PauseQuantiles[3].Nanoseconds()), metadata)
job.GaugeKv("pauses_quantile_max", float64(applicationGCStats.PauseQuantiles[4].Nanoseconds()), metadata)
job_rl.GaugeKv("rps", float64(GlobalRate.Rate()), metadata)
time.Sleep(5 * time.Second)
}
}()
}