/
statsd_wrapper.go
54 lines (43 loc) · 1.13 KB
/
statsd_wrapper.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
package statsd_wrapper
import (
"errors"
"fmt"
"github.com/cactus/go-statsd-client/statsd"
"regexp"
)
const (
//Default Ip Address and port for Statsd
DefaultStatsdAddr = "127.0.0.1:8125"
//Default Directory name for metrics in Graphite
DefaultStatsdDir = "default"
)
type Config struct {
Addr string
Dir string
}
//we are defining StatsdClient interface. This way we can use stubbed type in testing.
type Client interface {
Inc(stat string, value int64, rate float32) error
Dec(stat string, value int64, rate float32) error
Gauge(stat string, value int64, rate float32) error
Timing(stat string, value int64, rate float32) error
Close() error
}
func CreateStatsdClient(config *Config) (Client, error) {
var client Client
var err error
if config != nil {
client, err = statsd.Dial(config.Addr, config.Dir)
} else {
err = errors.New(" Configuration parameters for StatsD are not specified")
}
return client, err
}
func CleanString(name string) (cleanName string) {
reg, err := regexp.Compile("[%,\\/,\\\\,\\.]")
if err != nil {
fmt.Println(err)
}
cleanName = reg.ReplaceAllString(name, "-")
return cleanName
}