Skip to content

Commit 5728da3

Browse files
committed
feat: Add Sentry.getTransaction
1 parent e8093c2 commit 5728da3

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

packages/apm/src/hubextensions.ts

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ function traceHeaders(this: Hub): { [key: string]: string } {
1919
return {};
2020
}
2121

22+
/**
23+
* {@see Hub.getTransaction}
24+
*/
25+
function getTransaction(this: Hub, callback: (transaction: Transaction) => void): void {
26+
const scope = this.getScope();
27+
if (scope) {
28+
const span = scope.getSpan() as Transaction;
29+
if (span) {
30+
callback(span);
31+
}
32+
}
33+
}
34+
2235
/**
2336
* {@see Hub.startTransaction}
2437
*/
@@ -96,6 +109,9 @@ export function addExtensionMethods(): void {
96109
if (!carrier.__SENTRY__.extensions.startSpan) {
97110
carrier.__SENTRY__.extensions.startSpan = startSpan;
98111
}
112+
if (!carrier.__SENTRY__.extensions.getTransaction) {
113+
carrier.__SENTRY__.extensions.getTransaction = getTransaction;
114+
}
99115
if (!carrier.__SENTRY__.extensions.traceHeaders) {
100116
carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;
101117
}

packages/apm/test/hub.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ describe('Hub', () => {
1111
jest.useRealTimers();
1212
});
1313

14+
describe('getTransaction', () => {
15+
test('simple invoke', () => {
16+
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
17+
const transaction = hub.startTransaction({ name: 'foo' });
18+
hub.configureScope(scope => {
19+
scope.setSpan(transaction);
20+
});
21+
hub.getTransaction(t => {
22+
expect(t).toBe(transaction);
23+
});
24+
});
25+
26+
test('not invoke', () => {
27+
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
28+
const transaction = hub.startTransaction({ name: 'foo' });
29+
hub.getTransaction(_ => {
30+
expect(true).toBe(false);
31+
});
32+
transaction.finish();
33+
});
34+
});
35+
1436
describe('spans', () => {
1537
describe('sampling', () => {
1638
test('set tracesSampleRate 0 on span', () => {

packages/hub/src/hub.ts

+7
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@ export class Hub implements HubInterface {
380380
return this._callExtensionMethod('startTransaction', context);
381381
}
382382

383+
/**
384+
* @inheritDoc
385+
*/
386+
public getTransaction(callback: (transaction: Transaction) => void): void {
387+
this._callExtensionMethod('getTransaction', callback);
388+
}
389+
383390
/**
384391
* @inheritDoc
385392
*/

packages/minimal/src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,11 @@ export function _callOnClient(method: string, ...args: any[]): void {
196196
export function startTransaction(context: TransactionContext): Transaction {
197197
return callOnHub('startTransaction', { ...context });
198198
}
199+
200+
/**
201+
* Callback to retrieve an ongoing Transaction in case there is one.
202+
* @param callback Will only be invoked in case there is an active transaction
203+
*/
204+
export function getTransaction(callback: (transaction: Transaction) => void): void {
205+
callOnHub<void>('getTransaction', callback);
206+
}

packages/types/src/hub.ts

+6
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,10 @@ export interface Hub {
199199
* @param context Properties of the new `Transaction`.
200200
*/
201201
startTransaction(context: TransactionContext): Transaction;
202+
203+
/**
204+
* Callback to retrieve an ongoing Transaction in case there is one.
205+
* @param callback Will only be invoked in case there is an active transaction
206+
*/
207+
getTransaction(callback: (transaction: Transaction) => void): void;
202208
}

0 commit comments

Comments
 (0)