-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathspan_stats.spec.js
More file actions
415 lines (367 loc) · 11.6 KB
/
Copy pathspan_stats.spec.js
File metadata and controls
415 lines (367 loc) · 11.6 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
'use strict'
const assert = require('node:assert/strict')
const { hostname } = require('os')
const { describe, it } = require('mocha')
const sinon = require('sinon')
const proxyquire = require('proxyquire')
require('./setup/core')
const { LogCollapsingLowestDenseDDSketch } = require('../../../vendor/dist/@datadog/sketches-js')
const { version } = require('../src/pkg')
const pkg = require('../../../package.json')
const { ORIGIN_KEY, TOP_LEVEL_KEY } = require('../src/constants')
const {
MEASURED,
HTTP_STATUS_CODE,
HTTP_ENDPOINT,
HTTP_ROUTE,
HTTP_METHOD,
} = require('../../../ext/tags')
const {
DEFAULT_SPAN_NAME,
DEFAULT_SERVICE_NAME,
} = require('../src/encode/tags-processors')
const processTags = require('../src/process-tags')
// Mock spans
const basicSpan = {
startTime: 12345 * 1e9,
duration: 1234,
error: 0,
name: 'basic-span',
service: 'service-name',
resource: 'resource-name',
type: 'span-type',
meta: {
[HTTP_STATUS_CODE]: 200,
},
metrics: {},
}
const topLevelSpan = {
...basicSpan,
name: 'top-level-span',
metrics: {
...basicSpan.metrics,
[TOP_LEVEL_KEY]: 1,
},
}
const errorSpan = {
...basicSpan,
name: 'error-span',
error: 1,
meta: {
...basicSpan.meta,
[HTTP_STATUS_CODE]: 500,
},
metrics: {
...basicSpan.metrics,
[MEASURED]: 1,
},
}
const syntheticSpan = {
...basicSpan,
name: 'synthetic-span',
meta: {
...basicSpan.meta,
[ORIGIN_KEY]: 'synthetics',
},
}
const exporter = {
export: sinon.stub(),
}
const SpanStatsExporter = sinon.stub().returns(exporter)
const {
SpanAggStats,
SpanAggKey,
SpanBuckets,
TimeBuckets,
SpanStatsProcessor,
} = proxyquire('../src/span_stats', {
'./exporters/span-stats': {
SpanStatsExporter,
},
})
describe('SpanAggKey', () => {
it('should make aggregation key for a basic span', () => {
const key = new SpanAggKey(basicSpan)
assert.strictEqual(key.toString(), 'basic-span,service-name,resource-name,span-type,200,false,,')
})
it('should make aggregation key for a synthetic span', () => {
const key = new SpanAggKey(syntheticSpan)
assert.strictEqual(key.toString(), 'synthetic-span,service-name,resource-name,span-type,200,true,,')
})
it('should make aggregation key for an error span', () => {
const key = new SpanAggKey(errorSpan)
assert.strictEqual(key.toString(), 'error-span,service-name,resource-name,span-type,500,false,,')
})
it('should use sensible defaults', () => {
const key = new SpanAggKey({ meta: {}, metrics: {} })
assert.strictEqual(key.toString(), `${DEFAULT_SPAN_NAME},${DEFAULT_SERVICE_NAME},,,0,false,,`)
})
it('should include HTTP method and route in aggregation key', () => {
const span = {
...basicSpan,
meta: {
...basicSpan.meta,
[HTTP_METHOD]: 'GET',
[HTTP_ROUTE]: '/users/:id',
},
}
const key = new SpanAggKey(span)
assert.strictEqual(key.toString(), 'basic-span,service-name,resource-name,span-type,200,false,GET,/users/:id')
})
it('should include HTTP method and endpoint in aggregation key', () => {
const span = {
...basicSpan,
meta: {
...basicSpan.meta,
[HTTP_METHOD]: 'POST',
[HTTP_ENDPOINT]: '/users/{param:int}',
},
}
const key = new SpanAggKey(span)
assert.strictEqual(
key.toString(), 'basic-span,service-name,resource-name,span-type,200,false,POST,/users/{param:int}')
})
it('should prioritize http.route over http.endpoint', () => {
const span = {
...basicSpan,
meta: {
...basicSpan.meta,
[HTTP_METHOD]: 'GET',
[HTTP_ROUTE]: '/users/:id',
[HTTP_ENDPOINT]: '/users/{param:int}',
},
}
const key = new SpanAggKey(span)
assert.strictEqual(key.toString(), 'basic-span,service-name,resource-name,span-type,200,false,GET,/users/:id')
})
})
describe('SpanAggStats', () => {
it('should record a basic span', () => {
const aggKey = new SpanAggKey(basicSpan)
const aggStats = new SpanAggStats(aggKey)
aggStats.record(basicSpan)
const okDistribution = new LogCollapsingLowestDenseDDSketch(0.00775)
const errorDistribution = new LogCollapsingLowestDenseDDSketch(0.00775)
okDistribution.accept(basicSpan.duration)
assert.deepStrictEqual(aggStats.toJSON(), {
Name: aggKey.name,
Type: aggKey.type,
Resource: aggKey.resource,
Service: aggKey.service,
HTTPStatusCode: aggKey.statusCode,
Synthetics: aggKey.synthetics,
HTTPMethod: aggKey.method,
HTTPEndpoint: aggKey.endpoint,
Hits: 1,
TopLevelHits: 0,
Errors: 0,
Duration: basicSpan.duration,
OkSummary: okDistribution.toProto(),
ErrorSummary: errorDistribution.toProto(),
})
})
it('should record a top-level span', () => {
const aggKey = new SpanAggKey(topLevelSpan)
const aggStats = new SpanAggStats(aggKey)
aggStats.record(topLevelSpan)
const okDistribution = new LogCollapsingLowestDenseDDSketch(0.00775)
const errorDistribution = new LogCollapsingLowestDenseDDSketch(0.00775)
okDistribution.accept(topLevelSpan.duration)
assert.deepStrictEqual(aggStats.toJSON(), {
Name: aggKey.name,
Type: aggKey.type,
Resource: aggKey.resource,
Service: aggKey.service,
HTTPStatusCode: aggKey.statusCode,
Synthetics: aggKey.synthetics,
HTTPMethod: aggKey.method,
HTTPEndpoint: aggKey.endpoint,
Hits: 1,
TopLevelHits: 1,
Errors: 0,
Duration: topLevelSpan.duration,
OkSummary: okDistribution.toProto(),
ErrorSummary: errorDistribution.toProto(),
})
})
it('should record an error span', () => {
const aggKey = new SpanAggKey(errorSpan)
const aggStats = new SpanAggStats(aggKey)
aggStats.record(errorSpan)
const okDistribution = new LogCollapsingLowestDenseDDSketch(0.00775)
const errorDistribution = new LogCollapsingLowestDenseDDSketch(0.00775)
errorDistribution.accept(errorSpan.duration)
assert.deepStrictEqual(aggStats.toJSON(), {
Name: aggKey.name,
Type: aggKey.type,
Resource: aggKey.resource,
Service: aggKey.service,
HTTPStatusCode: aggKey.statusCode,
Synthetics: aggKey.synthetics,
HTTPMethod: aggKey.method,
HTTPEndpoint: aggKey.endpoint,
Hits: 1,
TopLevelHits: 0,
Errors: 1,
Duration: errorSpan.duration,
OkSummary: okDistribution.toProto(),
ErrorSummary: errorDistribution.toProto(),
})
})
})
describe('SpanBuckets', () => {
const buckets = new SpanBuckets()
it('should start empty', () => {
assert.strictEqual(buckets.size, 0)
})
it('should add a new entry when no matching span agg key is found', () => {
const bucket = buckets.forSpan(basicSpan)
assert.ok(bucket instanceof SpanAggStats)
assert.strictEqual(buckets.size, 1)
const [key, value] = Array.from(buckets.entries())[0]
assert.strictEqual(key, (new SpanAggKey(basicSpan)).toString())
assert.ok(value instanceof SpanAggStats)
})
it('should not add a new entry if matching span agg key is found', () => {
buckets.forSpan(basicSpan)
assert.strictEqual(buckets.size, 1)
})
it('should add a new entry when new span does not match existing agg keys', () => {
buckets.forSpan(errorSpan)
assert.strictEqual(buckets.size, 2)
})
})
describe('TimeBuckets', () => {
it('should acquire a span agg bucket for the given time', () => {
const buckets = new TimeBuckets()
assert.strictEqual(buckets.size, 0)
const bucket = buckets.forTime(12345)
assert.strictEqual(buckets.size, 1)
assert.ok(bucket instanceof SpanBuckets)
})
})
describe('SpanStatsProcessor', () => {
let errorDistribution
let okDistribution
let processor
const n = 100
const config = {
stats: {
enabled: true,
interval: 10,
},
hostname: '127.0.0.1',
port: 8126,
url: new URL('http://127.0.0.1:8126'),
env: 'test',
tags: { tag: 'some tag' },
version: '1.0.0',
}
it('should construct', () => {
processor = new SpanStatsProcessor(config)
clearTimeout(processor.timer)
assert.deepStrictEqual(SpanStatsExporter.lastCall.args[0], {
hostname: config.hostname,
port: config.port,
url: config.url,
tags: config.tags,
})
assert.strictEqual(processor.interval, config.stats.interval)
assert.ok(processor.buckets instanceof TimeBuckets)
assert.strictEqual(processor.hostname, hostname())
assert.strictEqual(processor.enabled, config.stats.enabled)
assert.strictEqual(processor.env, config.env)
assert.deepStrictEqual(processor.tags, config.tags)
assert.strictEqual(processor.version, config.version)
})
it('should construct a disabled instance', () => {
const disabledConfig = { ...config, stats: { enabled: false, interval: 10 } }
const processor = new SpanStatsProcessor(disabledConfig)
assert.strictEqual(processor.enabled, false)
assert.strictEqual(processor.timer, undefined)
})
it('should track span stats', () => {
assert.strictEqual(processor.buckets.size, 0)
for (let i = 0; i < n; i++) {
processor.onSpanFinished(topLevelSpan)
}
assert.strictEqual(processor.buckets.size, 1)
const timeBucket = processor.buckets.values().next().value
assert.ok(timeBucket instanceof SpanBuckets)
assert.strictEqual(timeBucket.size, 1)
const spanBucket = timeBucket.forSpan(topLevelSpan)
assert.strictEqual(timeBucket.size, 1)
assert.ok(spanBucket instanceof SpanAggStats)
okDistribution = new LogCollapsingLowestDenseDDSketch(0.00775)
errorDistribution = new LogCollapsingLowestDenseDDSketch(0.00775)
for (let i = 0; i < n; i++) {
okDistribution.accept(topLevelSpan.duration)
}
assert.deepStrictEqual(spanBucket.toJSON(), {
Name: 'top-level-span',
Service: 'service-name',
Resource: 'resource-name',
Type: 'span-type',
HTTPStatusCode: 200,
Synthetics: false,
HTTPMethod: '',
HTTPEndpoint: '',
Hits: n,
TopLevelHits: n,
Errors: 0,
Duration: (topLevelSpan.duration) * n,
OkSummary: okDistribution.toProto(),
ErrorSummary: errorDistribution.toProto(),
})
})
it('should export on interval', () => {
processor.onInterval()
assert.deepStrictEqual(exporter.export.lastCall.args[0], {
Hostname: hostname(),
Env: config.env,
Version: config.version,
Stats: [{
Start: 12340000000000,
Duration: 10000000000,
Stats: [{
Name: 'top-level-span',
Service: 'service-name',
Resource: 'resource-name',
Type: 'span-type',
HTTPStatusCode: 200,
Synthetics: false,
HTTPMethod: '',
HTTPEndpoint: '',
Hits: n,
TopLevelHits: n,
Errors: 0,
Duration: (topLevelSpan.duration) * n,
OkSummary: okDistribution.toProto(),
ErrorSummary: errorDistribution.toProto(),
}],
}],
Lang: 'javascript',
TracerVersion: pkg.version,
RuntimeID: processor.tags['runtime-id'],
Sequence: processor.sequence,
ProcessTags: processTags.serialized,
})
})
it('should export on interval with default version', () => {
const versionlessConfig = { ...config }
delete versionlessConfig.version
const processor = new SpanStatsProcessor(versionlessConfig)
processor.onInterval()
assert.deepStrictEqual(exporter.export.lastCall.args[0], {
Hostname: hostname(),
Env: config.env,
Version: version,
Stats: [],
Lang: 'javascript',
TracerVersion: pkg.version,
RuntimeID: processor.tags['runtime-id'],
Sequence: processor.sequence,
ProcessTags: processTags.serialized,
})
})
})