-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumologreader.go
More file actions
75 lines (70 loc) · 1.83 KB
/
sumologreader.go
File metadata and controls
75 lines (70 loc) · 1.83 KB
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
package main
import (
"bufio"
"bytes"
"fmt"
"net/url"
"strings"
)
//ParseSumoLogEntry creates a new RequestLogEntry from a line in the sumo log file
func (*SumoLogReaderInfo) ParseSumoLogEntry(logLine string) (RequestLogEntry, error) {
rle := RequestLogEntry{}
vals := strings.Split(logLine, ",")
if len(vals) != 2 {
return rle, fmt.Errorf("Not enough columns in line '%s'", logLine)
}
i := strings.Index(vals[0], " ")
if i < 0 {
return rle, fmt.Errorf("Incorrect format in first column '%s'", logLine)
}
url, err := url.Parse(vals[0][i+1:])
if err != nil {
return rle, err
}
els := strings.Split(url.Path, "/")
if els[0] == "" {
els = els[1:]
}
if len(els) < 1 || els[0] == "" {
return rle, fmt.Errorf("Expecting 1 or more elements in URL path '%s'", url.Path)
}
path := "/" + strings.Join(els[1:], "/")
rle.Method = strings.ToUpper(strings.TrimSpace(vals[0][0:i]))
rle.Path = path
rle.PathElements = els[1:]
rle.URL = url
rle.Query = url.Query()
rle.Service = els[0]
rle.Response = strings.TrimSpace(vals[1])
return rle, nil
}
//SumoLogReaderInfo contains the URLReader the LogReader should read from
type SumoLogReaderInfo struct {
LogReaderInfo
}
//NewSumoLogReader returns a new instance of log reader
func NewSumoLogReader() LogReader {
return &SumoLogReaderInfo{}
}
//GetLogEntries reads the log entries from the transaction log's URL and returns a list of the
//parsed entries
func (slr *SumoLogReaderInfo) GetLogEntries() ([]RequestLogEntry, error) {
c, err := slr.URLReader.ReadFromURL()
if err != nil {
return nil, err
}
rd := bufio.NewReader(bytes.NewReader(c))
lel := []RequestLogEntry{}
for {
line, eof := rd.ReadString('\n')
rle, err := slr.ParseSumoLogEntry(line)
//Skip lines we can't parse
if err == nil {
lel = append(lel, rle)
}
if eof != nil {
break
}
}
return lel, nil
}