-
Notifications
You must be signed in to change notification settings - Fork 19
/
stat.go
51 lines (43 loc) · 1.59 KB
/
stat.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
package gongular
import (
"bytes"
"fmt"
"net/http"
"time"
)
// HandlerStat keeps duration, error (if exists) and whether the chain was stopped for
// a single handler
type HandlerStat struct {
FuncName string
Duration time.Duration
Error error
StopChain bool
}
// RouteStat holds information for the whole route, which path it matched, the written
// response size and the final status code for the request, and finally the logs generated
// by all handlers and it includes the individual HandlerStat this route consists of.
type RouteStat struct {
Request *http.Request
Handlers []HandlerStat
MatchedPath string
TotalDuration time.Duration
ResponseSize int
ResponseCode int
Logs *bytes.Buffer
}
// RouteCallback is the interface to what to do with a given route
type RouteCallback func(stat RouteStat)
// DefaultRouteCallback prints many information about the the request including
// the individual handler(s) information as well
var DefaultRouteCallback RouteCallback = func(stat RouteStat) {
s := fmt.Sprintln(stat.Request.Method, stat.Request.RemoteAddr, stat.MatchedPath,
stat.Request.RequestURI, stat.TotalDuration, stat.ResponseSize, stat.ResponseCode)
for idx, h := range stat.Handlers {
s += fmt.Sprintln("\t", idx, " ", h.FuncName, " ", h.Duration)
}
// All the information is concatenated to avoid race conditions that might occur
fmt.Println(s)
}
// NoOpRouteCallback is doing nothing for a RouteCallback which should increases the performance
// which can be desirable when too many requests arrive
var NoOpRouteCallback = func(stat RouteStat) {}