-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node-experimental): Add
withActiveSpan
(#10194)
- Loading branch information
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getActiveSpan, getClient, startInactiveSpan, startSpan, withActiveSpan } from '../../src'; | ||
import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit'; | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
cleanupOtel(); | ||
}); | ||
|
||
describe('withActiveSpan()', () => { | ||
it('should set the active span within the callback', () => { | ||
mockSdkInit(); | ||
|
||
const inactiveSpan = startInactiveSpan({ name: 'inactive-span' }); | ||
|
||
expect(getActiveSpan()).not.toBe(inactiveSpan); | ||
|
||
withActiveSpan(inactiveSpan, () => { | ||
expect(getActiveSpan()).toBe(inactiveSpan); | ||
}); | ||
}); | ||
|
||
it('should create child spans when calling startSpan within the callback', async () => { | ||
const beforeSendTransaction = jest.fn(() => null); | ||
mockSdkInit({ enableTracing: true, beforeSendTransaction }); | ||
const client = getClient(); | ||
|
||
const inactiveSpan = startInactiveSpan({ name: 'inactive-span' }); | ||
|
||
withActiveSpan(inactiveSpan, () => { | ||
startSpan({ name: 'child-span' }, () => {}); | ||
}); | ||
|
||
startSpan({ name: 'floating-span' }, () => {}); | ||
|
||
inactiveSpan.end(); | ||
|
||
await client.flush(); | ||
|
||
// The child span should be a child of the inactive span | ||
expect(beforeSendTransaction).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
transaction: 'inactive-span', | ||
spans: expect.arrayContaining([expect.objectContaining({ description: 'child-span' })]), | ||
}), | ||
expect.anything(), | ||
); | ||
|
||
// The floating span should be a separate transaction | ||
expect(beforeSendTransaction).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
transaction: 'floating-span', | ||
}), | ||
expect.anything(), | ||
); | ||
}); | ||
}); |