forked from sedan07/varnish-reporter
/
record.go
55 lines (44 loc) · 1016 Bytes
/
record.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
package main
import (
"regexp"
"fmt"
"strconv"
"os"
"github.com/quipo/statsd"
"math"
)
type Record struct {
Message string
stats *statsd.StatsdBuffer
recordRegex string
}
type Stat struct {
responseTime float64
path string
}
func (record *Record) Process() {
var stat, err = record.parse()
if err == `nil` {
stat.report(record.stats)
}
}
func (record *Record) parse() (Stat, string) {
var response = regexp.MustCompile(record.recordRegex)
res := response.FindStringSubmatch(record.Message)
if len(res) == 3 {
time, err := strconv.ParseFloat(res[2], 64)
if err != nil {
fmt.Fprintln(os.Stderr, `balls!!!`, err)
}
time = time * 1000
return Stat{time, res[1]}, `nil`
} else {
return Stat{0, `null`}, `Stat did not match`
}
}
func (stat *Stat) report(statsd *statsd.StatsdBuffer) {
statsd.Timing(`requesttime.` + stat.path, int64(Round(stat.responseTime)) )
}
func Round(f float64) float64 {
return math.Floor(f + .5)
}