forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathline_tokenizer.go
189 lines (165 loc) · 4.7 KB
/
line_tokenizer.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
package drain
import (
"bytes"
"strings"
"unicode"
gologfmt "github.com/go-logfmt/logfmt"
"github.com/grafana/loki/v3/pkg/logql/log/logfmt"
)
type LineTokenizer interface {
Tokenize(line string) ([]string, interface{})
Join(tokens []string, state interface{}) string
}
type spacesTokenizer struct{}
func (spacesTokenizer) Tokenize(line string) ([]string, interface{}) {
return strings.Split(line, " "), nil
}
func (spacesTokenizer) Join(tokens []string, _ interface{}) string {
return strings.Join(tokens, " ")
}
type punctuationTokenizer struct {
includeDelimiters [128]rune
excludeDelimiters [128]rune
}
func newPunctuationTokenizer() *punctuationTokenizer {
var included [128]rune
var excluded [128]rune
included['='] = 1
excluded['_'] = 1
excluded['-'] = 1
return &punctuationTokenizer{
includeDelimiters: included,
excludeDelimiters: excluded,
}
}
func (p *punctuationTokenizer) Tokenize(line string) ([]string, interface{}) {
tokens := make([]string, len(line)) // Maximum size is every character is punctuation
spacesAfter := make([]int, strings.Count(line, " ")) // Could be a bitmap, but it's not worth it for a few bytes.
start := 0
nextTokenIdx := 0
nextSpaceIdx := 0
for i, char := range line {
if unicode.IsLetter(char) || unicode.IsNumber(char) || char < 128 && p.excludeDelimiters[char] != 0 {
continue
}
included := char < 128 && p.includeDelimiters[char] != 0
if char == ' ' || included || unicode.IsPunct(char) {
if i > start {
tokens[nextTokenIdx] = line[start:i]
nextTokenIdx++
}
if char == ' ' {
spacesAfter[nextSpaceIdx] = nextTokenIdx - 1
nextSpaceIdx++
} else {
tokens[nextTokenIdx] = line[i : i+1]
nextTokenIdx++
}
start = i + 1
}
}
if start < len(line) {
tokens[nextTokenIdx] = line[start:]
nextTokenIdx++
}
return tokens[:nextTokenIdx], spacesAfter[:nextSpaceIdx]
}
func (p *punctuationTokenizer) Join(tokens []string, state interface{}) string {
spacesAfter := state.([]int)
strBuilder := strings.Builder{}
spacesIdx := 0
for i, token := range tokens {
strBuilder.WriteString(token)
for spacesIdx < len(spacesAfter) && i == spacesAfter[spacesIdx] {
// One entry for each space following the token
strBuilder.WriteRune(' ')
spacesIdx++
}
}
return strBuilder.String()
}
type splittingTokenizer struct{}
func (splittingTokenizer) Tokenize(line string) ([]string, interface{}) {
numEquals := strings.Count(line, "=")
numColons := strings.Count(line, ":")
numSpaces := strings.Count(line, " ")
expectedTokens := numSpaces + numEquals
keyvalSeparator := "="
if numColons > numEquals {
keyvalSeparator = ":"
expectedTokens = numSpaces + numColons
}
tokens := make([]string, 0, expectedTokens)
spacesAfter := make([]int, 0, strings.Count(line, " "))
for _, token := range strings.SplitAfter(line, keyvalSeparator) {
words := strings.Split(token, " ")
for i, entry := range words {
tokens = append(tokens, entry)
if i == len(words)-1 {
continue
}
spacesAfter = append(spacesAfter, len(tokens)-1)
}
}
return tokens, spacesAfter
}
func (splittingTokenizer) Join(tokens []string, state interface{}) string {
spacesAfter := state.([]int)
strBuilder := strings.Builder{}
spacesIdx := 0
for i, token := range tokens {
strBuilder.WriteString(token)
for spacesIdx < len(spacesAfter) && i == spacesAfter[spacesIdx] {
// One entry for each space following the token
strBuilder.WriteRune(' ')
spacesIdx++
}
}
return strBuilder.String()
}
type logfmtTokenizer struct {
dec *logfmt.Decoder
varReplace string
}
func newLogfmtTokenizer(varReplace string) *logfmtTokenizer {
return &logfmtTokenizer{
dec: logfmt.NewDecoder(nil),
varReplace: varReplace,
}
}
func (t *logfmtTokenizer) Tokenize(line string) ([]string, interface{}) {
var tokens []string
t.dec.Reset([]byte(line))
for !t.dec.EOL() && t.dec.ScanKeyval() {
key := t.dec.Key()
if isTimeStampField(key) {
tokens = append(tokens, string(t.dec.Key()), t.varReplace)
continue
}
tokens = append(tokens, string(t.dec.Key()), string(t.dec.Value()))
}
if t.dec.Err() != nil {
return nil, nil
}
return tokens, nil
}
func (t *logfmtTokenizer) Join(tokens []string, state interface{}) string {
if len(tokens) == 0 {
return ""
}
if len(tokens)%2 == 1 {
tokens = append(tokens, "")
}
buf := bytes.NewBuffer(make([]byte, 0, 1024))
enc := gologfmt.NewEncoder(buf)
for i := 0; i < len(tokens); i += 2 {
k, v := tokens[i], tokens[i+1]
if err := enc.EncodeKeyval(k, v); err != nil {
return ""
}
}
return buf.String()
}
func isTimeStampField(key []byte) bool {
return bytes.EqualFold(key, []byte("ts")) || bytes.EqualFold(key, []byte("time")) || bytes.EqualFold(key, []byte("timestamp"))
}