-
Notifications
You must be signed in to change notification settings - Fork 698
/
metrics.go
378 lines (312 loc) · 12.6 KB
/
metrics.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package config
import (
"time"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/metrics"
prometheusmetrics "github.com/prebid/prebid-server/metrics/prometheus"
"github.com/prebid/prebid-server/openrtb_ext"
gometrics "github.com/rcrowley/go-metrics"
influxdb "github.com/vrischmann/go-metrics-influxdb"
)
// NewMetricsEngine reads the configuration and returns the appropriate metrics engine
// for this instance.
func NewMetricsEngine(cfg *config.Configuration, adapterList []openrtb_ext.BidderName, syncerKeys []string) *DetailedMetricsEngine {
// Create a list of metrics engines to use.
// Capacity of 2, as unlikely to have more than 2 metrics backends, and in the case
// of 1 we won't use the list so it will be garbage collected.
engineList := make(MultiMetricsEngine, 0, 2)
returnEngine := DetailedMetricsEngine{}
if cfg.Metrics.Influxdb.Host != "" {
// Currently use go-metrics as the metrics piece for influx
returnEngine.GoMetrics = metrics.NewMetrics(gometrics.NewPrefixedRegistry("prebidserver."), adapterList, cfg.Metrics.Disabled, syncerKeys)
engineList = append(engineList, returnEngine.GoMetrics)
// Set up the Influx logger
go influxdb.InfluxDB(
returnEngine.GoMetrics.MetricsRegistry, // metrics registry
time.Second*time.Duration(cfg.Metrics.Influxdb.MetricSendInterval), // Configurable interval
cfg.Metrics.Influxdb.Host, // the InfluxDB url
cfg.Metrics.Influxdb.Database, // your InfluxDB database
cfg.Metrics.Influxdb.Measurement, // your measurement
cfg.Metrics.Influxdb.Username, // your InfluxDB user
cfg.Metrics.Influxdb.Password, // your InfluxDB password,
cfg.Metrics.Influxdb.AlignTimestamps, // align timestamps
)
// Influx is not added to the engine list as goMetrics takes care of it already.
}
if cfg.Metrics.Prometheus.Port != 0 {
// Set up the Prometheus metrics.
returnEngine.PrometheusMetrics = prometheusmetrics.NewMetrics(cfg.Metrics.Prometheus, cfg.Metrics.Disabled, syncerKeys)
engineList = append(engineList, returnEngine.PrometheusMetrics)
}
// Now return the proper metrics engine
if len(engineList) > 1 {
returnEngine.MetricsEngine = &engineList
} else if len(engineList) == 1 {
returnEngine.MetricsEngine = engineList[0]
} else {
returnEngine.MetricsEngine = &NilMetricsEngine{}
}
return &returnEngine
}
// DetailedMetricsEngine is a MultiMetricsEngine that preserves links to underlying metrics engines.
type DetailedMetricsEngine struct {
metrics.MetricsEngine
GoMetrics *metrics.Metrics
PrometheusMetrics *prometheusmetrics.Metrics
}
// MultiMetricsEngine logs metrics to multiple metrics databases The can be useful in transitioning
// an instance from one engine to another, you can run both in parallel to verify stats match up.
type MultiMetricsEngine []metrics.MetricsEngine
// RecordRequest across all engines
func (me *MultiMetricsEngine) RecordRequest(labels metrics.Labels) {
for _, thisME := range *me {
thisME.RecordRequest(labels)
}
}
func (me *MultiMetricsEngine) RecordConnectionAccept(success bool) {
for _, thisME := range *me {
thisME.RecordConnectionAccept(success)
}
}
func (me *MultiMetricsEngine) RecordConnectionClose(success bool) {
for _, thisME := range *me {
thisME.RecordConnectionClose(success)
}
}
//RecordsImps records imps with imp types across all metric engines
func (me *MultiMetricsEngine) RecordImps(implabels metrics.ImpLabels) {
for _, thisME := range *me {
thisME.RecordImps(implabels)
}
}
// RecordRequestTime across all engines
func (me *MultiMetricsEngine) RecordRequestTime(labels metrics.Labels, length time.Duration) {
for _, thisME := range *me {
thisME.RecordRequestTime(labels, length)
}
}
// RecordStoredDataFetchTime across all engines
func (me *MultiMetricsEngine) RecordStoredDataFetchTime(labels metrics.StoredDataLabels, length time.Duration) {
for _, thisME := range *me {
thisME.RecordStoredDataFetchTime(labels, length)
}
}
// RecordStoredDataError across all engines
func (me *MultiMetricsEngine) RecordStoredDataError(labels metrics.StoredDataLabels) {
for _, thisME := range *me {
thisME.RecordStoredDataError(labels)
}
}
// RecordAdapterPanic across all engines
func (me *MultiMetricsEngine) RecordAdapterPanic(labels metrics.AdapterLabels) {
for _, thisME := range *me {
thisME.RecordAdapterPanic(labels)
}
}
// RecordAdapterRequest across all engines
func (me *MultiMetricsEngine) RecordAdapterRequest(labels metrics.AdapterLabels) {
for _, thisME := range *me {
thisME.RecordAdapterRequest(labels)
}
}
// Keeps track of created and reused connections to adapter bidders and the time from the
// connection request, to the connection creation, or reuse from the pool across all engines
func (me *MultiMetricsEngine) RecordAdapterConnections(bidderName openrtb_ext.BidderName, connWasReused bool, connWaitTime time.Duration) {
for _, thisME := range *me {
thisME.RecordAdapterConnections(bidderName, connWasReused, connWaitTime)
}
}
// Times the DNS resolution process
func (me *MultiMetricsEngine) RecordDNSTime(dnsLookupTime time.Duration) {
for _, thisME := range *me {
thisME.RecordDNSTime(dnsLookupTime)
}
}
func (me *MultiMetricsEngine) RecordTLSHandshakeTime(tlsHandshakeTime time.Duration) {
for _, thisME := range *me {
thisME.RecordTLSHandshakeTime(tlsHandshakeTime)
}
}
// RecordAdapterBidReceived across all engines
func (me *MultiMetricsEngine) RecordAdapterBidReceived(labels metrics.AdapterLabels, bidType openrtb_ext.BidType, hasAdm bool) {
for _, thisME := range *me {
thisME.RecordAdapterBidReceived(labels, bidType, hasAdm)
}
}
// RecordAdapterPrice across all engines
func (me *MultiMetricsEngine) RecordAdapterPrice(labels metrics.AdapterLabels, cpm float64) {
for _, thisME := range *me {
thisME.RecordAdapterPrice(labels, cpm)
}
}
// RecordAdapterTime across all engines
func (me *MultiMetricsEngine) RecordAdapterTime(labels metrics.AdapterLabels, length time.Duration) {
for _, thisME := range *me {
thisME.RecordAdapterTime(labels, length)
}
}
// RecordCookieSync across all engines
func (me *MultiMetricsEngine) RecordCookieSync(status metrics.CookieSyncStatus) {
for _, thisME := range *me {
thisME.RecordCookieSync(status)
}
}
// RecordSyncerRequest across all engines
func (me *MultiMetricsEngine) RecordSyncerRequest(key string, status metrics.SyncerCookieSyncStatus) {
for _, thisME := range *me {
thisME.RecordSyncerRequest(key, status)
}
}
// RecordSetUid across all engines
func (me *MultiMetricsEngine) RecordSetUid(status metrics.SetUidStatus) {
for _, thisME := range *me {
thisME.RecordSetUid(status)
}
}
// RecordSyncerSet across all engines
func (me *MultiMetricsEngine) RecordSyncerSet(key string, status metrics.SyncerSetUidStatus) {
for _, thisME := range *me {
thisME.RecordSyncerSet(key, status)
}
}
// RecordStoredReqCacheResult across all engines
func (me *MultiMetricsEngine) RecordStoredReqCacheResult(cacheResult metrics.CacheResult, inc int) {
for _, thisME := range *me {
thisME.RecordStoredReqCacheResult(cacheResult, inc)
}
}
// RecordStoredImpCacheResult across all engines
func (me *MultiMetricsEngine) RecordStoredImpCacheResult(cacheResult metrics.CacheResult, inc int) {
for _, thisME := range *me {
thisME.RecordStoredImpCacheResult(cacheResult, inc)
}
}
// RecordAccountCacheResult across all engines
func (me *MultiMetricsEngine) RecordAccountCacheResult(cacheResult metrics.CacheResult, inc int) {
for _, thisME := range *me {
thisME.RecordAccountCacheResult(cacheResult, inc)
}
}
// RecordPrebidCacheRequestTime across all engines
func (me *MultiMetricsEngine) RecordPrebidCacheRequestTime(success bool, length time.Duration) {
for _, thisME := range *me {
thisME.RecordPrebidCacheRequestTime(success, length)
}
}
// RecordRequestQueueTime across all engines
func (me *MultiMetricsEngine) RecordRequestQueueTime(success bool, requestType metrics.RequestType, length time.Duration) {
for _, thisME := range *me {
thisME.RecordRequestQueueTime(success, requestType, length)
}
}
// RecordTimeoutNotice across all engines
func (me *MultiMetricsEngine) RecordTimeoutNotice(success bool) {
for _, thisME := range *me {
thisME.RecordTimeoutNotice(success)
}
}
// RecordRequestPrivacy across all engines
func (me *MultiMetricsEngine) RecordRequestPrivacy(privacy metrics.PrivacyLabels) {
for _, thisME := range *me {
thisME.RecordRequestPrivacy(privacy)
}
}
// RecordAdapterGDPRRequestBlocked across all engines
func (me *MultiMetricsEngine) RecordAdapterGDPRRequestBlocked(adapter openrtb_ext.BidderName) {
for _, thisME := range *me {
thisME.RecordAdapterGDPRRequestBlocked(adapter)
}
}
// RecordDebugRequest across all engines
func (me *MultiMetricsEngine) RecordDebugRequest(debugEnabled bool, pubId string) {
for _, thisME := range *me {
thisME.RecordDebugRequest(debugEnabled, pubId)
}
}
// NilMetricsEngine implements the MetricsEngine interface where no metrics are actually captured. This is
// used if no metric backend is configured and also for tests.
type NilMetricsEngine struct{}
// RecordRequest as a noop
func (me *NilMetricsEngine) RecordRequest(labels metrics.Labels) {
}
// RecordConnectionAccept as a noop
func (me *NilMetricsEngine) RecordConnectionAccept(success bool) {
}
// RecordConnectionClose as a noop
func (me *NilMetricsEngine) RecordConnectionClose(success bool) {
}
// RecordImps as a noop
func (me *NilMetricsEngine) RecordImps(implabels metrics.ImpLabels) {
}
// RecordRequestTime as a noop
func (me *NilMetricsEngine) RecordRequestTime(labels metrics.Labels, length time.Duration) {
}
// RecordStoredDataFetchTime as a noop
func (me *NilMetricsEngine) RecordStoredDataFetchTime(labels metrics.StoredDataLabels, length time.Duration) {
}
// RecordStoredDataError as a noop
func (me *NilMetricsEngine) RecordStoredDataError(labels metrics.StoredDataLabels) {
}
// RecordAdapterPanic as a noop
func (me *NilMetricsEngine) RecordAdapterPanic(labels metrics.AdapterLabels) {
}
// RecordAdapterRequest as a noop
func (me *NilMetricsEngine) RecordAdapterRequest(labels metrics.AdapterLabels) {
}
// RecordAdapterConnections as a noop
func (me *NilMetricsEngine) RecordAdapterConnections(bidderName openrtb_ext.BidderName, connWasReused bool, connWaitTime time.Duration) {
}
// RecordDNSTime as a noop
func (me *NilMetricsEngine) RecordDNSTime(dnsLookupTime time.Duration) {
}
// RecordTLSHandshakeTime as a noop
func (me *NilMetricsEngine) RecordTLSHandshakeTime(tlsHandshakeTime time.Duration) {
}
// RecordAdapterBidReceived as a noop
func (me *NilMetricsEngine) RecordAdapterBidReceived(labels metrics.AdapterLabels, bidType openrtb_ext.BidType, hasAdm bool) {
}
// RecordAdapterPrice as a noop
func (me *NilMetricsEngine) RecordAdapterPrice(labels metrics.AdapterLabels, cpm float64) {
}
// RecordAdapterTime as a noop
func (me *NilMetricsEngine) RecordAdapterTime(labels metrics.AdapterLabels, length time.Duration) {
}
// RecordCookieSync as a noop
func (me *NilMetricsEngine) RecordCookieSync(status metrics.CookieSyncStatus) {
}
// RecordSyncerRequest as a noop
func (me *NilMetricsEngine) RecordSyncerRequest(key string, status metrics.SyncerCookieSyncStatus) {
}
// RecordSetUid as a noop
func (me *NilMetricsEngine) RecordSetUid(status metrics.SetUidStatus) {
}
// RecordSyncerSet as a noop
func (me *NilMetricsEngine) RecordSyncerSet(key string, status metrics.SyncerSetUidStatus) {
}
// RecordStoredReqCacheResult as a noop
func (me *NilMetricsEngine) RecordStoredReqCacheResult(cacheResult metrics.CacheResult, inc int) {
}
// RecordStoredImpCacheResult as a noop
func (me *NilMetricsEngine) RecordStoredImpCacheResult(cacheResult metrics.CacheResult, inc int) {
}
// RecordAccountCacheResult as a noop
func (me *NilMetricsEngine) RecordAccountCacheResult(cacheResult metrics.CacheResult, inc int) {
}
// RecordPrebidCacheRequestTime as a noop
func (me *NilMetricsEngine) RecordPrebidCacheRequestTime(success bool, length time.Duration) {
}
// RecordRequestQueueTime as a noop
func (me *NilMetricsEngine) RecordRequestQueueTime(success bool, requestType metrics.RequestType, length time.Duration) {
}
// RecordTimeoutNotice as a noop
func (me *NilMetricsEngine) RecordTimeoutNotice(success bool) {
}
// RecordRequestPrivacy as a noop
func (me *NilMetricsEngine) RecordRequestPrivacy(privacy metrics.PrivacyLabels) {
}
// RecordAdapterGDPRRequestBlocked as a noop
func (me *NilMetricsEngine) RecordAdapterGDPRRequestBlocked(adapter openrtb_ext.BidderName) {
}
// RecordDebugRequest as a noop
func (me *NilMetricsEngine) RecordDebugRequest(debugEnabled bool, pubId string) {
}