-
Notifications
You must be signed in to change notification settings - Fork 4
/
metric.go
89 lines (71 loc) · 1.82 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
package owl
import (
"errors"
"time"
)
func initMetric() error {
if config.Metric == nil {
useMetric = false
return nil
}
if useMetric && config.Metric.StatsdUrl == "" && config.Metric.WavefrontUrl == "" {
return errors.New("owl: cannot use Metric logging with both `StatsdUrl` and `WavefrontUrl` fields empty")
}
if config.Metric.StatsdUrl != "" {
if err := initMetricStatsd(); err != nil {
return err
}
}
if config.Metric.WavefrontUrl != "" {
if err := initMetricWavefront(); err != nil {
return err
}
}
return nil
}
type MetricTimer struct {
start time.Time
}
func NewMetricTimer() *MetricTimer {
return &MetricTimer{
start: time.Now(),
}
}
func (t *MetricTimer) Stop(stat string) {
t.StopWithTags(stat, nil)
}
func (t *MetricTimer) StopWithTags(stat string, tags map[string]string) {
MetricDurationWithTags(stat, time.Now().Sub(t.start), tags)
}
func MetricDuration(stat string, delta time.Duration) {
MetricDurationWithTags(stat, delta, nil)
}
func MetricDurationWithTags(stat string, delta time.Duration, tags map[string]string) {
MetricGaugeWithTags(stat, int64(delta.Nanoseconds()/1000000), tags)
}
func MetricIncByOne(stat string) {
MetricIncByOneWithTags(stat, nil)
}
func MetricIncByOneWithTags(stat string, tags map[string]string) {
MetricIncWithTags(stat, 1, tags)
}
func MetricInc(stat string, value int64) {
MetricIncWithTags(stat, value, nil)
}
func MetricIncWithTags(stat string, value int64, tags map[string]string) {
if !useMetric {
return
}
metricIncWavefront(stat, value, tags)
metricIncStatsd(stat, value)
}
func MetricGauge(stat string, value int64) {
MetricGaugeWithTags(stat, value, nil)
}
func MetricGaugeWithTags(stat string, value int64, tags map[string]string) {
if !useMetric {
return
}
metricGaugeWavefront(stat, value, tags)
metricGaugeStatsd(stat, value)
}