-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp_logger.go
60 lines (49 loc) · 1.32 KB
/
http_logger.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
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
)
type StatusRecorder struct {
http.ResponseWriter
Status int
ResponseBody string
}
func (r *StatusRecorder) WriteHeader(status int) {
r.Status = status
r.ResponseWriter.WriteHeader(status)
}
func (r *StatusRecorder) Write(body []byte) (int, error) {
r.ResponseBody = string(body)
return r.ResponseWriter.Write(body)
}
func PrometheusMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
recorder := &StatusRecorder{
ResponseWriter: w,
Status: 200,
}
route := mux.CurrentRoute(r)
path, err := route.GetPathTemplate()
if err != nil {
log.Println("http_logger: get path template")
}
request, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("http_logger: read request body")
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(request))
start := time.Now()
next.ServeHTTP(recorder, r)
SaveHTTPDurationHistogram(start, path, recorder.Status, r.Method)
SaveHTTPDuration(start, path, recorder.Status, r.Method)
SaveHTTPCount(1, path, recorder.Status, r.Method)
sleepData := r.URL.Query().Get("sleep")
sl, _ := strconv.Atoi(sleepData)
SaveHTTPGauge(float64(sl), path, recorder.Status, r.Method)
})
}