This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparser.go
162 lines (151 loc) · 4.31 KB
/
parser.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
package dendrite
import (
"crypto/sha1"
"encoding/base64"
"github.com/fizx/logs"
"regexp"
"strconv"
"sync/atomic"
"time"
)
type Column struct {
Type FieldType
Treatment FieldTreatment
Value interface{}
}
type Record map[string]Column
type Parser interface {
Consume(bytes []byte, counter *int64)
}
type RegexpParser struct {
group string
compiled *regexp.Regexp
output chan Record
buffer []byte
fields []FieldConfig
file string
hostname string
maxLineSize int
}
func NewRegexpParser(hostname string, group string, file string, output chan Record, pattern string, fields []FieldConfig, maxLineSize int64) Parser {
parser := new(RegexpParser)
parser.maxLineSize = int(maxLineSize)
parser.hostname = hostname
parser.file = file
parser.group = group
parser.output = output
parser.buffer = make([]byte, 0)
re, err := regexp.Compile(pattern)
if err != nil {
panic(err)
} else {
parser.compiled = re
for i, name := range re.SubexpNames() {
if name != "" {
found := false
for n, spec := range fields {
if spec.Name == "" {
spec.Name = spec.Alias
}
if name == spec.Name {
found = true
fields[n].Group = i
logs.Debug("setting group alias: %s, name: %s, group: %d", spec.Alias, spec.Name, spec.Group)
}
}
if !found {
var spec FieldConfig
spec.Group = i
spec.Alias = name
spec.Type = String
fields = append(fields, spec)
}
}
}
}
parser.fields = fields
for _, f := range parser.fields {
logs.Debug("p.f: alias: %s, name: %s, group: %d, type: %d", f.Alias, f.Name, f.Group, f.Type)
}
return parser
}
func (parser *RegexpParser) Consume(bytes []byte, counter *int64) {
parser.buffer = append(parser.buffer, bytes...)
logs.Debug("consuming %d bytes of %s", len(bytes), parser.file)
l := len(parser.buffer)
if l > parser.maxLineSize {
off := l - parser.maxLineSize
logs.Debug("chopping %d bytes off buffer (was: %d, max: %d)", off, l, parser.maxLineSize)
atomic.AddInt64(counter, int64(off))
parser.buffer = parser.buffer[off:]
}
for {
m := parser.compiled.FindSubmatchIndex(parser.buffer)
if m == nil {
return
}
hasher := sha1.New()
out := make(map[string]Column)
out["_offset"] = Column{Integer, Simple, atomic.LoadInt64(counter)}
out["_file"] = Column{String, Simple, parser.file}
out["_time"] = Column{Timestamp, Simple, StandardTimeProvider.Now().Unix()}
out["_group"] = Column{String, Simple, parser.group}
out["_hostname"] = Column{String, Simple, parser.hostname}
for _, spec := range parser.fields {
g := spec.Group
if g < 0 || g > len(m)/2 {
logs.Error("spec group out of range: alias: %s, name: %s, g: %d", spec.Alias, spec.Name, g)
panic(-1)
}
if m[g*2] == -1 {
continue
}
s := string(parser.buffer[m[g*2]:m[g*2+1]])
switch spec.Type {
case Timestamp:
t, err := time.Parse(spec.Format, s)
if err != nil {
logs.Warn("date parse error: %s", err)
} else {
if t.Year() == 0 {
now := StandardTimeProvider.Now()
adjusted := time.Date(now.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
if adjusted.After(now) {
adjusted = time.Date(now.Year()-1, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}
t = adjusted
}
out[spec.Alias] = Column{Timestamp, spec.Treatment, t.Unix()}
}
case String:
if spec.Treatment == Tokens {
out[spec.Alias] = Column{Tokens, spec.Treatment, spec.Pattern.FindAllString(s, -1)}
} else if spec.Treatment == Hash {
hasher.Reset()
hasher.Write([]byte(spec.Salt))
hasher.Write([]byte(s))
sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
out[spec.Alias] = Column{Tokens, spec.Treatment, sha}
} else {
out[spec.Alias] = Column{String, spec.Treatment, s}
}
case Integer:
n, err := strconv.ParseInt(s, 10, 64)
if err == nil {
out[spec.Alias] = Column{spec.Type, spec.Treatment, n}
}
case Double:
n, err := strconv.ParseFloat(s, 64)
if err == nil {
out[spec.Alias] = Column{spec.Type, spec.Treatment, n}
}
default:
panic(nil)
}
}
parser.output <- out
atomic.AddInt64(counter, int64(m[1]))
parser.buffer = parser.buffer[m[1]:]
}
logs.Debug("done with %s", parser.file)
}