-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorders.go
58 lines (51 loc) · 1.54 KB
/
recorders.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
package main
import (
"log"
"strings"
"github.com/cactus/go-statsd-client/statsd"
)
// This interface is used to represent the functionality of clients/objects
// that send information to be persisted or further processed.
type Recorder interface {
pushStat(stat string, value int64) bool
cleanURI(input string) string
validStat(stat string) bool
}
type StatsDRecorder struct {
Statter statsd.Statter
}
// Push stats to StatsD.
// This assumes that the data being written is always timing data and we are
// always collecting all the samples.
func (statsd StatsDRecorder) pushStat(stat string, value int64) bool {
if statsd.Statter != nil {
err := statsd.Statter.Timing(stat, value, 1.0)
if err != nil {
log.Fatal("there was an error sending the statsd timing", err)
return false
}
}
return true
}
// The valid page-uri checker for StatsD. We don't want to accept anything
// that the storage would have trouble handing.
func (statsd StatsDRecorder) validStat(stat string) bool {
return !strings.ContainsAny(stat, "&#") && strings.Index(stat, "//") == -1
}
// Clean up the page-uri.
// This will strip the leading and trailing /'s and replace the rest with '.'
func (statsd StatsDRecorder) cleanURI(input string) string {
//replace rightmost / with "index"
ret := input
if last := len(ret) - 1; last >= 0 && ret[last] == '/' {
ret = ret + "index"
}
ret = strings.TrimLeft(ret, "/")
ret = strings.Split(ret, ".")[0]
ret = strings.Replace(ret, "/", ".", -1)
ret = strings.ToLower(ret)
if len(ret) > 0 {
ret = ret + "."
}
return ret
}