Skip to content

Various improvements to profiling sample label generator #5316

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 3 commits into from
Feb 26, 2025
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
19 changes: 15 additions & 4 deletions packages/dd-trace/src/profiling/profilers/wall.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,25 @@ class NativeWallProfiler {

const labels = { ...getThreadLabels() }

const { context: { ref }, timestamp } = context
const { spanId, rootSpanId, webTags, endpoint } = ref ?? {}

if (this._timelineEnabled) {
// Incoming timestamps are in microseconds, we emit nanos.
labels[END_TIMESTAMP_LABEL] = timestamp * 1000n
labels[END_TIMESTAMP_LABEL] = context.timestamp * 1000n
}

const asyncId = context.asyncId
if (asyncId !== undefined && asyncId !== -1) {
labels['async id'] = asyncId
}

// Native profiler doesn't set context.context for some samples, such as idle samples or when
// the context was otherwise unavailable when the sample was taken.
const ref = context.context?.ref
if (typeof ref !== 'object') {
return labels
}

const { spanId, rootSpanId, webTags, endpoint } = ref

if (spanId !== undefined) {
labels[SPAN_ID_LABEL] = spanId
}
Expand Down
91 changes: 90 additions & 1 deletion packages/dd-trace/test/profiling/profilers/wall.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ describe('profilers/native/wall', () => {
time: {
start: sinon.stub(),
stop: sinon.stub().returns('profile'),
setContext: sinon.stub(),
v8ProfilerStuckEventLoopDetected: sinon.stub().returns(false),
constants: {
kSampleCount: 0
kSampleCount: 0,
NON_JS_THREADS_FUNCTION_NAME: 'Non JS threads activity'
}
}
}
Expand Down Expand Up @@ -174,4 +176,91 @@ describe('profilers/native/wall', () => {
collectCpuTime: false
})
})

it('should generate appropriate sample labels', () => {
const profiler = new NativeWallProfiler({ timelineEnabled: true })
profiler.start()
profiler.stop()

function expectLabels (context, expected) {
const actual = profiler._generateLabels({ node: {}, context })
expect(actual).to.deep.equal(expected)
}

expect(profiler._generateLabels({ node: { name: 'Non JS threads activity' } })).to.deep.equal({
'thread name': 'Non-JS threads',
'thread id': 'NA',
'os thread id': 'NA'
})

const shared = require('../../../src/profiling/profilers/shared')
const nativeThreadId = shared.getThreadLabels()['os thread id']
const threadInfo = {
'thread name': 'Main Event Loop',
'thread id': '0',
'os thread id': nativeThreadId
}

expectLabels(undefined, threadInfo)

const threadInfoWithTimestamp = {
...threadInfo,
end_timestamp_ns: 1234000n
}

expectLabels({ timestamp: 1234n }, threadInfoWithTimestamp)

expectLabels({ timestamp: 1234n, asyncId: -1 }, threadInfoWithTimestamp)

expectLabels({ timestamp: 1234n, asyncId: 1 }, {
...threadInfoWithTimestamp,
'async id': 1
})

expectLabels({ timestamp: 1234n, context: {} }, threadInfoWithTimestamp)

expectLabels({ timestamp: 1234n, context: { ref: {} } }, threadInfoWithTimestamp)

expectLabels({ timestamp: 1234n, context: { ref: { spanId: 'foo' } } }, {
...threadInfoWithTimestamp,
'span id': 'foo'
})

expectLabels({ timestamp: 1234n, context: { ref: { rootSpanId: 'foo' } } }, {
...threadInfoWithTimestamp,
'local root span id': 'foo'
})

expectLabels({
timestamp: 1234n,
context: { ref: { webTags: { 'http.method': 'GET', 'http.route': '/foo/bar' } } }
}, {
...threadInfoWithTimestamp,
'trace endpoint': 'GET /foo/bar'
})

expectLabels({ timestamp: 1234n, context: { ref: { endpoint: 'GET /foo/bar/2' } } }, {
...threadInfoWithTimestamp,
'trace endpoint': 'GET /foo/bar/2'
})

// All at once
expectLabels({
timestamp: 1234n,
asyncId: 2,
context: {
ref: {
spanId: '1234567890',
rootSpanId: '0987654321',
webTags: { 'http.method': 'GET', 'http.route': '/foo/bar' }
}
}
}, {
...threadInfoWithTimestamp,
'async id': 2,
'span id': '1234567890',
'local root span id': '0987654321',
'trace endpoint': 'GET /foo/bar'
})
})
})
Loading