Skip to content

Commit 43fee33

Browse files
authored
[DI] Do not fail if a custom logger is configured (#6127)
* [DI] Do not fail if a custom logger is configured Do not try to serialize a custom logger if provided when sending the config object to the DI worker thread. Instead, only serialize the config options that are needed. This change doesn't address the issue of the custom logger not being used by the worker thread, only that it shouldn't fail to start it. That will be addressed later.
1 parent b35b839 commit 43fee33

File tree

8 files changed

+104
-24
lines changed

8 files changed

+104
-24
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const assert = require('node:assert')
4+
const { setup } = require('./utils')
5+
6+
describe('Dynamic Instrumentation', function () {
7+
const t = setup()
8+
9+
it('should not abort if a custom logger is used', function (done) {
10+
t.agent.on('debugger-input', ({ payload: [payload] }) => {
11+
assert.strictEqual(payload.message, 'Hello World!')
12+
done()
13+
})
14+
15+
t.agent.addRemoteConfig(t.rcConfig)
16+
t.triggerBreakpoint()
17+
})
18+
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
require('dd-trace').init({
4+
logger: {
5+
error: err => console.error(err), // eslint-disable-line no-console
6+
warn: message => console.warn(message), // eslint-disable-line no-console
7+
info: message => console.info(message), // eslint-disable-line no-console
8+
debug: message => console.debug(message) // eslint-disable-line no-console
9+
}
10+
})
11+
12+
const http = require('http')
13+
14+
const server = http.createServer((req, res) => {
15+
res.end('hello world') // BREAKPOINT: /
16+
setImmediate(() => {
17+
server.close()
18+
})
19+
})
20+
21+
server.listen(process.env.APP_PORT || 0, () => {
22+
process.send?.({ port: server.address().port })
23+
})

packages/dd-trace/src/ci-visibility/dynamic-instrumentation/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { Worker, threadId: parentThreadId } = require('worker_threads')
55
const { randomUUID } = require('crypto')
66
const log = require('../../log')
77
const { getEnvironmentVariables } = require('../../config-helper')
8+
const getDebuggerConfig = require('../../debugger/config')
89

910
const probeIdToResolveBreakpointSet = new Map()
1011
const probeIdToResolveBreakpointRemove = new Map()
@@ -82,7 +83,7 @@ class TestVisDynamicInstrumentation {
8283
DD_INSTRUMENTATION_TELEMETRY_ENABLED: 'false'
8384
},
8485
workerData: {
85-
config: this._config.serialize(),
86+
config: getDebuggerConfig(this._config),
8687
parentThreadId,
8788
probePort: probeChannel.port1,
8889
configPort: configChannel.port1,

packages/dd-trace/src/config.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,22 +1546,6 @@ class Config {
15461546
}
15471547
}
15481548
}
1549-
1550-
// TODO: Refactor the Config class so it never produces any config objects that are incompatible with MessageChannel
1551-
/**
1552-
* Serializes the config object so it can be passed over a Worker Thread MessageChannel.
1553-
* @returns {Object} The serialized config object.
1554-
*/
1555-
serialize () {
1556-
// URL objects cannot be serialized over the MessageChannel, so we need to convert them to strings first
1557-
if (this.url instanceof URL) {
1558-
const config = { ...this }
1559-
config.url = this.url.toString()
1560-
return config
1561-
}
1562-
1563-
return this
1564-
}
15651549
}
15661550

15671551
function handleOtel (tagString) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
module.exports = function getDebuggerConfig (config) {
4+
return {
5+
commitSHA: config.commitSHA,
6+
dynamicInstrumentation: config.dynamicInstrumentation,
7+
hostname: config.hostname,
8+
port: config.port,
9+
repositoryUrl: config.repositoryUrl,
10+
runtimeId: config.tags['runtime-id'],
11+
service: config.service,
12+
url: config.url?.toString(),
13+
}
14+
}

packages/dd-trace/src/debugger/devtools_client/config.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ const { format } = require('node:url')
55
const log = require('../../log')
66

77
const config = module.exports = {
8-
dynamicInstrumentation: parentConfig.dynamicInstrumentation,
9-
runtimeId: parentConfig.tags['runtime-id'],
10-
service: parentConfig.service,
11-
commitSHA: parentConfig.commitSHA,
12-
repositoryUrl: parentConfig.repositoryUrl,
8+
...parentConfig,
139
parentThreadId,
1410
maxTotalPayloadSize: 5 * 1024 * 1024 // 5MB
1511
}

packages/dd-trace/src/debugger/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { readFile } = require('fs')
44
const { types } = require('util')
55
const { join } = require('path')
66
const { Worker, MessageChannel, threadId: parentThreadId } = require('worker_threads')
7+
const getDebuggerConfig = require('./config')
78
const log = require('../log')
89

910
let worker = null
@@ -60,7 +61,7 @@ function start (config, rc) {
6061
execArgv: [], // Avoid worker thread inheriting the `-r` command line argument
6162
env, // Avoid worker thread inheriting the `NODE_OPTIONS` environment variable (in case it contains `-r`)
6263
workerData: {
63-
config: config.serialize(),
64+
config: getDebuggerConfig(config),
6465
parentThreadId,
6566
probePort: probeChannel.port1,
6667
configPort: configChannel.port1
@@ -100,7 +101,7 @@ function start (config, rc) {
100101

101102
function configure (config) {
102103
if (configChannel === null) return
103-
configChannel.port2.postMessage(config.serialize())
104+
configChannel.port2.postMessage(getDebuggerConfig(config))
104105
}
105106

106107
function readProbeFile (path, cb) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
require('../setup/mocha')
4+
5+
const assert = require('node:assert')
6+
const getDebuggerConfig = require('../../src/debugger/config')
7+
const Config = require('../../src/config')
8+
9+
describe('getDebuggerConfig', function () {
10+
it('should only contain the allowed properties', function () {
11+
const tracerConfig = new Config({
12+
url: new URL('http://example.com:1234')
13+
})
14+
const config = getDebuggerConfig(tracerConfig)
15+
assert.deepStrictEqual(Object.keys(config), [
16+
'commitSHA',
17+
'dynamicInstrumentation',
18+
'hostname',
19+
'port',
20+
'repositoryUrl',
21+
'runtimeId',
22+
'service',
23+
'url',
24+
])
25+
assert.strictEqual(config.commitSHA, tracerConfig.commitSHA)
26+
assert.deepStrictEqual(config.dynamicInstrumentation, tracerConfig.dynamicInstrumentation)
27+
assert.strictEqual(config.hostname, tracerConfig.hostname)
28+
assert.strictEqual(config.port, tracerConfig.port)
29+
assert.strictEqual(config.repositoryUrl, tracerConfig.repositoryUrl)
30+
assert.strictEqual(config.runtimeId, tracerConfig.tags['runtime-id'])
31+
assert.strictEqual(config.service, tracerConfig.service)
32+
assert.strictEqual(config.url, tracerConfig.url.toString())
33+
})
34+
35+
it('should be able to send the config over a MessageChannel', function () {
36+
const config = getDebuggerConfig(new Config())
37+
const channel = new MessageChannel()
38+
channel.port1.on('message', (message) => {
39+
assert.deepStrictEqual(message, config)
40+
})
41+
channel.port2.postMessage(config)
42+
})
43+
})

0 commit comments

Comments
 (0)