This repository was archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstatsd.go
129 lines (106 loc) · 3.49 KB
/
statsd.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package testutil
import (
"math"
"sync"
)
// StatsClientGaugeArgs represents arguments to a StatsClient Gauge method call.
type StatsClientGaugeArgs struct {
Name string
Value float64
Tags []string
Rate float64
}
// StatsClientCountArgs represents arguments to a StatsClient Count method call.
type StatsClientCountArgs struct {
Name string
Value int64
Tags []string
Rate float64
}
// StatsClientHistogramArgs represents arguments to a StatsClient Histogram method call.
type StatsClientHistogramArgs struct {
Name string
Value float64
Tags []string
Rate float64
}
// CountSummary contains a summary of all Count method calls to a particular StatsClient for a particular key.
type CountSummary struct {
Calls []StatsClientCountArgs
Sum int64
}
// GaugeSummary contains a summary of all Gauge method calls to a particular StatsClient for a particular key.
type GaugeSummary struct {
Calls []StatsClientGaugeArgs
Last float64
Max float64
}
// TestStatsClient is a mocked StatsClient that records all calls and replies with configurable error return values.
type TestStatsClient struct {
gaugeLock sync.Mutex
GaugeErr error
GaugeCalls []StatsClientGaugeArgs
countLock sync.RWMutex
CountErr error
CountCalls []StatsClientCountArgs
HistogramErr error
HistogramCalls []StatsClientHistogramArgs
histogramLock sync.Mutex
}
// Gauge records a call to a Gauge operation and replies with GaugeErr
func (c *TestStatsClient) Gauge(name string, value float64, tags []string, rate float64) error {
c.gaugeLock.Lock()
defer c.gaugeLock.Unlock()
c.GaugeCalls = append(c.GaugeCalls, StatsClientGaugeArgs{Name: name, Value: value, Tags: tags, Rate: rate})
return c.GaugeErr
}
// Count records a call to a Count operation and replies with CountErr
func (c *TestStatsClient) Count(name string, value int64, tags []string, rate float64) error {
c.countLock.Lock()
defer c.countLock.Unlock()
c.CountCalls = append(c.CountCalls, StatsClientCountArgs{Name: name, Value: value, Tags: tags, Rate: rate})
return c.CountErr
}
// Histogram records a call to a Histogram operation and replies with HistogramErr
func (c *TestStatsClient) Histogram(name string, value float64, tags []string, rate float64) error {
c.histogramLock.Lock()
defer c.histogramLock.Unlock()
c.HistogramCalls = append(c.HistogramCalls, StatsClientHistogramArgs{Name: name, Value: value, Tags: tags, Rate: rate})
return c.HistogramErr
}
// GetCountSummaries computes summaries for all names supplied as parameters to Count calls.
func (c *TestStatsClient) GetCountSummaries() map[string]*CountSummary {
result := map[string]*CountSummary{}
c.countLock.RLock()
defer c.countLock.RUnlock()
for _, countCall := range c.CountCalls {
name := countCall.Name
summary, ok := result[name]
if !ok {
summary = &CountSummary{}
result[name] = summary
}
summary.Calls = append(summary.Calls, countCall)
summary.Sum += countCall.Value
}
return result
}
// GetGaugeSummaries computes summaries for all names supplied as parameters to Gauge calls.
func (c *TestStatsClient) GetGaugeSummaries() map[string]*GaugeSummary {
result := map[string]*GaugeSummary{}
for _, gaugeCall := range c.GaugeCalls {
name := gaugeCall.Name
summary, ok := result[name]
if !ok {
summary = &GaugeSummary{}
summary.Max = math.MinInt64
result[name] = summary
}
summary.Calls = append(summary.Calls, gaugeCall)
summary.Last = gaugeCall.Value
if gaugeCall.Value > summary.Max {
summary.Max = gaugeCall.Value
}
}
return result
}