-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefault_middleware.go
More file actions
58 lines (45 loc) · 1.14 KB
/
default_middleware.go
File metadata and controls
58 lines (45 loc) · 1.14 KB
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
package influence
import (
"net/http"
"strconv"
"time"
client "github.com/influxdata/influxdb/client/v2"
)
// DefaultMiddleware sends http handler statistic to InfluxDB
func DefaultMiddleware(conn client.Client, next http.Handler) http.Handler {
tags := getEnvTags(confHandlerTagsKey)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
defer func() {
go func() {
endTime := time.Now()
bp, err := getPointsBatch()
if err != nil {
return
}
contentLength, err := strconv.Atoi(w.Header().Get("Content-Length"))
if err != nil {
contentLength = 0
}
fields := map[string]interface{}{
"execution_time": endTime.Sub(startTime).Nanoseconds() / int64(time.Millisecond),
"content_length": contentLength,
}
handlerTags := map[string]string{
"handler": r.URL.Path,
"method": r.Method,
}
for k, v := range handlerTags {
tags[k] = v
}
pt, err := client.NewPoint(httpHandlerTablename, tags, fields, endTime)
if err != nil {
return
}
bp.AddPoint(pt)
conn.Write(bp)
}()
}()
next.ServeHTTP(w, r)
})
}