@@ -10,7 +10,6 @@ import (
10
10
"net"
11
11
"os"
12
12
"os/signal"
13
- "regexp"
14
13
"runtime"
15
14
"sort"
16
15
"strconv"
@@ -278,48 +277,94 @@ func processTimers(buffer *bytes.Buffer, now int64, pctls Percentiles) int64 {
278
277
return num
279
278
}
280
279
281
- var packetRegexp = regexp .MustCompile ("^([^:]+):(-?[0-9]+)\\ |(g|c|ms)(\\ |@([0-9\\ .]+))?\n ?$" )
282
-
283
280
func parseMessage (data []byte ) []* Packet {
284
- var output []* Packet
281
+ var (
282
+ output []* Packet
283
+ input []byte
284
+ )
285
+
285
286
for _ , line := range bytes .Split (data , []byte ("\n " )) {
286
287
if len (line ) == 0 {
287
288
continue
288
289
}
290
+ input = line
289
291
290
- item := packetRegexp .FindSubmatch (line )
291
- if len (item ) == 0 {
292
+ index := bytes .IndexByte (input , ':' )
293
+ if index < 0 {
294
+ if * debug {
295
+ log .Printf ("ERROR: failed to parse line: %s\n " , string (line ))
296
+ }
292
297
continue
293
298
}
294
299
295
- var err error
296
- var value interface {}
297
- modifier := string (item [3 ])
298
- switch modifier {
299
- case "c" :
300
- value , err = strconv .ParseInt (string (item [2 ]), 10 , 64 )
300
+ name := input [:index ]
301
+
302
+ index ++
303
+ input = input [index :]
304
+
305
+ index = bytes .IndexByte (input , '|' )
306
+ if index < 0 {
307
+ if * debug {
308
+ log .Printf ("ERROR: failed to parse line: %s\n " , string (line ))
309
+ }
310
+ continue
311
+ }
312
+
313
+ val := input [:index ]
314
+ index ++
315
+
316
+ var mtypeStr string
317
+
318
+ if input [index ] == 'm' {
319
+ index ++
320
+ if index >= len (input ) || input [index ] != 's' {
321
+ if * debug {
322
+ log .Printf ("ERROR: failed to parse line: %s\n " , string (line ))
323
+ }
324
+ continue
325
+ }
326
+ mtypeStr = "ms"
327
+ } else {
328
+ mtypeStr = string (input [index ])
329
+ }
330
+
331
+ index ++
332
+ input = input [index :]
333
+
334
+ var (
335
+ value interface {}
336
+ err error
337
+ )
338
+
339
+ if mtypeStr [0 ] == 'c' {
340
+ value , err = strconv .ParseInt (string (val ), 10 , 64 )
301
341
if err != nil {
302
- log .Printf ("ERROR: failed to ParseInt %s - %s" , item [ 2 ] , err )
342
+ log .Printf ("ERROR: failed to ParseInt %s - %s" , string ( val ) , err )
303
343
continue
304
344
}
305
- default :
306
- value , err = strconv .ParseUint (string (item [ 2 ] ), 10 , 64 )
345
+ } else {
346
+ value , err = strconv .ParseUint (string (val ), 10 , 64 )
307
347
if err != nil {
308
- log .Printf ("ERROR: failed to ParseUint %s - %s" , item [ 2 ] , err )
348
+ log .Printf ("ERROR: failed to ParseUint %s - %s" , string ( val ) , err )
309
349
continue
310
350
}
311
351
}
312
352
313
- sampleRate , err := strconv .ParseFloat (string (item [5 ]), 32 )
314
- if err != nil {
315
- sampleRate = 1
353
+ var sampleRate float32 = 1
354
+
355
+ if len (input ) > 0 && bytes .HasPrefix (input , []byte ("|@" )) {
356
+ input = input [2 :]
357
+ rate , err := strconv .ParseFloat (string (input ), 32 )
358
+ if err == nil {
359
+ sampleRate = float32 (rate )
360
+ }
316
361
}
317
362
318
363
packet := & Packet {
319
- Bucket : * prefix + string (item [ 1 ] ),
364
+ Bucket : * prefix + string (name ),
320
365
Value : value ,
321
- Modifier : modifier ,
322
- Sampling : float32 ( sampleRate ) ,
366
+ Modifier : mtypeStr ,
367
+ Sampling : sampleRate ,
323
368
}
324
369
output = append (output , packet )
325
370
}
0 commit comments