Skip to content

Commit de78c96

Browse files
committed
feat: Safeguard for name prop in transaction
1 parent 46d2526 commit de78c96

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

packages/apm/src/hubextensions.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getMainCarrier, Hub } from '@sentry/hub';
22
import { SpanContext, TransactionContext } from '@sentry/types';
3+
import { logger } from '@sentry/utils';
34

45
import { Span } from './span';
56
import { Transaction } from './transaction';
@@ -48,8 +49,16 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
4849
}
4950
}
5051

51-
// We are dealing with a Transaction
52+
// This is our safeguard so people always get a Transaction in return.
53+
// We set `_isTransaction: true` in {@link Sentry.startTransaction} to have a runtime check
54+
// if the user really wanted to create a Transaction.
55+
if ((context as TransactionContext)._isTransaction && !(context as TransactionContext).name) {
56+
logger.warn('You are trying to start a Transaction but forgot to provide a `name` property.');
57+
logger.warn('Will fall back to <unlabeled transaction>, use `transaction.setName()` to change it.');
58+
}
59+
5260
if ((newSpanContext as TransactionContext).name) {
61+
// We are dealing with a Transaction
5362
const transaction = new Transaction(newSpanContext as TransactionContext, hub);
5463

5564
// We only roll the dice on sampling for root spans of transactions because all child spans inherit this state

packages/minimal/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,8 @@ export function _callOnClient(method: string, ...args: any[]): void {
175175
* a transaction it will be sent to Sentry.
176176
*/
177177
export function startTransaction(transactionContext: TransactionContext): Transaction {
178-
return callOnHub<Transaction>('startSpan', transactionContext);
178+
return callOnHub<Transaction>('startSpan', {
179+
...transactionContext,
180+
_isTransaction: true,
181+
});
179182
}

packages/types/src/transaction.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ export interface TransactionContext extends SpanContext {
1212
* transaction after a given "idle time" and we don't want this "idle time" to be part of the transaction.
1313
*/
1414
trimEnd?: boolean;
15+
16+
/**
17+
* This flag is internally used by {@link Sentry.startTransaction} and set there to determine this is really a
18+
* Transaction. Since there is no type safety in JS we set it in the top level function to true to check later
19+
* if the user really wanted to create a Transaction and we can log a warning if they forgot to set the name property.
20+
*/
21+
_isTransaction?: boolean;
1522
}
1623

1724
/**

0 commit comments

Comments
 (0)