-
Notifications
You must be signed in to change notification settings - Fork 1
/
elblog.go
223 lines (211 loc) · 4.75 KB
/
elblog.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package elblog
import (
"bufio"
"bytes"
"io"
"net"
"strconv"
"time"
"unicode/utf8"
)
// Log ...
type Log struct {
Time time.Time
Name string
From, To *net.TCPAddr
RequestProcessingTime time.Duration
BackendProcessingTime time.Duration
ResponseProcessingTime time.Duration
ELBStatusCode, BackendStatusCode int
ReceivedBytes int64
SentBytes int64
Request string
UserAgent string
SSLCipher string
SSLProtocol string
}
// Parse ...
func Parse(b []byte) (log *Log, err error) {
var (
adv, i int
code int64
dur float64
tok []byte
parts [][]byte
)
data := b[adv:]
log = &Log{}
for i != 15 {
data = data[adv:]
adv, tok, err = scan(data, i == 14)
if err != nil {
return
}
switch i {
case 0:
if log.Time, err = time.Parse(time.RFC3339Nano, string(tok)); err != nil {
return
}
case 1:
log.Name = string(tok)
case 2:
parts = bytes.Split(tok, []byte(":"))
switch len(parts) {
case 1:
log.From = &net.TCPAddr{
IP: net.ParseIP(string(parts[0])),
}
case 2:
ip, err := strconv.ParseInt(string(parts[1]), 10, 32)
if err != nil {
return nil, err
}
log.From = &net.TCPAddr{
IP: net.ParseIP(string(parts[0])),
Port: int(ip),
}
}
case 3:
parts = bytes.Split(tok, []byte(":"))
switch len(parts) {
case 1:
log.To = &net.TCPAddr{
IP: net.ParseIP(string(parts[0])),
}
case 2:
ip, err := strconv.ParseInt(string(parts[1]), 10, 32)
if err != nil {
return nil, err
}
log.To = &net.TCPAddr{
IP: net.ParseIP(string(parts[0])),
Port: int(ip),
}
}
case 4:
dur, err = strconv.ParseFloat(string(tok), 64)
if err != nil {
return
}
log.RequestProcessingTime = time.Duration(dur * 1000 * 1000 * 1000)
case 5:
dur, err = strconv.ParseFloat(string(tok), 64)
if err != nil {
return
}
log.BackendProcessingTime = time.Duration(dur * 1000 * 1000 * 1000)
case 6:
dur, err = strconv.ParseFloat(string(tok), 64)
if err != nil {
return
}
log.ResponseProcessingTime = time.Duration(dur * 1000 * 1000 * 1000)
case 7:
code, err = strconv.ParseInt(string(tok), 10, 32)
if err != nil {
return
}
log.ELBStatusCode = int(code)
case 8:
code, err = strconv.ParseInt(string(tok), 10, 32)
if err != nil {
return
}
log.BackendStatusCode = int(code)
case 9:
if log.ReceivedBytes, err = strconv.ParseInt(string(tok), 10, 32); err != nil {
return
}
case 10:
if log.SentBytes, err = strconv.ParseInt(string(tok), 10, 32); err != nil {
return
}
case 11:
log.Request = string(tok)
case 12:
log.UserAgent = string(tok)
case 13:
log.SSLCipher = string(tok)
case 14:
log.SSLProtocol = string(tok)
}
i++
}
return
}
// scan works like bufio.ScanWord (most of the code is taken from there),
// but treat everything between quotation marks also as a word.
func scan(data []byte, atEOF bool) (advance int, token []byte, err error) {
// Skip leading spaces.
start := 0
open := false
trim := false
for width := 0; start < len(data); start += width {
var r rune
r, width = utf8.DecodeRune(data[start:])
if r != ' ' {
break
}
}
// Scan until space, marking end of word.
for width, i := 0, start; i < len(data); i += width {
var r rune
r, width = utf8.DecodeRune(data[i:])
if r == '"' {
trim = true
open = !open
}
if r == ' ' && !open {
if trim {
return i + width, data[start+1 : i-1], nil
}
return i + width, data[start:i], nil
}
}
// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
if atEOF && len(data) > start {
return len(data), data[start:], nil
}
// Request more data.
return start, nil, nil
}
// Decoder ...
type Decoder struct {
s *bufio.Scanner
token []byte
}
// NewDecoder allocates new Decoder object for given input.
func NewDecoder(r io.Reader) *Decoder {
s := bufio.NewScanner(r)
s.Split(bufio.ScanLines)
return &Decoder{
s: s,
}
}
// Decode scans input and parse into Log. It can return EOF if underlying scanner Scan method returns false.
func (d *Decoder) Decode() (*Log, error) {
if d.token != nil {
log, err := Parse(d.token)
d.token = nil
if err != nil {
return nil, err
}
return log, nil
}
ok := d.s.Scan()
if !ok {
return nil, io.EOF
}
return Parse(d.s.Bytes())
}
// More return true if token is not empty or underlying scanner Scan method will return true.
func (d *Decoder) More() bool {
if d.token != nil {
return true
}
ok := d.s.Scan()
if ok {
d.token = d.s.Bytes()
}
return ok
}