forked from byte-power/room
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.go
111 lines (98 loc) · 2.93 KB
/
metric.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
package base
import (
"errors"
"time"
"gopkg.in/alexcesaro/statsd.v2"
)
const (
counterMetricPrefix = "counter."
timeMetricPrefix = "time."
gaugeMetricPrefix = "gauge."
histogramMetricPrefix = "histogram."
)
type MetricConfig struct {
Prefix string `yaml:"prefix"`
Host string `yaml:"host"`
Network string `yaml:"network"`
MaxPacktSize int `yaml:"max_packet_size"`
FlushPeriodSeconds int64 `yaml:"flush_period_seconds"`
SampleRate float32 `yaml:"sample_rate"`
Tags []string `yaml:"tags"`
}
func (config MetricConfig) check() error {
if config.Host == "" {
return errors.New("host should not be empty")
}
if len(config.Tags)%2 != 0 {
return errors.New("tags count should be even")
}
return nil
}
type MetricClient struct {
*statsd.Client
}
// 初始化Metric (statsd)
//
// Returns:
// - metric连接对象的指针
// - 测试连接可能出现的错误
func InitMetric(config MetricConfig) (*MetricClient, error) {
if err := config.check(); err != nil {
return nil, err
}
var opts []statsd.Option
opts = append(opts, statsd.Address(config.Host))
if config.Prefix != "" {
opts = append(opts, statsd.Prefix(config.Prefix))
}
if config.MaxPacktSize > 0 {
opts = append(opts, statsd.MaxPacketSize(config.MaxPacktSize))
}
if config.FlushPeriodSeconds > 0 {
opts = append(opts, statsd.FlushPeriod(time.Second*time.Duration(config.FlushPeriodSeconds)))
}
if config.Network != "" {
opts = append(opts, statsd.Network(config.Network))
}
if config.SampleRate > 0 {
opts = append(opts, statsd.SampleRate(config.SampleRate))
}
if len(config.Tags) > 0 {
opts = append(opts, statsd.Tags(config.Tags...))
}
c := &MetricClient{}
client, err := statsd.New(opts...)
c.Client = client
return c, err
}
// MetricCount would change count on <num> for key.
func (mc *MetricClient) MetricCount(key string, num interface{}) *MetricClient {
mc.Count(counterMetricPrefix+key, num)
return mc
}
// MetricIncrease would increase count on 1 for key with statsd count.
func (mc *MetricClient) MetricIncrease(key string) *MetricClient {
mc.Count(counterMetricPrefix+key, 1)
return mc
}
// MetricTimeDuration would record time duration for key with statsd timing.
//
// - Parameters:
// - duration: e.g. time.Now().Sub(oldTime)
func (mc *MetricClient) MetricTimeDuration(key string, duration time.Duration) *MetricClient {
ms := float64(duration) / float64(time.Millisecond)
mc.Timing(timeMetricPrefix+key, ms)
return mc
}
func (mc *MetricClient) MetricTiming(key string, value interface{}) *MetricClient {
mc.Timing(timeMetricPrefix+key, value)
return mc
}
func (mc *MetricClient) MetricGauge(bucket string, value interface{}) *MetricClient {
mc.Gauge(gaugeMetricPrefix+bucket, value)
return mc
}
func (mc *MetricClient) MetricHistogram(bucket string, value interface{}) *MetricClient {
mc.Histogram(histogramMetricPrefix+bucket, value)
return mc
}