dd-trace 4.0.0-pre-9c54395 → 4.0.0-pre-19ea4d3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/ci/init.js +3 -1
- package/package.json +1 -1
- package/packages/datadog-instrumentations/src/helpers/hooks.js +1 -0
- package/packages/datadog-instrumentations/src/jest.js +43 -0
- package/packages/datadog-instrumentations/src/playwright.js +40 -11
- package/packages/datadog-plugin-jest/src/index.js +2 -0
- package/packages/dd-trace/src/config.js +1 -1
- package/packages/dd-trace/src/opentracing/span_context.js +1 -1
- package/packages/dd-trace/src/plugins/index.js +1 -0
- package/packages/dd-trace/src/profiler.js +3 -0
- package/packages/dd-trace/src/profiling/config.js +8 -3
- package/packages/dd-trace/src/profiling/exporters/file.js +13 -2
- package/packages/dd-trace/src/profiling/profiler.js +23 -6
- package/packages/dd-trace/src/profiling/profilers/wall.js +1 -0
- package/packages/dd-trace/src/span_processor.js +1 -1
- package/packages/dd-trace/src/span_sampler.js +68 -52
package/README.md
CHANGED
|
@@ -24,11 +24,11 @@ Most of the documentation for `dd-trace` is available on these webpages:
|
|
|
24
24
|
|
|
25
25
|
## Version Release Lines and Maintenance
|
|
26
26
|
|
|
27
|
-
| Release Line | Latest Version | Status |Initial Release | End of Life |
|
|
28
|
-
|
|
|
29
|
-
| [`v1`](https://github.com/DataDog/dd-trace-js/tree/v1.x) |  | **End of Life** | 2021-07-13 | 2022-02-25 |
|
|
30
|
-
| [`v2`](https://github.com/DataDog/dd-trace-js/tree/v2.x) |  | **Maintenance** | 2022-01-28 | 2023-08-15 |
|
|
31
|
-
| [`v3`](https://github.com/DataDog/dd-trace-js/tree/v3.x) |  | **Current** | 2022-08-15 | Unknown |
|
|
27
|
+
| Release Line | Latest Version | Node.js | Status |Initial Release | End of Life |
|
|
28
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
29
|
+
| [`v1`](https://github.com/DataDog/dd-trace-js/tree/v1.x) |  | `>= v12` | **End of Life** | 2021-07-13 | 2022-02-25 |
|
|
30
|
+
| [`v2`](https://github.com/DataDog/dd-trace-js/tree/v2.x) |  | `>= v12` | **Maintenance** | 2022-01-28 | 2023-08-15 |
|
|
31
|
+
| [`v3`](https://github.com/DataDog/dd-trace-js/tree/v3.x) |  | `>= v14` | **Current** | 2022-08-15 | Unknown |
|
|
32
32
|
|
|
33
33
|
We currently maintain two release lines, namely `v2` and `v3`.
|
|
34
34
|
Features and bug fixes that are merged are released to the `v3` line and, if appropriate, also the `v2` line.
|
package/ci/init.js
CHANGED
|
@@ -3,13 +3,15 @@ const tracer = require('../packages/dd-trace')
|
|
|
3
3
|
const { ORIGIN_KEY } = require('../packages/dd-trace/src/constants')
|
|
4
4
|
const { isTrue } = require('../packages/dd-trace/src/util')
|
|
5
5
|
|
|
6
|
+
const isJestWorker = () => !!process.env.JEST_WORKER_ID
|
|
7
|
+
|
|
6
8
|
const options = {
|
|
7
9
|
startupLogs: false,
|
|
8
10
|
tags: {
|
|
9
11
|
[ORIGIN_KEY]: 'ciapp-test'
|
|
10
12
|
},
|
|
11
13
|
isCiVisibility: true,
|
|
12
|
-
flushInterval: 5000
|
|
14
|
+
flushInterval: isJestWorker() ? 120000 : 5000
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
let shouldInit = true
|
package/package.json
CHANGED
|
@@ -47,6 +47,7 @@ module.exports = {
|
|
|
47
47
|
'jest-environment-node': () => require('../jest'),
|
|
48
48
|
'jest-environment-jsdom': () => require('../jest'),
|
|
49
49
|
'jest-jasmine2': () => require('../jest'),
|
|
50
|
+
'jest-worker': () => require('../jest'),
|
|
50
51
|
'koa': () => require('../koa'),
|
|
51
52
|
'koa-router': () => require('../koa'),
|
|
52
53
|
'kafkajs': () => require('../kafkajs'),
|
|
@@ -11,6 +11,7 @@ const testSessionConfigurationCh = channel('ci:jest:session:configuration')
|
|
|
11
11
|
|
|
12
12
|
const testSuiteStartCh = channel('ci:jest:test-suite:start')
|
|
13
13
|
const testSuiteFinishCh = channel('ci:jest:test-suite:finish')
|
|
14
|
+
|
|
14
15
|
const testSuiteCodeCoverageCh = channel('ci:jest:test-suite:code-coverage')
|
|
15
16
|
|
|
16
17
|
const testStartCh = channel('ci:jest:test:start')
|
|
@@ -475,3 +476,45 @@ addHook({
|
|
|
475
476
|
versions: ['>=24.8.0'],
|
|
476
477
|
file: 'build/jasmineAsyncInstall.js'
|
|
477
478
|
}, jasmineAsyncInstallWraper)
|
|
479
|
+
|
|
480
|
+
// Maximum time we allow the workers to shutdown
|
|
481
|
+
const JEST_WORKER_SHUTDOWN_TIMEOUT = 20
|
|
482
|
+
// https://github.com/facebook/jest/blob/d6ad15b0f88a05816c2fe034dd6900d28315d570/packages/jest-worker/src/types.ts#L38
|
|
483
|
+
const CHILD_MESSAGE_END = 2
|
|
484
|
+
|
|
485
|
+
// 25.1.0 is where waitForExit
|
|
486
|
+
addHook({
|
|
487
|
+
name: 'jest-worker',
|
|
488
|
+
versions: ['>=24.9.0'],
|
|
489
|
+
file: 'build/base/BaseWorkerPool.js'
|
|
490
|
+
}, (baseWorkerPool) => {
|
|
491
|
+
const BaseWorkerPool = baseWorkerPool.default ? baseWorkerPool.default : baseWorkerPool
|
|
492
|
+
shimmer.wrap(BaseWorkerPool.prototype, 'end', end => async function () {
|
|
493
|
+
let timeoutId
|
|
494
|
+
|
|
495
|
+
try {
|
|
496
|
+
// End everything: we listen to this message and attempt to flush everything
|
|
497
|
+
this._workers.forEach(worker => {
|
|
498
|
+
worker.send([CHILD_MESSAGE_END], () => {}, () => {}, () => {})
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
const killPromise = new Promise((resolve) => {
|
|
502
|
+
timeoutId = setTimeout(() => {
|
|
503
|
+
resolve()
|
|
504
|
+
}, JEST_WORKER_SHUTDOWN_TIMEOUT * 1000)
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
const workersWaitForExitPromise = Promise.all(this._workers.map(worker =>
|
|
508
|
+
worker.waitForExit()
|
|
509
|
+
))
|
|
510
|
+
|
|
511
|
+
// If the workers are able to shut down gracefully before the timeout, we proceed
|
|
512
|
+
await Promise.race([workersWaitForExitPromise, killPromise])
|
|
513
|
+
clearTimeout(timeoutId)
|
|
514
|
+
} catch (e) {
|
|
515
|
+
// ignore error
|
|
516
|
+
}
|
|
517
|
+
return end.apply(this, arguments)
|
|
518
|
+
})
|
|
519
|
+
return baseWorkerPool
|
|
520
|
+
})
|
|
@@ -25,6 +25,17 @@ const STATUS_TO_TEST_STATUS = {
|
|
|
25
25
|
|
|
26
26
|
let remainingTestsByFile = {}
|
|
27
27
|
|
|
28
|
+
function getTestsBySuiteFromTestGroups (testGroups) {
|
|
29
|
+
return testGroups.reduce((acc, { requireFile, tests }) => {
|
|
30
|
+
if (acc[requireFile]) {
|
|
31
|
+
acc[requireFile] = acc[requireFile].concat(tests)
|
|
32
|
+
} else {
|
|
33
|
+
acc[requireFile] = tests
|
|
34
|
+
}
|
|
35
|
+
return acc
|
|
36
|
+
}, {})
|
|
37
|
+
}
|
|
38
|
+
|
|
28
39
|
function getTestsBySuiteFromTestsById (testsById) {
|
|
29
40
|
const testsByTestSuite = {}
|
|
30
41
|
for (const { test } of testsById.values()) {
|
|
@@ -48,7 +59,7 @@ function getPlaywrightConfig (playwrightRunner) {
|
|
|
48
59
|
try {
|
|
49
60
|
return playwrightRunner._loader.fullConfig()
|
|
50
61
|
} catch (e) {
|
|
51
|
-
return {}
|
|
62
|
+
return playwrightRunner._config || {}
|
|
52
63
|
}
|
|
53
64
|
}
|
|
54
65
|
}
|
|
@@ -135,6 +146,13 @@ function dispatcherRunWrapper (run) {
|
|
|
135
146
|
}
|
|
136
147
|
}
|
|
137
148
|
|
|
149
|
+
function dispatcherRunWrapperNew (run) {
|
|
150
|
+
return function () {
|
|
151
|
+
remainingTestsByFile = getTestsBySuiteFromTestGroups(arguments[0])
|
|
152
|
+
return run.apply(this, arguments)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
138
156
|
function dispatcherHook (dispatcherExport) {
|
|
139
157
|
shimmer.wrap(dispatcherExport.Dispatcher.prototype, 'run', dispatcherRunWrapper)
|
|
140
158
|
shimmer.wrap(dispatcherExport.Dispatcher.prototype, '_createWorker', createWorker => function () {
|
|
@@ -160,8 +178,8 @@ function dispatcherHook (dispatcherExport) {
|
|
|
160
178
|
return dispatcherExport
|
|
161
179
|
}
|
|
162
180
|
|
|
163
|
-
function dispatcherHookNew (dispatcherExport) {
|
|
164
|
-
shimmer.wrap(dispatcherExport.Dispatcher.prototype, 'run',
|
|
181
|
+
function dispatcherHookNew (dispatcherExport, runWrapper) {
|
|
182
|
+
shimmer.wrap(dispatcherExport.Dispatcher.prototype, 'run', runWrapper)
|
|
165
183
|
shimmer.wrap(dispatcherExport.Dispatcher.prototype, '_createWorker', createWorker => function () {
|
|
166
184
|
const dispatcher = this
|
|
167
185
|
const worker = createWorker.apply(this, arguments)
|
|
@@ -192,24 +210,23 @@ function runnerHook (runnerExport, playwrightVersion) {
|
|
|
192
210
|
testSessionStartCh.publish({ command, frameworkVersion: playwrightVersion, rootDir })
|
|
193
211
|
})
|
|
194
212
|
|
|
195
|
-
const
|
|
196
|
-
const sessionStatus =
|
|
213
|
+
const runAllTestsReturn = await runAllTests.apply(this, arguments)
|
|
214
|
+
const sessionStatus = runAllTestsReturn.status || runAllTestsReturn
|
|
197
215
|
|
|
198
216
|
let onDone
|
|
199
217
|
|
|
200
218
|
const flushWait = new Promise(resolve => {
|
|
201
219
|
onDone = resolve
|
|
202
220
|
})
|
|
203
|
-
|
|
204
221
|
testSessionAsyncResource.runInAsyncScope(() => {
|
|
205
|
-
testSessionFinishCh.publish({ status: sessionStatus, onDone })
|
|
222
|
+
testSessionFinishCh.publish({ status: STATUS_TO_TEST_STATUS[sessionStatus], onDone })
|
|
206
223
|
})
|
|
207
224
|
await flushWait
|
|
208
225
|
|
|
209
226
|
startedSuites = []
|
|
210
227
|
remainingTestsByFile = {}
|
|
211
228
|
|
|
212
|
-
return
|
|
229
|
+
return runAllTestsReturn
|
|
213
230
|
})
|
|
214
231
|
|
|
215
232
|
return runnerExport
|
|
@@ -218,7 +235,7 @@ function runnerHook (runnerExport, playwrightVersion) {
|
|
|
218
235
|
addHook({
|
|
219
236
|
name: '@playwright/test',
|
|
220
237
|
file: 'lib/runner.js',
|
|
221
|
-
versions: ['>=1.18.0']
|
|
238
|
+
versions: ['>=1.18.0 <1.30.0']
|
|
222
239
|
}, runnerHook)
|
|
223
240
|
|
|
224
241
|
addHook({
|
|
@@ -230,5 +247,17 @@ addHook({
|
|
|
230
247
|
addHook({
|
|
231
248
|
name: '@playwright/test',
|
|
232
249
|
file: 'lib/dispatcher.js',
|
|
233
|
-
versions: ['>=1.30.0']
|
|
234
|
-
}, dispatcherHookNew)
|
|
250
|
+
versions: ['>=1.30.0 <1.31.0']
|
|
251
|
+
}, (dispatcher) => dispatcherHookNew(dispatcher, dispatcherRunWrapper))
|
|
252
|
+
|
|
253
|
+
addHook({
|
|
254
|
+
name: '@playwright/test',
|
|
255
|
+
file: 'lib/runner/dispatcher.js',
|
|
256
|
+
versions: ['>=1.31.0']
|
|
257
|
+
}, (dispatcher) => dispatcherHookNew(dispatcher, dispatcherRunWrapperNew))
|
|
258
|
+
|
|
259
|
+
addHook({
|
|
260
|
+
name: '@playwright/test',
|
|
261
|
+
file: 'lib/runner/runner.js',
|
|
262
|
+
versions: ['>=1.31.0']
|
|
263
|
+
}, runnerHook)
|
|
@@ -29,6 +29,8 @@ class JestPlugin extends CiPlugin {
|
|
|
29
29
|
// Used to handle the end of a jest worker to be able to flush
|
|
30
30
|
const handler = ([message]) => {
|
|
31
31
|
if (message === CHILD_MESSAGE_END) {
|
|
32
|
+
this.testSuiteSpan.finish()
|
|
33
|
+
finishAllTraceSpans(this.testSuiteSpan)
|
|
32
34
|
this.tracer._exporter.flush(() => {
|
|
33
35
|
// eslint-disable-next-line
|
|
34
36
|
// https://github.com/facebook/jest/blob/24ed3b5ecb419c023ee6fdbc838f07cc028fc007/packages/jest-worker/src/workers/processChild.ts#L118-L133
|
|
@@ -376,7 +376,7 @@ ken|consumer_?(?:id|key|secret)|sign(?:ed|ature)?|auth(?:entication|orization)?)
|
|
|
376
376
|
|
|
377
377
|
const DD_CIVISIBILITY_GIT_UPLOAD_ENABLED = coalesce(
|
|
378
378
|
process.env.DD_CIVISIBILITY_GIT_UPLOAD_ENABLED,
|
|
379
|
-
|
|
379
|
+
true
|
|
380
380
|
)
|
|
381
381
|
|
|
382
382
|
const ingestion = options.ingestion || {}
|
|
@@ -12,7 +12,7 @@ class DatadogSpanContext {
|
|
|
12
12
|
this._name = props.name
|
|
13
13
|
this._isFinished = props.isFinished || false
|
|
14
14
|
this._tags = props.tags || {}
|
|
15
|
-
this._sampling = props.sampling
|
|
15
|
+
this._sampling = Object.assign({}, props.sampling)
|
|
16
16
|
this._baggageItems = props.baggageItems || {}
|
|
17
17
|
this._traceparent = props.traceparent
|
|
18
18
|
this._tracestate = props.tracestate
|
|
@@ -39,6 +39,7 @@ module.exports = {
|
|
|
39
39
|
get 'jest-environment-node' () { return require('../../../datadog-plugin-jest/src') },
|
|
40
40
|
get 'jest-environment-jsdom' () { return require('../../../datadog-plugin-jest/src') },
|
|
41
41
|
get 'jest-jasmine2' () { return require('../../../datadog-plugin-jest/src') },
|
|
42
|
+
get 'jest-worker' () { return require('../../../datadog-plugin-jest/src') },
|
|
42
43
|
get 'koa' () { return require('../../../datadog-plugin-koa/src') },
|
|
43
44
|
get 'koa-router' () { return require('../../../datadog-plugin-koa/src') },
|
|
44
45
|
get 'kafkajs' () { return require('../../../datadog-plugin-kafkajs/src') },
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
const log = require('./log')
|
|
4
4
|
const { profiler } = require('./profiling')
|
|
5
5
|
|
|
6
|
+
// Stop profiler upon exit in order to collect and export the current profile
|
|
7
|
+
process.once('beforeExit', () => { profiler.stop() })
|
|
8
|
+
|
|
6
9
|
module.exports = {
|
|
7
10
|
start: config => {
|
|
8
11
|
const { service, version, env, url, hostname, port, tags } = config
|
|
@@ -23,7 +23,9 @@ const {
|
|
|
23
23
|
DD_AGENT_HOST,
|
|
24
24
|
DD_TRACE_AGENT_PORT,
|
|
25
25
|
DD_PROFILING_UPLOAD_TIMEOUT,
|
|
26
|
-
DD_PROFILING_SOURCE_MAP
|
|
26
|
+
DD_PROFILING_SOURCE_MAP,
|
|
27
|
+
DD_PROFILING_UPLOAD_PERIOD,
|
|
28
|
+
DD_PROFILING_PPROF_PREFIX
|
|
27
29
|
} = process.env
|
|
28
30
|
|
|
29
31
|
class Config {
|
|
@@ -35,13 +37,15 @@ class Config {
|
|
|
35
37
|
const version = coalesce(options.version, DD_VERSION)
|
|
36
38
|
const functionname = process.env.AWS_LAMBDA_FUNCTION_NAME
|
|
37
39
|
// Must be longer than one minute so pad with five seconds
|
|
38
|
-
const flushInterval = coalesce(options.interval, 65 * 1000)
|
|
40
|
+
const flushInterval = coalesce(options.interval, Number(DD_PROFILING_UPLOAD_PERIOD) * 1000, 65 * 1000)
|
|
39
41
|
const uploadTimeout = coalesce(options.uploadTimeout,
|
|
40
|
-
DD_PROFILING_UPLOAD_TIMEOUT, 60 * 1000)
|
|
42
|
+
Number(DD_PROFILING_UPLOAD_TIMEOUT), 60 * 1000)
|
|
41
43
|
const sourceMap = coalesce(options.sourceMap,
|
|
42
44
|
DD_PROFILING_SOURCE_MAP, true)
|
|
43
45
|
const endpointCollection = coalesce(options.endpointCollection,
|
|
44
46
|
DD_PROFILING_ENDPOINT_COLLECTION_ENABLED, false)
|
|
47
|
+
const pprofPrefix = coalesce(options.pprofPrefix,
|
|
48
|
+
DD_PROFILING_PPROF_PREFIX)
|
|
45
49
|
|
|
46
50
|
this.enabled = String(enabled) !== 'false'
|
|
47
51
|
this.service = service
|
|
@@ -60,6 +64,7 @@ class Config {
|
|
|
60
64
|
this.uploadTimeout = uploadTimeout
|
|
61
65
|
this.sourceMap = sourceMap
|
|
62
66
|
this.endpointCollection = endpointCollection
|
|
67
|
+
this.pprofPrefix = pprofPrefix
|
|
63
68
|
|
|
64
69
|
const hostname = coalesce(options.hostname, DD_AGENT_HOST) || 'localhost'
|
|
65
70
|
const port = coalesce(options.port, DD_TRACE_AGENT_PORT) || 8126
|
|
@@ -4,11 +4,22 @@ const fs = require('fs')
|
|
|
4
4
|
const { promisify } = require('util')
|
|
5
5
|
const writeFile = promisify(fs.writeFile)
|
|
6
6
|
|
|
7
|
+
function formatDateTime (t) {
|
|
8
|
+
const pad = (n) => String(n).padStart(2, '0')
|
|
9
|
+
return `${t.getUTCFullYear()}${pad(t.getUTCMonth() + 1)}${pad(t.getUTCDate())}` +
|
|
10
|
+
`T${pad(t.getUTCHours())}${pad(t.getUTCMinutes())}${pad(t.getUTCSeconds())}Z`
|
|
11
|
+
}
|
|
12
|
+
|
|
7
13
|
class FileExporter {
|
|
8
|
-
|
|
14
|
+
constructor ({ pprofPrefix } = {}) {
|
|
15
|
+
this._pprofPrefix = pprofPrefix || ''
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export ({ profiles, end }) {
|
|
9
19
|
const types = Object.keys(profiles)
|
|
20
|
+
const dateStr = formatDateTime(end)
|
|
10
21
|
const tasks = types.map(type => {
|
|
11
|
-
return writeFile(`${type}.
|
|
22
|
+
return writeFile(`${this._pprofPrefix}${type}_${dateStr}.pprof`, profiles[type])
|
|
12
23
|
})
|
|
13
24
|
|
|
14
25
|
return Promise.all(tasks)
|
|
@@ -56,7 +56,7 @@ class Profiler extends EventEmitter {
|
|
|
56
56
|
this._capture(this._timeoutInterval)
|
|
57
57
|
} catch (e) {
|
|
58
58
|
this._logger.error(e)
|
|
59
|
-
this.
|
|
59
|
+
this._stop()
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
|
|
@@ -64,7 +64,16 @@ class Profiler extends EventEmitter {
|
|
|
64
64
|
this._timeoutInterval = this._config.flushInterval
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
stop () {
|
|
67
|
+
async stop () {
|
|
68
|
+
if (!this._enabled) return
|
|
69
|
+
|
|
70
|
+
// collect and export current profiles
|
|
71
|
+
// once collect returns, profilers can be safely stopped
|
|
72
|
+
this._collect()
|
|
73
|
+
this._stop()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
_stop () {
|
|
68
77
|
if (!this._enabled) return
|
|
69
78
|
|
|
70
79
|
this._enabled = false
|
|
@@ -92,16 +101,24 @@ class Profiler extends EventEmitter {
|
|
|
92
101
|
}
|
|
93
102
|
|
|
94
103
|
async _collect () {
|
|
104
|
+
if (!this._enabled) return
|
|
105
|
+
|
|
95
106
|
const start = this._lastStart
|
|
96
107
|
const end = new Date()
|
|
97
|
-
const profiles =
|
|
108
|
+
const profiles = []
|
|
109
|
+
const encodedProfiles = {}
|
|
98
110
|
|
|
99
111
|
try {
|
|
112
|
+
// collect profiles synchronously so that profilers can be safely stopped asynchronously
|
|
100
113
|
for (const profiler of this._config.profilers) {
|
|
101
114
|
const profile = profiler.profile()
|
|
102
115
|
if (!profile) continue
|
|
116
|
+
profiles.push({ profiler, profile })
|
|
117
|
+
}
|
|
103
118
|
|
|
104
|
-
|
|
119
|
+
// encode and export asynchronously
|
|
120
|
+
for (const { profiler, profile } of profiles) {
|
|
121
|
+
encodedProfiles[profiler.type] = await profiler.encode(profile)
|
|
105
122
|
this._logger.debug(() => {
|
|
106
123
|
const profileJson = JSON.stringify(profile, (key, value) => {
|
|
107
124
|
return typeof value === 'bigint' ? value.toString() : value
|
|
@@ -111,11 +128,11 @@ class Profiler extends EventEmitter {
|
|
|
111
128
|
}
|
|
112
129
|
|
|
113
130
|
this._capture(this._timeoutInterval)
|
|
114
|
-
await this._submit(
|
|
131
|
+
await this._submit(encodedProfiles, start, end)
|
|
115
132
|
this._logger.debug('Submitted profiles')
|
|
116
133
|
} catch (err) {
|
|
117
134
|
this._logger.error(err)
|
|
118
|
-
this.
|
|
135
|
+
this._stop()
|
|
119
136
|
}
|
|
120
137
|
}
|
|
121
138
|
|
|
@@ -2,76 +2,92 @@
|
|
|
2
2
|
const { globMatch } = require('../src/util')
|
|
3
3
|
const { USER_KEEP, AUTO_KEEP } = require('../../../ext').priority
|
|
4
4
|
const RateLimiter = require('./rate_limiter')
|
|
5
|
+
const Sampler = require('./sampler')
|
|
5
6
|
|
|
6
|
-
class
|
|
7
|
-
constructor ({
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
7
|
+
class SpanSamplingRule {
|
|
8
|
+
constructor ({ service, name, sampleRate = 1.0, maxPerSecond } = {}) {
|
|
9
|
+
this.service = service
|
|
10
|
+
this.name = name
|
|
11
|
+
|
|
12
|
+
this._sampler = new Sampler(sampleRate)
|
|
13
|
+
this._limiter = undefined
|
|
14
|
+
|
|
15
|
+
if (Number.isFinite(maxPerSecond)) {
|
|
16
|
+
this._limiter = new RateLimiter(maxPerSecond)
|
|
17
|
+
}
|
|
10
18
|
}
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
get sampleRate () {
|
|
21
|
+
return this._sampler.rate()
|
|
22
|
+
}
|
|
15
23
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const name = span._name
|
|
20
|
-
const rule = findRule(this._rules, service, name)
|
|
21
|
-
if (!rule) continue
|
|
24
|
+
get maxPerSecond () {
|
|
25
|
+
return this._limiter && this._limiter._rateLimit
|
|
26
|
+
}
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (!sampled) continue
|
|
28
|
+
static from (config) {
|
|
29
|
+
return new SpanSamplingRule(config)
|
|
30
|
+
}
|
|
27
31
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
span.context()._sampling.spanSampling = {
|
|
32
|
-
sampleRate,
|
|
33
|
-
maxPerSecond
|
|
34
|
-
}
|
|
35
|
-
}
|
|
32
|
+
match (service, name) {
|
|
33
|
+
if (this.service && !globMatch(this.service, service)) {
|
|
34
|
+
return false
|
|
36
35
|
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const servicePattern = getService(rule.service)
|
|
43
|
-
const namePattern = getName(rule.name)
|
|
44
|
-
if (globMatch(servicePattern, service) && globMatch(namePattern, name)) {
|
|
45
|
-
return rule
|
|
37
|
+
if (this.name && !globMatch(this.name, name)) {
|
|
38
|
+
return false
|
|
46
39
|
}
|
|
40
|
+
|
|
41
|
+
return true
|
|
47
42
|
}
|
|
48
|
-
}
|
|
49
43
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
44
|
+
sample () {
|
|
45
|
+
if (!this._sampler.isSampled()) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (this._limiter) {
|
|
50
|
+
return this._limiter.isAllowed()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return true
|
|
53
54
|
}
|
|
54
|
-
return list[key]
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
class SpanSampler {
|
|
58
|
+
constructor ({ spanSamplingRules = [] } = {}) {
|
|
59
|
+
this._rules = spanSamplingRules.map(SpanSamplingRule.from)
|
|
60
|
+
}
|
|
60
61
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
findRule (service, name) {
|
|
63
|
+
for (const rule of this._rules) {
|
|
64
|
+
if (rule.match(service, name)) {
|
|
65
|
+
return rule
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
70
|
+
sample (spanContext) {
|
|
71
|
+
const decision = spanContext._sampling.priority
|
|
72
|
+
if (decision === USER_KEEP || decision === AUTO_KEEP) return
|
|
68
73
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
74
|
+
const { started } = spanContext._trace
|
|
75
|
+
for (const span of started) {
|
|
76
|
+
const tags = span.context()._tags || {}
|
|
77
|
+
const name = span._name
|
|
78
|
+
const service = tags.service ||
|
|
79
|
+
tags['service.name'] ||
|
|
80
|
+
span.tracer()._service
|
|
72
81
|
|
|
73
|
-
|
|
74
|
-
|
|
82
|
+
const rule = this.findRule(service, name)
|
|
83
|
+
if (rule && rule.sample()) {
|
|
84
|
+
span.context()._sampling.spanSampling = {
|
|
85
|
+
sampleRate: rule.sampleRate,
|
|
86
|
+
maxPerSecond: rule.maxPerSecond
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
75
91
|
}
|
|
76
92
|
|
|
77
93
|
module.exports = SpanSampler
|