Skip to content

[DI] Associate probe results with active span #5035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion integration-tests/debugger/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,18 @@ describe('Dynamic Instrumentation', function () {

describe('input messages', function () {
it('should capture and send expected payload when a log line probe is triggered', function (done) {
let traceId, spanId, dd

t.triggerBreakpoint()

t.agent.on('message', ({ payload }) => {
const span = payload.find((arr) => arr[0].name === 'fastify.request')[0]
traceId = span.trace_id.toString()
spanId = span.span_id.toString()

assertDD()
})

t.agent.on('debugger-input', ({ payload }) => {
const expected = {
ddsource: 'dd_debugger',
Expand All @@ -260,7 +270,17 @@ describe('Dynamic Instrumentation', function () {
}

assertObjectContains(payload, expected)

assert.match(payload.logger.thread_id, /^pid:\d+$/)

assert.isObject(payload.dd)
assert.hasAllKeys(payload.dd, ['trace_id', 'span_id'])
assert.typeOf(payload.dd.trace_id, 'string')
assert.typeOf(payload.dd.span_id, 'string')
assert.isAbove(payload.dd.trace_id.length, 0)
assert.isAbove(payload.dd.span_id.length, 0)
dd = payload.dd

assertUUID(payload['debugger.snapshot'].id)
assert.isNumber(payload['debugger.snapshot'].timestamp)
assert.isTrue(payload['debugger.snapshot'].timestamp > Date.now() - 1000 * 60)
Expand All @@ -283,10 +303,17 @@ describe('Dynamic Instrumentation', function () {
assert.strictEqual(topFrame.lineNumber, t.breakpoint.line)
assert.strictEqual(topFrame.columnNumber, 3)

done()
assertDD()
})

t.agent.addRemoteConfig(t.rcConfig)

function assertDD () {
if (!traceId || !spanId || !dd) return
assert.strictEqual(dd.trace_id, traceId)
assert.strictEqual(dd.span_id, spanId)
done()
}
})

it('should respond with updated message if probe message is updated', function (done) {
Expand Down
27 changes: 26 additions & 1 deletion packages/dd-trace/src/debugger/devtools_client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const { version } = require('../../../../../package.json')

require('./remote_config')

// Expression to run on a call frame of the paused thread to get its active trace and span id.
const expression = `
const context = global.require('dd-trace').scope().active()?.context();
({ trace_id: context?.toTraceId(), span_id: context?.toSpanId() })
`

// There doesn't seem to be an official standard for the content of these fields, so we're just populating them with
// something that should be useful to a Node.js developer.
const threadId = parentThreadId === 0 ? `pid:${process.pid}` : `pid:${process.pid};tid:${parentThreadId}`
Expand Down Expand Up @@ -59,6 +65,7 @@ session.on('Debugger.paused', async ({ params }) => {
}

const timestamp = Date.now()
const dd = await getDD(params.callFrames[0].callFrameId)

let processLocalState
if (captureSnapshotForProbe !== null) {
Expand Down Expand Up @@ -122,7 +129,7 @@ session.on('Debugger.paused', async ({ params }) => {
}

// TODO: Process template (DEBUG-2628)
send(probe.template, logger, snapshot, (err) => {
send(probe.template, logger, dd, snapshot, (err) => {
if (err) log.error('Debugger error', err)
else ackEmitting(probe)
})
Expand All @@ -132,3 +139,21 @@ session.on('Debugger.paused', async ({ params }) => {
function highestOrUndefined (num, max) {
return num === undefined ? max : Math.max(num, max ?? 0)
}

async function getDD (callFrameId) {
const { result } = await session.post('Debugger.evaluateOnCallFrame', {
callFrameId,
expression,
returnByValue: true,
includeCommandLineAPI: true
})

if (result?.value?.trace_id === undefined) {
if (result?.subtype === 'error') {
log.error('[debugger:devtools_client] Error getting trace/span id:', result.description)
}
return
}

return result.value
}
3 changes: 2 additions & 1 deletion packages/dd-trace/src/debugger/devtools_client/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ddtags = [

const path = `/debugger/v1/input?${stringify({ ddtags })}`

function send (message, logger, snapshot, cb) {
function send (message, logger, dd, snapshot, cb) {
const opts = {
method: 'POST',
url: config.url,
Expand All @@ -36,6 +36,7 @@ function send (message, logger, snapshot, cb) {
service,
message,
logger,
dd,
'debugger.snapshot': snapshot
}

Expand Down
Loading