Skip to content

Commit 5b6ea3d

Browse files
authored
ref: Use startTransaction where appropriate (#2644)
Follow up on #2626. Now that startTransaction exists, replace usages of startSpan where the intention is to create a transaction. Additionally, documentation updates and some refactoring to minimize type castings and linter flag usage.
1 parent fc53353 commit 5b6ea3d

File tree

6 files changed

+47
-35
lines changed

6 files changed

+47
-35
lines changed

packages/apm/src/hubextensions.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,23 @@ function startTransaction(this: Hub, context: TransactionContext): Transaction {
5151
function startSpan(this: Hub, context: SpanContext): Transaction | Span {
5252
/**
5353
* @deprecated
54-
* This is here to make sure we don't break users that relied on calling startSpan to create a transaction
55-
* with the transaction poperty set.
54+
* TODO: consider removing this in a future release.
55+
*
56+
* This is for backwards compatibility with releases before startTransaction
57+
* existed, to allow for a smoother transition.
5658
*/
57-
if ((context as any).transaction !== undefined) {
58-
logger.warn(`Use \`Sentry.startTransaction({name: ${(context as any).transaction}})\` to start a Transaction.`);
59-
(context as TransactionContext).name = (context as any).transaction as string;
60-
}
61-
62-
// We have the check of not undefined since we defined it's ok to start a transaction with an empty name
63-
// tslint:disable-next-line: strict-type-predicates
64-
if ((context as TransactionContext).name !== undefined) {
65-
return this.startTransaction(context as TransactionContext);
59+
{
60+
// The `TransactionContext.name` field used to be called `transaction`.
61+
const transactionContext = context as Partial<TransactionContext & { transaction: string }>;
62+
if (transactionContext.transaction !== undefined) {
63+
transactionContext.name = transactionContext.transaction;
64+
}
65+
// Check for not undefined since we defined it's ok to start a transaction
66+
// with an empty name.
67+
if (transactionContext.name !== undefined) {
68+
logger.warn('Deprecated: Use startTransaction to start transactions and Transaction.startChild to start spans.');
69+
return this.startTransaction(transactionContext as TransactionContext);
70+
}
6671
}
6772

6873
const scope = this.getScope();

packages/apm/src/integrations/tracing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ export class Tracing implements Integration {
431431
return undefined;
432432
}
433433

434-
Tracing._activeTransaction = hub.startSpan({
434+
Tracing._activeTransaction = hub.startTransaction({
435435
trimEnd: true,
436436
...transactionContext,
437437
}) as Transaction;

packages/apm/test/hub.test.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { BrowserClient } from '@sentry/browser';
22
import { Hub, Scope } from '@sentry/hub';
3-
import { Span, Transaction } from '@sentry/types';
43

54
import { addExtensionMethods } from '../src/hubextensions';
65

@@ -21,21 +20,18 @@ describe('Hub', () => {
2120
});
2221
test('set tracesSampleRate 0 on transaction', () => {
2322
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 }));
24-
// @ts-ignore
25-
const transaction = hub.startSpan({ name: 'foo' }) as any;
23+
const transaction = hub.startTransaction({ name: 'foo' });
2624
expect(transaction.sampled).toBe(false);
2725
});
2826
test('set tracesSampleRate 1 on transaction', () => {
2927
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
30-
// @ts-ignore
31-
const transaction = hub.startSpan({ name: 'foo' }) as any;
28+
const transaction = hub.startTransaction({ name: 'foo' });
3229
expect(transaction.sampled).toBeTruthy();
3330
});
3431
test('set tracesSampleRate should be propergated to children', () => {
3532
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 }));
36-
// @ts-ignore
37-
const transaction = hub.startSpan({ name: 'foo' }) as any;
38-
const child = transaction.startChild({ op: 1 });
33+
const transaction = hub.startTransaction({ name: 'foo' });
34+
const child = transaction.startChild({ op: 'test' });
3935
expect(child.sampled).toBeFalsy();
4036
});
4137
});
@@ -49,8 +45,7 @@ describe('Hub', () => {
4945

5046
test('simple standalone Transaction', () => {
5147
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
52-
// @ts-ignore
53-
const transaction = hub.startSpan({ name: 'transaction' }) as Transaction;
48+
const transaction = hub.startTransaction({ name: 'transaction' });
5449
expect(transaction.spanId).toBeTruthy();
5550
// tslint:disable-next-line: no-unbound-method
5651
expect(transaction.setName).toBeTruthy();
@@ -71,8 +66,7 @@ describe('Hub', () => {
7166
test('create a child if there is a Span already on the scope', () => {
7267
const myScope = new Scope();
7368
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }), myScope);
74-
// @ts-ignore
75-
const transaction = hub.startSpan({ name: 'transaction' }) as Transaction;
69+
const transaction = hub.startTransaction({ name: 'transaction' });
7670
hub.configureScope(scope => {
7771
scope.setSpan(transaction);
7872
});

packages/minimal/src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,21 @@ export function _callOnClient(method: string, ...args: any[]): void {
177177
}
178178

179179
/**
180-
* Starts a Transaction. This is the entry point to do manual tracing. You can
181-
* add child spans to transactions. Spans themselves can have children, building
182-
* a tree structure. This function returns a Transaction and you need to keep
183-
* track of the instance yourself. When you call `.finish()` on the transaction
184-
* it will be sent to Sentry.
180+
* Starts a new `Transaction` and returns it. This is the entry point to manual
181+
* tracing instrumentation.
182+
*
183+
* A tree structure can be built by adding child spans to the transaction, and
184+
* child spans to other spans. To start a new child span within the transaction
185+
* or any span, call the respective `.startChild()` method.
186+
*
187+
* Every child span must be finished before the transaction is finished,
188+
* otherwise the unfinished spans are discarded.
189+
*
190+
* The transaction must be finished with a call to its `.finish()` method, at
191+
* which point the transaction with all its finished child spans will be sent to
192+
* Sentry.
193+
*
194+
* @param context Properties of the new `Transaction`.
185195
*/
186196
export function startTransaction(context: TransactionContext): Transaction {
187197
return callOnHub('startTransaction', { ...context });

packages/node/test/manual/apm-transaction/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Tracing {
2424
? Span.fromTraceparent(req.headers['sentry-trace'], {
2525
transaction,
2626
})
27-
: Sentry.getCurrentHub().startSpan({
28-
transaction,
27+
: Sentry.startTransaction({
28+
name: transaction,
2929
});
3030

3131
Sentry.getCurrentHub().configureScope(scope => {

packages/types/src/hub.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,15 @@ export interface Hub {
174174
traceHeaders(): { [key: string]: string };
175175

176176
/**
177-
* This function starts a span. If there is already a `Span` on the Scope,
178-
* the created Span with the SpanContext will have a reference to it and become it's child.
179-
* Otherwise it'll create a new `Span`.
177+
* Starts a new `Span` and returns it. If there is a `Span` on the `Scope`,
178+
* the new `Span` will be a child of the existing `Span`.
179+
*
180+
* @param context Properties of the new `Span`.
180181
*/
181182
startSpan(context: SpanContext): Span;
182183

183184
/**
184-
* Starts a new transaction and returns it. This is the entry point to manual
185+
* Starts a new `Transaction` and returns it. This is the entry point to manual
185186
* tracing instrumentation.
186187
*
187188
* A tree structure can be built by adding child spans to the transaction, and
@@ -194,6 +195,8 @@ export interface Hub {
194195
* The transaction must be finished with a call to its `.finish()` method, at
195196
* which point the transaction with all its finished child spans will be sent to
196197
* Sentry.
198+
*
199+
* @param context Properties of the new `Transaction`.
197200
*/
198201
startTransaction(context: TransactionContext): Transaction;
199202
}

0 commit comments

Comments
 (0)