Skip to content

Commit 6db3b4d

Browse files
authored
fix: Fixed api.getTraceMetadata to handle when there is an active transaction but not active segment (#2944)
1 parent b85111c commit 6db3b4d

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

api.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1537,18 +1537,24 @@ API.prototype.getTraceMetadata = function getTraceMetadata() {
15371537
NAMES.SUPPORTABILITY.API + '/getTraceMetadata'
15381538
)
15391539
metric.incrementCallCount()
1540-
15411540
const metadata = {}
15421541

1543-
const segment = this.agent.tracer.getSegment()
1542+
if (this.agent.config.distributed_tracing.enabled === false) {
1543+
return metadata
1544+
}
1545+
15441546
const transaction = this.agent.tracer.getTransaction()
1545-
if (!(segment || transaction)) {
1547+
if (!transaction) {
15461548
logger.debug('No transaction found when calling API#getTraceMetadata')
1547-
} else if (!this.agent.config.distributed_tracing.enabled) {
1548-
logger.debug('Distributed tracing disabled when calling API#getTraceMetadata')
15491549
} else {
15501550
metadata.traceId = transaction.traceId
1551+
}
1552+
1553+
const segment = this.agent.tracer.getSegment()
15511554

1555+
if (!segment) {
1556+
logger.debug('No segment found when calling API#getTraceMetadata')
1557+
} else {
15521558
const spanId = segment.getSpanId()
15531559
if (spanId) {
15541560
metadata.spanId = spanId

test/unit/api/api-get-trace-metadata.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,22 @@ test('Agent API - trace metadata', async (t) => {
7171
end()
7272
})
7373
})
74+
75+
await t.test('should not include transaction when not in active transaction', (t) => {
76+
const { api } = t.nr
77+
const metadata = api.getTraceMetadata()
78+
assert.deepEqual(metadata, {})
79+
})
80+
81+
await t.test('should not include spanId when in active active transaction but not active segment', (t, end) => {
82+
const { api, agent } = t.nr
83+
84+
helper.runInTransaction(agent, function (txn) {
85+
agent.tracer.setSegment({ segment: null })
86+
const metadata = api.getTraceMetadata()
87+
assert.equal(metadata.traceId, txn.traceId)
88+
assert.ok(!metadata.spanId)
89+
end()
90+
})
91+
})
7492
})

0 commit comments

Comments
 (0)