Skip to content

Commit ced9e22

Browse files
authored
fix: Fixed tracer.transactionNestProxy to create a new transaction if there is no transaction or transaction is not active (#3007)
1 parent 5403018 commit ced9e22

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/transaction/tracer/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,11 @@ function transactionNestProxy(type, handler) {
222222

223223
// don't nest transactions, reuse existing ones
224224
let context = tracer.getContext()
225+
const transaction = tracer.getTransaction()
225226

226227
let createNew = false
227228

228-
if (!context?.transaction || context?.transaction.type !== type) {
229+
if (!transaction || transaction.type !== type) {
229230
createNew = true
230231
}
231232

test/unit/api/api-start-background-transaction.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,28 @@ test('Agent API - startBackgroundTransaction', async (t) => {
270270
)
271271
})
272272
)
273+
274+
await t.test('should allow nesting startBackgroundTransaction', function(t, end) {
275+
const { api, tracer } = t.nr
276+
let called = false
277+
function bg() {
278+
if (!called) {
279+
called = true
280+
setTimeout(() => {
281+
wrap('second')
282+
}, 10)
283+
} else {
284+
end()
285+
}
286+
}
287+
288+
function wrap(name) {
289+
api.startBackgroundTransaction(name, () => {
290+
const tx = tracer.getTransaction()
291+
assert.ok(tx._partialName, `Nodejs/${name}`)
292+
bg()
293+
})
294+
}
295+
wrap('first')
296+
})
273297
})

0 commit comments

Comments
 (0)