forked from getlantern/lumbermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_msg.go
149 lines (143 loc) · 3.75 KB
/
router_msg.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bytes"
"strconv"
"strings"
)
var (
keyAt = []byte("at")
keyCode = []byte("code")
keyDesc = []byte("desc")
keyMethod = []byte("method")
keyPath = []byte("path")
keyHost = []byte("host")
keyRequestID = []byte("request_id")
keyFwd = []byte("fwd")
keyConnect = []byte("connect")
keyService = []byte("service")
keyStatus = []byte("status")
keySock = []byte("sock")
keyBytes = []byte("bytes")
keyCodeH = []byte(" code=H")
keyCodeBlank = []byte(" code=blank-app")
keyDescBlank = []byte(" desc=\"Blank app\"")
)
// at=info method=GET path=/check?metric=railgun.accepting:sum:max,railgun.running:sum:max&0
// host=umpire.herokai.com request_id=1f3ed8a9-c80c-49de-a4af-2df9f4ddb858 fwd="46.20.45.18"
// dyno=web.14 connect=1ms service=849ms status=500 bytes=306
type routerMsg struct {
Method string
Path string
Host string
RequestID string
Fwd string
Dyno string
Connect int
Service int
Status int
Bytes int
}
func (rm *routerMsg) HandleLogfmt(key, val []byte) error {
switch {
case bytes.Equal(key, keyMethod):
rm.Method = string(val)
case bytes.Equal(key, keyPath):
rm.Path = string(val)
case bytes.Equal(key, keyHost):
rm.Host = string(val)
case bytes.Equal(key, keyRequestID):
rm.RequestID = string(val)
case bytes.Equal(key, keyFwd):
rm.Fwd = string(val)
case bytes.Equal(key, keyDyno):
rm.Dyno = string(val)
case bytes.Equal(key, keyConnect):
connect, e := strconv.Atoi(strings.TrimSuffix(string(val), "ms"))
if e != nil {
return e
}
rm.Connect = connect
case bytes.Equal(key, keyService):
service, e := strconv.Atoi(strings.TrimSuffix(string(val), "ms"))
if e != nil {
return e
}
rm.Service = service
case bytes.Equal(key, keyStatus):
status, e := strconv.Atoi(string(val))
if e != nil {
return e
}
rm.Status = status
case bytes.Equal(key, keyBytes):
bytes, e := strconv.Atoi(string(val))
if e != nil {
return e
}
rm.Bytes = bytes
default:
return nil
// log.Printf("Unknown key (%s) with value: %s\n", key, string(val))
}
return nil
}
type routerError struct {
At string
Code string
Desc string
Method string
Host string
Fwd string
Dyno string
Path string
RequestID string
Connect int
Service int
Status int
Bytes int
Sock string
}
func (re *routerError) HandleLogfmt(key, val []byte) error {
switch {
case bytes.Equal(key, keyAt):
re.At = string(val)
case bytes.Equal(key, keyCode):
re.Code = string(val)
case bytes.Equal(key, keyDesc):
re.Desc = string(val)
case bytes.Equal(key, keyMethod):
re.Method = string(val)
case bytes.Equal(key, keyHost):
re.Host = string(val)
case bytes.Equal(key, keyFwd):
re.Fwd = string(val)
case bytes.Equal(key, keyPath):
re.Path = string(val)
case bytes.Equal(key, keyRequestID):
re.RequestID = string(val)
case bytes.Equal(key, keyDyno):
re.Dyno = string(val)
case bytes.Equal(key, keyConnect):
connect, _ := strconv.Atoi(strings.TrimSuffix(string(val), "ms"))
// swallow errors because connect could be nothing
re.Connect = connect
case bytes.Equal(key, keyService):
service, _ := strconv.Atoi(strings.TrimSuffix(string(val), "ms"))
// swallow errors because service could be nothing
re.Service = service
case bytes.Equal(key, keyStatus):
status, _ := strconv.Atoi(string(val))
// swallow errors because status could be nothing
re.Status = status
case bytes.Equal(key, keyBytes):
bytes, _ := strconv.Atoi(string(val))
// swallow errors because bytes could be nothing
re.Bytes = bytes
case bytes.Equal(key, keySock):
re.Sock = string(val)
default:
return nil
// log.Printf("Unknown key (%s) with value: %s\n", key, string(val))
}
return nil
}