Skip to content
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

feat: Add scope.getTransaction, rename scope.setTransaction -> setTransactionName #2668

Merged
merged 16 commits into from Jun 22, 2020
16 changes: 16 additions & 0 deletions packages/apm/src/hubextensions.ts
Expand Up @@ -19,6 +19,19 @@ function traceHeaders(this: Hub): { [key: string]: string } {
return {};
}

/**
* {@see Hub.getTransaction}
*/
function getTransaction(this: Hub, callback: (transaction: Transaction) => void): void {
const scope = this.getScope();
if (scope) {
const span = scope.getSpan() as Transaction;
HazAT marked this conversation as resolved.
Show resolved Hide resolved
if (span) {
callback(span);
}
}
}

/**
* {@see Hub.startTransaction}
*/
Expand Down Expand Up @@ -96,6 +109,9 @@ export function addExtensionMethods(): void {
if (!carrier.__SENTRY__.extensions.startSpan) {
carrier.__SENTRY__.extensions.startSpan = startSpan;
}
if (!carrier.__SENTRY__.extensions.getTransaction) {
carrier.__SENTRY__.extensions.getTransaction = getTransaction;
}
if (!carrier.__SENTRY__.extensions.traceHeaders) {
carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;
}
Expand Down
22 changes: 22 additions & 0 deletions packages/apm/test/hub.test.ts
Expand Up @@ -11,6 +11,28 @@ describe('Hub', () => {
jest.useRealTimers();
});

describe('getTransaction', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are not technically "correct". defined/undefined test should be moved to scope.test.ts, as this functionality is owned by the Scope, and tests below should only assert that a given method was called on the scope. expect(s.getTransaction).toBeCalled().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to scope.test.ts

test('simple invoke', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
const transaction = hub.startTransaction({ name: 'foo' });
hub.configureScope(scope => {
scope.setSpan(transaction);
});
hub.getTransaction(t => {
expect(t).toBe(transaction);
});
});

test('not invoke', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
const transaction = hub.startTransaction({ name: 'foo' });
hub.getTransaction(_ => {
expect(true).toBe(false);
});
transaction.finish();
});
});

describe('spans', () => {
describe('sampling', () => {
test('set tracesSampleRate 0 on span', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/hub/src/hub.ts
Expand Up @@ -380,6 +380,13 @@ export class Hub implements HubInterface {
return this._callExtensionMethod('startTransaction', context);
}

/**
* @inheritDoc
*/
public getTransaction(callback: (transaction: Transaction) => void): void {
this._callExtensionMethod<void>('getTransaction', callback);
}

/**
* @inheritDoc
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/minimal/src/index.ts
Expand Up @@ -196,3 +196,11 @@ export function _callOnClient(method: string, ...args: any[]): void {
export function startTransaction(context: TransactionContext): Transaction {
return callOnHub('startTransaction', { ...context });
}

/**
* Callback to retrieve an ongoing Transaction in case there is one.
* @param callback Will only be invoked in case there is an active transaction
*/
export function getTransaction(callback: (transaction: Transaction) => void): void {
callOnHub<void>('getTransaction', callback);
}
6 changes: 6 additions & 0 deletions packages/types/src/hub.ts
Expand Up @@ -199,4 +199,10 @@ export interface Hub {
* @param context Properties of the new `Transaction`.
*/
startTransaction(context: TransactionContext): Transaction;

/**
* Callback to retrieve an ongoing Transaction in case there is one.
* @param callback Will only be invoked in case there is an active transaction
*/
getTransaction(callback: (transaction: Transaction) => void): void;
}