-
Notifications
You must be signed in to change notification settings - Fork 453
/
parser.go
313 lines (269 loc) · 8.3 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package carbon
import (
"bufio"
"errors"
"fmt"
"io"
"math"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/m3db/m3/src/x/instrument"
"github.com/m3db/m3/src/x/unsafe"
"go.uber.org/zap"
)
const (
negativeNanStr = "-nan"
nanStr = "nan"
floatFormatByte = 'f'
floatPrecision = -1
intBitSize = 64
floatBitSize = 64
intBase = 10
initScannerBufferSize = 2 << 15 // ~ 65KiB
maxScannerBufferSize = 2 << 17 // ~ 0.25iB
)
var (
errInvalidLine = errors.New("invalid line")
errNotUTF8 = errors.New("not valid UTF8 string")
mathNan = math.NaN()
)
// Metric represents a carbon metric.
type Metric struct {
Name []byte
Time time.Time
Val float64
}
// ToLine converts the carbon Metric struct to a line.
func (m *Metric) ToLine() string {
return string(m.Name) + " " + strconv.FormatFloat(m.Val, floatFormatByte, floatPrecision, floatBitSize) +
" " + strconv.FormatInt(m.Time.Unix(), intBase) + "\n"
}
// ParsePacket parses a carbon packet and returns the metrics and number of malformed lines.
func ParsePacket(packet []byte) ([]Metric, int) {
return parsePacket([]Metric{}, packet)
}
// ParseAndAppendPacket does the same thing as parse packet, but it allows the caller to pass
// in the []Metric to facilitate pooling.
func ParseAndAppendPacket(mets []Metric, packet []byte) ([]Metric, int) {
return parsePacket(mets, packet)
}
func parsePacket(mets []Metric, packet []byte) ([]Metric, int) {
var malformed, prevIdx, i int
for i = 0; i < len(packet); i++ {
if packet[i] == '\n' {
if (i - prevIdx) > 1 {
name, timestamp, value, err := Parse(packet[prevIdx:i])
if err == nil {
mets = append(mets, Metric{
Name: name,
Time: timestamp,
Val: value,
})
} else {
malformed++
}
}
prevIdx = i + 1
}
}
if (i - prevIdx) > 1 {
name, timestamp, value, err := Parse(packet[prevIdx:i])
if err == nil {
mets = append(mets, Metric{
Name: name,
Time: timestamp,
Val: value,
})
} else {
malformed++
}
}
return mets, malformed
}
// ParseName parses out the name portion of a string and returns the
// name and the remaining portion of the line.
func ParseName(line []byte) (name []byte, rest []byte, err error) {
firstSepIdx := -1
for i := 0; i < len(line); i++ {
if line[i] == ' ' && !(i != 0 && line[i-1] == ' ') {
firstSepIdx = i
break
}
}
if firstSepIdx == -1 {
err = errInvalidLine
return
}
name = line[:firstSepIdx]
if len(name) == 0 {
err = errInvalidLine
return
}
if !utf8.Valid(name) {
err = errNotUTF8
return
}
nonSpaceIdx := firstSepIdx + 1
for nonSpaceIdx < len(line) && line[nonSpaceIdx] == ' ' {
nonSpaceIdx++
}
rest = line[nonSpaceIdx:]
return
}
// ParseRemainder parses a line's components (name and remainder) and returns
// all but the name and returns the timestamp of the metric, its value, the
// time it was received and any error encountered.
func ParseRemainder(rest []byte) (timestamp time.Time, value float64, err error) {
if !utf8.Valid(rest) {
err = errNotUTF8
return
}
// Determine the start and end offsets for the value.
valStart, valEnd := parseWordOffsets(rest)
if valStart == -1 || valEnd == -1 || valEnd >= len(rest) {
// If we couldn't determine the offsets, or the end of the value is also
// the end of the line, then this is an invalid line.
err = errInvalidLine
return
}
// Found valid offsets for the value, try and parse it into a float. Note that
// we use unsafe.WithString() so that we can use standard library functions
// without allocating a string.
unsafe.WithString(rest, func(s string) {
if val := strings.ToLower(s[valStart:valEnd]); val == negativeNanStr || val == nanStr {
value = mathNan
} else {
value, err = strconv.ParseFloat(s[valStart:valEnd], floatBitSize)
}
})
if err != nil {
return
}
// Determine the start and end offsets for the timestamp (seconds).
rest = rest[valEnd:]
secStart, secEnd := parseWordOffsets(rest)
if secStart == -1 || secEnd == -1 || secEnd != len(rest) {
// If we couldn't determine the offsets, or the end of the the timestamp
// is not the end of the line (I.E there are still characters after the end
// of the timestamp), then this is an invalid line.
err = errInvalidLine
return
}
// Found valid offsets for the timestamp, try and parse it into an integer. Note that
// we use unsafe.WithString() so that we can use standard library functions without
// allocating a string.
var tsInSecs int64
unsafe.WithString(rest, func(s string) {
tsInSecs, err = strconv.ParseInt(s[secStart:secEnd], intBase, intBitSize)
if err != nil {
err = fmt.Errorf("invalid timestamp %s: %v", rest[secStart:secEnd], err)
}
})
if err != nil {
return
}
timestamp = time.Unix(tsInSecs, 0)
return
}
// Parse parses a carbon line into the corresponding parts.
func Parse(line []byte) (name []byte, timestamp time.Time, value float64, err error) {
var rest []byte
name, rest, err = ParseName(line)
if err != nil {
return
}
timestamp, value, err = ParseRemainder(rest)
return
}
// A Scanner is used to scan carbon lines from an underlying io.Reader.
type Scanner struct {
scanner *bufio.Scanner
timestamp time.Time
path []byte
value float64
// The number of malformed metrics encountered.
MalformedCount int
iOpts instrument.Options
}
// NewScanner creates a new carbon scanner.
func NewScanner(r io.Reader, iOpts instrument.Options) *Scanner {
s := bufio.NewScanner(r)
// Force the scanner to use a large buffer upfront to reduce the number of
// syscalls that occur if the io.Reader is backed by something that requires
// I/O (like a TCP connection).
// TODO(rartoul): Make this configurable.
s.Buffer(make([]byte, 0, initScannerBufferSize), maxScannerBufferSize)
s.Split(bufio.ScanLines)
return &Scanner{scanner: s, iOpts: iOpts}
}
// Scan scans for the next carbon metric. Malformed metrics are skipped but counted.
func (s *Scanner) Scan() bool {
for {
if !s.scanner.Scan() {
return false
}
var err error
if s.path, s.timestamp, s.value, err = Parse(s.scanner.Bytes()); err != nil {
s.iOpts.Logger().Error("error trying to scan malformed carbon line",
zap.String("line", string(s.path)), zap.Error(err))
s.MalformedCount++
continue
}
return true
}
}
// Metric returns the path, timestamp, and value of the last parsed metric.
func (s *Scanner) Metric() ([]byte, time.Time, float64) {
return s.path, s.timestamp, s.value
}
// Err returns any errors in the scan.
func (s *Scanner) Err() error { return s.scanner.Err() }
// parseWordOffsets scans through b searching for the start and end offsets
// of the next "word" (ignores spaces on either side), returning offsets
// such that b[start:end] will return the complete word with no spaces. Note
// that the function will tolerate any number of spaces on either side.
func parseWordOffsets(b []byte) (int, int) {
valStart := -1
for i := 0; i < len(b); i++ {
charByte := b[i]
if valStart == -1 && charByte != ' ' {
valStart = i
break
}
}
valEnd := valStart
reachedEnd := true
for i := valStart + 1; i < len(b); i++ {
valEnd = i
charByte := b[i]
if charByte == ' ' {
reachedEnd = false
break
}
}
if reachedEnd {
valEnd = valEnd + 1
}
return valStart, valEnd
}