-
Notifications
You must be signed in to change notification settings - Fork 35
/
statsd.go
175 lines (136 loc) · 4.09 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// This file only wraps all the statsd client call to be used directly in the form of
// telemetry.Gauge() instead of telemetry.statsd.Gauge()
// It does NOT implement all the function from the statsd.ClientInterface interface
// because some of these are never going to be used in this application
package statsd
import (
"time"
"github.com/DataDog/KubeHound/pkg/telemetry/log"
"github.com/DataDog/datadog-go/v5/statsd"
)
var (
// statsdClient is the global statsd statsdClient.
statsdClient statsd.ClientInterface
)
// just to make sure we have a client that does nothing by default
func init() {
statsdClient = &NoopClient{}
}
func Setup(statsdURL string) error {
log.I.Infof("Using %s for statsd URL", statsdURL)
var err error
// In case we don't have a statsd url set, we just want to continue, but log that we aren't going to submit metrics.
if statsdURL == "" {
log.I.Warn("No metrics collector has been setup. All metrics submission are going to be NOOP.")
return nil
}
statsdClient, err = statsd.New(statsdURL)
if err != nil {
return err
}
return nil
}
// Count tracks how many times something happened per second.
func Count(name string, value int64, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.Count(name, value, tags, rate)
}
// Gauge measures the value of a metric at a particular time.
func Gauge(name string, value float64, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.Gauge(name, value, tags, rate)
}
// Incr is just Count of 1
func Incr(name string, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.Incr(name, tags, rate)
}
// Decr is just Count of -1
func Decr(name string, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.Decr(name, tags, rate)
}
// Histogram tracks the statistical distribution of a set of values.
func Histogram(name string, value float64, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.Histogram(name, value, tags, rate)
}
// Event sends the provided Event.
func Event(event *statsd.Event) error {
if statsdClient == nil {
return nil
}
return statsdClient.Event(event)
}
// SimpleEvent sends an event with the provided title and text.
func SimpleEvent(title string, text string) error {
if statsdClient == nil {
return nil
}
return statsdClient.SimpleEvent(title, text)
}
// Set counts the number of unique elements in a group.
func Set(name string, value string, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.Set(name, value, tags, rate)
}
// Timing sends timing information, it is an alias for TimeInMilliseconds
func Timing(name string, value time.Duration, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.Timing(name, value, tags, rate)
}
// TimingDist sends dt in milliseconds as a distribution (p50-p99)
func TimingDist(name string, dt time.Duration, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
const secToMillis = 1000
return statsdClient.Distribution(name, dt.Seconds()*secToMillis, tags, rate)
}
// TimeInMilliseconds sends timing information in milliseconds.
func TimeInMilliseconds(name string, value float64, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.TimeInMilliseconds(name, value, tags, rate)
}
// Distribution tracks accurate global percentiles of a set of values.
func Distribution(name string, value float64, tags []string, rate float64) error {
if statsdClient == nil {
return nil
}
return statsdClient.Distribution(name, value, tags, rate)
}
// Flush flushes any pending stats in the statsd client.
func Flush() error {
if statsdClient == nil {
return nil
}
return statsdClient.Flush()
}
func IsClosed() bool {
if statsdClient == nil {
return false
}
return statsdClient.IsClosed()
}
func Close() error {
if statsdClient == nil {
return nil
}
return statsdClient.Close()
}