-
Notifications
You must be signed in to change notification settings - Fork 1
/
goji_logger.go
131 lines (109 loc) · 3.25 KB
/
goji_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package log4go
import (
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/peterbourgon/g2s"
)
var LastPathParamIntegerRegex = regexp.MustCompile(`\_[0-9]{1,}$`)
var PathParamIntegerRegex = regexp.MustCompile(`\_[0-9]{1,}\_`)
type StatsdConfig struct {
// IP and port of the statsd server. Optional. Default to "127.0.0.1:8125".
IpPort string
// Prefix added to the metric keys. Optional.
Prefix string
}
type gojiLogger struct {
h http.Handler
}
type gojiStatds struct {
h http.Handler
WriteLog bool
Config StatsdConfig
}
func (g gojiLogger) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
writeGojiLog(fmt.Sprintf("Started %s '%s' from %s", req.Method, req.URL.Path, req.RemoteAddr))
lresp := wrapWriter(resp)
startAt := time.Now()
g.h.ServeHTTP(lresp, req)
lresp.maybeWriteHeader()
latency := float64(time.Since(startAt)) / float64(time.Millisecond)
writeGojiLog(fmt.Sprintf("Returning %d in %s", lresp.status(), fmt.Sprintf("%6.4f ms", latency)))
}
func (g gojiStatds) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
startAt := time.Now()
if g.WriteLog {
writeGojiLog(fmt.Sprintf("Started %s '%s' from %s", req.Method, req.URL.Path, req.RemoteAddr))
}
lresp := wrapWriter(resp)
g.h.ServeHTTP(lresp, req)
lresp.maybeWriteHeader()
elapsedTime := time.Since(startAt)
latency := float64(elapsedTime) / float64(time.Millisecond)
if g.WriteLog {
writeGojiLog(fmt.Sprintf("Returning %d in %s", lresp.status(), fmt.Sprintf("%6.4f ms", latency)))
}
statsd, err := g2s.Dial("udp", g.Config.IpPort)
if err != nil {
return
}
apiName := ReplaceIntegerPathParameters(req.URL.Path, PathParamIntegerRegex, LastPathParamIntegerRegex)
keyBase := ""
if g.Config.Prefix != "" {
keyBase += g.Config.Prefix + "."
}
keyBase += apiName + "."
keyBase += req.Method + "."
statsd.Counter(1.0, keyBase+"status_code."+strconv.Itoa(lresp.status()), 1)
statsd.Timing(1.0, keyBase+"elapsed_time.", elapsedTime)
}
func NewGojiLog() func(http.Handler) http.Handler {
fn := func(h http.Handler) http.Handler {
return gojiLogger{h: h}
}
return fn
}
func NewGojiStatsd(config StatsdConfig, writeLog bool) func(http.Handler) http.Handler {
if len(config.IpPort) == 0 {
config.IpPort = "127.0.0.1:8125"
}
fn := func(h http.Handler) http.Handler {
return gojiStatds{h: h, WriteLog: writeLog, Config: config}
}
return fn
}
func writeGojiLog(msg string) {
rec := &LogRecord{
Level: DEBUG,
Created: time.Now(),
Source: "GojiController",
Message: msg,
}
for _, filt := range Global {
if filt.Level > rec.Level {
continue
}
filt.LogWrite(rec)
}
}
func ReplaceIntegerPathParameters(input string, pathRegex *regexp.Regexp, lastPathRegex *regexp.Regexp) (output string) {
output = strings.Replace(input, "/", "_", -1)
matchs := pathRegex.FindAllStringSubmatch(output, -1)
for _, match := range matchs {
if match[0] != "" {
output = strings.Replace(output, match[0], "_id_", -1)
}
}
output = replaceLastIntegerPathParameters(output, lastPathRegex)
return output
}
func replaceLastIntegerPathParameters(input string, r *regexp.Regexp) (output string) {
match := r.FindStringSubmatch(input)
if len(match) > 0 && match[0] != "" {
output = strings.Replace(input, match[0], "_id", -1)
}
return output
}