-
Notifications
You must be signed in to change notification settings - Fork 4
/
datadog.go
303 lines (285 loc) · 9.04 KB
/
datadog.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 PayinTech
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package caddy_datadog
import (
"container/list"
"github.com/DataDog/datadog-go/statsd"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"reflect"
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"
)
const (
STATSD_SERVER = "127.0.0.1:8125"
STATSD_RATE = 1.0
STATSD_NAMESPACE = "caddy."
TICKER_INTERVAL = 10.0
TRACE_AGENT = "127.0.0.1:8126"
SERVICE_NAME = "caddy"
)
type DatadogModule struct {
Next httpserver.Handler
Metrics *DatadogMetrics
}
type DatadogMetrics struct {
// Known Go bug: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
// must be first field for 64 bit alignment
// on x86 and arm.
index int64
response1xxCount uint64
response2xxCount uint64
response3xxCount uint64
response4xxCount uint64
response5xxCount uint64
responseSize uint64
responseTime uint64
area []string
}
// Handle to collected metrics.
var glDatadogMetrics list.List
// Handle to the statsd client.
var glStatsdClient *statsd.Client = nil
// Handle to the plugin ticker
var glTicker *time.Ticker
// Try to retrieve metrics for the given area. If no existing
// metrics was found, a newly created metrics structure will
// be returned.
func getOrCreateMetrics(area []string) *DatadogMetrics {
if area != nil && len(area) == 0 {
return getOrCreateMetrics(nil)
}
for e := glDatadogMetrics.Front(); e != nil; e = e.Next() {
if reflect.DeepEqual(e.Value.(*DatadogMetrics).area, area) {
return e.Value.(*DatadogMetrics)
}
}
newMetrics := &DatadogMetrics{
area: area,
}
glDatadogMetrics.PushBack(newMetrics)
return newMetrics
}
// Reconfigure statsd client with the last know configuration.
// If statsd is already configured; the current connection will
// be closed and a statsd client will be reconfigured.
func reconfigureStatsdClient(server string, namespace string, tags []string) error {
var err = error(nil)
if server != "" {
if glStatsdClient != nil {
glStatsdClient.Close()
}
glStatsdClient, err = statsd.New(server)
}
if tags == nil || len(tags) == 0 {
glStatsdClient.Tags = nil
} else {
glStatsdClient.Tags = tags
}
if namespace == "" {
glStatsdClient.Namespace = STATSD_NAMESPACE
} else {
glStatsdClient.Namespace = namespace
}
return err
}
// Initialize the Datadog module by parsing the current Caddy
// configuration file.
func initializeDatadogHQ(controller *caddy.Controller) error {
hostnameRegex := regexp.MustCompile(`^[0-9a-zA-Z\\._-]{1,100}:[0-9]{1,5}$`)
tagRegex := regexp.MustCompile(`^[a-zA-Z0-9:_-]{1,100}$`)
namespaceRegex := regexp.MustCompile(`^[a-zA-Z0-9\\.\\-_]{2,25}$`)
serviceRegex := regexp.MustCompile(`^[a-zA-Z0-9\\.\\-_]{1,100}$`)
if glStatsdClient == nil {
reconfigureStatsdClient(STATSD_SERVER, STATSD_NAMESPACE, nil)
}
currentDatadogModule := DatadogModule{}
for controller.Next() {
currentDatadogModule.Metrics = getOrCreateMetrics(controller.RemainingArgs())
var statsdServer, statsdNamespace, statsdTags = "", glStatsdClient.Namespace, glStatsdClient.Tags
var traceEnabled, traceAgent, serviceName = false, TRACE_AGENT, SERVICE_NAME
for controller.NextBlock() {
switch controller.Val() {
case "statsd":
var args = controller.RemainingArgs()
if len(args) > 0 {
statsdServer = args[0]
} else {
statsdServer = STATSD_SERVER
}
if !hostnameRegex.MatchString(statsdServer) {
return controller.Err("datadog: not a valid address. Must be <hostname>:<port>")
}
case "tags":
statsdTags = controller.RemainingArgs()
for idx, tag := range statsdTags {
if !tagRegex.MatchString(tag) {
return controller.Errf("datadog: tag #%d is not valid", idx+1)
}
}
case "namespace":
var args = controller.RemainingArgs()
if len(args) > 0 {
statsdNamespace = args[0]
} else {
statsdNamespace = STATSD_NAMESPACE
}
if !strings.HasSuffix(statsdNamespace, ".") {
statsdNamespace += "."
}
if !namespaceRegex.MatchString(statsdNamespace) ||
strings.Contains(statsdNamespace, "..") ||
strings.HasPrefix(statsdNamespace, ".") {
return controller.Err("datadog: not a valid namespace")
}
case "trace_enabled":
var args = controller.RemainingArgs()
var err error
if len(args) > 0 {
traceEnabled, err = strconv.ParseBool(args[0])
} else {
traceEnabled = true
}
if err != nil {
return controller.Err("datadog: not a valid boolean value for trace_enabled")
}
case "trace_agent":
var args = controller.RemainingArgs()
if len(args) > 0 {
traceAgent = args[0]
} else {
traceAgent = TRACE_AGENT
}
if !hostnameRegex.MatchString(traceAgent) {
return controller.Err("datadog: not a valid trace_agent address. Must be <hostname>:<port>")
}
case "service_name":
var args = controller.RemainingArgs()
if len(args) > 0 {
serviceName = args[0]
} else {
serviceName = SERVICE_NAME
}
if !serviceRegex.MatchString(serviceName) {
return controller.Err("datadog: not a valid service")
}
}
}
if traceEnabled {
tracer.Start(tracer.WithAgentAddr(traceAgent), tracer.WithServiceName(serviceName))
}
reconfigureStatsdClient(statsdServer, statsdNamespace, statsdTags)
}
httpserver.
GetConfig(controller).
AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
currentDatadogModule.Next = next
return currentDatadogModule
})
if glTicker == nil {
glTicker = time.NewTicker(time.Second * TICKER_INTERVAL)
quit := make(chan struct{})
go func() {
for {
select {
case <-glTicker.C:
for e := glDatadogMetrics.Front(); e != nil; e = e.Next() {
var currentMetrics = e.Value.(*DatadogMetrics)
totalResponsesPeriod := currentMetrics.response1xxCount +
currentMetrics.response2xxCount +
currentMetrics.response3xxCount +
currentMetrics.response4xxCount +
currentMetrics.response5xxCount
glStatsdClient.Gauge(
"requests.per_s",
float64(totalResponsesPeriod)/TICKER_INTERVAL,
currentMetrics.area,
STATSD_RATE,
)
glStatsdClient.Incr(
"responses.1xx",
currentMetrics.area,
float64(currentMetrics.response1xxCount),
)
glStatsdClient.Gauge(
"responses.2xx",
float64(currentMetrics.response2xxCount),
currentMetrics.area,
STATSD_RATE,
)
glStatsdClient.Gauge(
"responses.3xx",
float64(currentMetrics.response3xxCount),
currentMetrics.area,
STATSD_RATE,
)
glStatsdClient.Gauge(
"responses.4xx",
float64(currentMetrics.response4xxCount),
currentMetrics.area,
STATSD_RATE,
)
glStatsdClient.Gauge(
"responses.5xx",
float64(currentMetrics.response5xxCount),
currentMetrics.area,
STATSD_RATE,
)
glStatsdClient.Gauge(
"responses.size_bytes",
float64(currentMetrics.responseSize),
currentMetrics.area,
STATSD_RATE,
)
if totalResponsesPeriod == 0 { // Avoid div. per zero
totalResponsesPeriod = 1
}
glStatsdClient.Gauge(
"responses.duration",
float64(currentMetrics.responseTime)/float64(totalResponsesPeriod),
currentMetrics.area,
STATSD_RATE,
)
atomic.AddUint64(¤tMetrics.response1xxCount, -currentMetrics.response1xxCount)
atomic.AddUint64(¤tMetrics.response2xxCount, -currentMetrics.response2xxCount)
atomic.AddUint64(¤tMetrics.response3xxCount, -currentMetrics.response3xxCount)
atomic.AddUint64(¤tMetrics.response4xxCount, -currentMetrics.response4xxCount)
atomic.AddUint64(¤tMetrics.response5xxCount, -currentMetrics.response5xxCount)
atomic.AddUint64(¤tMetrics.responseSize, -currentMetrics.responseSize)
currentMetrics.responseTime = 0
}
case <-quit:
glTicker.Stop()
tracer.Stop()
return
}
}
}()
}
return nil
}