Skip to content

Commit 65ed4bd

Browse files
committed
feat(core): Remove startTransaction export
Instead we, for now, inline this directly into core `trace` functions, which is the only place left where we use this.
1 parent e249f36 commit 65ed4bd

File tree

19 files changed

+64
-265
lines changed

19 files changed

+64
-265
lines changed

packages/browser/src/exports.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ export {
4343
makeMain,
4444
setCurrentClient,
4545
Scope,
46-
// eslint-disable-next-line deprecation/deprecation
47-
startTransaction,
4846
continueTrace,
4947
SDK_VERSION,
5048
setContext,

packages/core/src/exports.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -124,36 +124,6 @@ export function setUser(user: User | null): ReturnType<Hub['setUser']> {
124124
getIsolationScope().setUser(user);
125125
}
126126

127-
/**
128-
* Starts a new `Transaction` and returns it. This is the entry point to manual tracing instrumentation.
129-
*
130-
* A tree structure can be built by adding child spans to the transaction, and child spans to other spans. To start a
131-
* new child span within the transaction or any span, call the respective `.startChild()` method.
132-
*
133-
* Every child span must be finished before the transaction is finished, otherwise the unfinished spans are discarded.
134-
*
135-
* The transaction must be finished with a call to its `.end()` method, at which point the transaction with all its
136-
* finished child spans will be sent to Sentry.
137-
*
138-
* NOTE: This function should only be used for *manual* instrumentation. Auto-instrumentation should call
139-
* `startTransaction` directly on the hub.
140-
*
141-
* @param context Properties of the new `Transaction`.
142-
* @param customSamplingContext Information given to the transaction sampling function (along with context-dependent
143-
* default values). See {@link Options.tracesSampler}.
144-
*
145-
* @returns The transaction which was just started
146-
*
147-
* @deprecated Use `startSpan()`, `startSpanManual()` or `startInactiveSpan()` instead.
148-
*/
149-
export function startTransaction(
150-
context: TransactionContext,
151-
customSamplingContext?: CustomSamplingContext,
152-
): ReturnType<Hub['startTransaction']> {
153-
// eslint-disable-next-line deprecation/deprecation
154-
return getCurrentHub().startTransaction({ ...context }, customSamplingContext);
155-
}
156-
157127
/**
158128
* Create a cron monitor check in and send it to Sentry.
159129
*

packages/core/src/hub.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
Breadcrumb,
44
BreadcrumbHint,
55
Client,
6-
CustomSamplingContext,
76
Event,
87
EventHint,
98
Extra,
@@ -16,8 +15,6 @@ import type {
1615
Session,
1716
SessionContext,
1817
SeverityLevel,
19-
Transaction,
20-
TransactionContext,
2118
User,
2219
} from '@sentry/types';
2320
import { GLOBAL_OBJ, consoleSandbox, dateTimestampInSeconds, isThenable, logger, uuid4 } from '@sentry/utils';
@@ -438,46 +435,6 @@ export class Hub implements HubInterface {
438435
}
439436
}
440437

441-
/**
442-
* Starts a new `Transaction` and returns it. This is the entry point to manual tracing instrumentation.
443-
*
444-
* A tree structure can be built by adding child spans to the transaction, and child spans to other spans. To start a
445-
* new child span within the transaction or any span, call the respective `.startChild()` method.
446-
*
447-
* Every child span must be finished before the transaction is finished, otherwise the unfinished spans are discarded.
448-
*
449-
* The transaction must be finished with a call to its `.end()` method, at which point the transaction with all its
450-
* finished child spans will be sent to Sentry.
451-
*
452-
* @param context Properties of the new `Transaction`.
453-
* @param customSamplingContext Information given to the transaction sampling function (along with context-dependent
454-
* default values). See {@link Options.tracesSampler}.
455-
*
456-
* @returns The transaction which was just started
457-
*
458-
* @deprecated Use `startSpan()`, `startSpanManual()` or `startInactiveSpan()` instead.
459-
*/
460-
public startTransaction(context: TransactionContext, customSamplingContext?: CustomSamplingContext): Transaction {
461-
const result = this._callExtensionMethod<Transaction>('startTransaction', context, customSamplingContext);
462-
463-
if (DEBUG_BUILD && !result) {
464-
// eslint-disable-next-line deprecation/deprecation
465-
const client = this.getClient();
466-
if (!client) {
467-
logger.warn(
468-
"Tracing extension 'startTransaction' is missing. You should 'init' the SDK before calling 'startTransaction'",
469-
);
470-
} else {
471-
logger.warn(`Tracing extension 'startTransaction' has not been added. Call 'addTracingExtensions' before calling 'init':
472-
Sentry.addTracingExtensions();
473-
Sentry.init({...});
474-
`);
475-
}
476-
}
477-
478-
return result;
479-
}
480-
481438
/**
482439
* @inheritDoc
483440
*

packages/core/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export {
1717
captureMessage,
1818
close,
1919
flush,
20-
// eslint-disable-next-line deprecation/deprecation
21-
startTransaction,
2220
setContext,
2321
setExtra,
2422
setExtras,
Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,9 @@
1-
import type { ClientOptions, CustomSamplingContext, Hub, TransactionContext } from '@sentry/types';
2-
import { getMainCarrier } from '../asyncContext';
3-
41
import { registerErrorInstrumentation } from './errors';
5-
import { sampleTransaction } from './sampling';
6-
import { Transaction } from './transaction';
72

83
/**
9-
* Creates a new transaction and adds a sampling decision if it doesn't yet have one.
10-
*
11-
* The Hub.startTransaction method delegates to this method to do its work, passing the Hub instance in as `this`, as if
12-
* it had been called on the hub directly. Exists as a separate function so that it can be injected into the class as an
13-
* "extension method."
14-
*
15-
* @param this: The Hub starting the transaction
16-
* @param transactionContext: Data used to configure the transaction
17-
* @param CustomSamplingContext: Optional data to be provided to the `tracesSampler` function (if any)
18-
*
19-
* @returns The new transaction
20-
*
21-
* @see {@link Hub.startTransaction}
22-
*/
23-
function _startTransaction(
24-
this: Hub,
25-
transactionContext: TransactionContext,
26-
customSamplingContext?: CustomSamplingContext,
27-
): Transaction {
28-
// eslint-disable-next-line deprecation/deprecation
29-
const client = this.getClient();
30-
const options: Partial<ClientOptions> = (client && client.getOptions()) || {};
31-
32-
// eslint-disable-next-line deprecation/deprecation
33-
let transaction = new Transaction(transactionContext, this);
34-
transaction = sampleTransaction(transaction, options, {
35-
name: transactionContext.name,
36-
parentSampled: transactionContext.parentSampled,
37-
transactionContext,
38-
attributes: {
39-
// eslint-disable-next-line deprecation/deprecation
40-
...transactionContext.data,
41-
...transactionContext.attributes,
42-
},
43-
...customSamplingContext,
44-
});
45-
if (client) {
46-
client.emit('startTransaction', transaction);
47-
client.emit('spanStart', transaction);
48-
}
49-
return transaction;
50-
}
51-
52-
/**
53-
* Adds tracing extensions to the global hub.
4+
* Adds tracing extensions.
5+
* TODO (v8): Do we still need this?? Can we solve this differently?
546
*/
557
export function addTracingExtensions(): void {
56-
const carrier = getMainCarrier();
57-
if (!carrier.__SENTRY__) {
58-
return;
59-
}
60-
carrier.__SENTRY__.extensions = carrier.__SENTRY__.extensions || {};
61-
if (!carrier.__SENTRY__.extensions.startTransaction) {
62-
carrier.__SENTRY__.extensions.startTransaction = _startTransaction;
63-
}
64-
658
registerErrorInstrumentation();
669
}

packages/core/src/tracing/sentrySpan.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ export class SentrySpan implements Span {
6060
private _logMessage?: string;
6161

6262
/**
63-
* You should never call the constructor manually, always use `Sentry.startTransaction()`
64-
* or call `startChild()` on an existing span.
63+
* You should never call the constructor manually, always use `Sentry.startSpan()`
64+
* or other span methods.
6565
* @internal
6666
* @hideconstructor
6767
* @hidden

packages/core/src/tracing/trace.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import type { Hub, Scope, Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';
1+
import type {
2+
ClientOptions,
3+
Hub,
4+
Scope,
5+
Span,
6+
SpanTimeInput,
7+
StartSpanOptions,
8+
TransactionContext,
9+
} from '@sentry/types';
210

311
import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/utils';
412
import type { AsyncContextStrategy } from '../asyncContext';
513
import { getMainCarrier } from '../asyncContext';
6-
import { getCurrentScope, getIsolationScope, withScope } from '../currentScopes';
14+
import { getClient, getCurrentScope, getIsolationScope, withScope } from '../currentScopes';
715

816
import { DEBUG_BUILD } from '../debug-build';
917
import { getAsyncContextStrategy, getCurrentHub } from '../hub';
@@ -17,9 +25,11 @@ import {
1725
spanToJSON,
1826
} from '../utils/spanUtils';
1927
import { getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
28+
import { sampleTransaction } from './sampling';
2029
import { SentryNonRecordingSpan } from './sentryNonRecordingSpan';
2130
import type { SentrySpan } from './sentrySpan';
2231
import { SPAN_STATUS_ERROR } from './spanstatus';
32+
import { Transaction } from './transaction';
2333
import { setCapturedScopesOnSpan } from './utils';
2434

2535
/**
@@ -324,8 +334,7 @@ function createChildSpanOrTransaction(
324334
const { traceId, spanId: parentSpanId } = parentSpan.spanContext();
325335
const sampled = spanIsSampled(parentSpan);
326336

327-
// eslint-disable-next-line deprecation/deprecation
328-
span = hub.startTransaction({
337+
span = _startTransaction({
329338
traceId,
330339
parentSpanId,
331340
parentSampled: sampled,
@@ -342,8 +351,7 @@ function createChildSpanOrTransaction(
342351
...scope.getPropagationContext(),
343352
};
344353

345-
// eslint-disable-next-line deprecation/deprecation
346-
span = hub.startTransaction({
354+
span = _startTransaction({
347355
traceId,
348356
parentSpanId,
349357
parentSampled: sampled,
@@ -390,3 +398,26 @@ function getAcs(): AsyncContextStrategy {
390398
const carrier = getMainCarrier();
391399
return getAsyncContextStrategy(carrier);
392400
}
401+
402+
function _startTransaction(transactionContext: TransactionContext): Transaction {
403+
const client = getClient();
404+
const options: Partial<ClientOptions> = (client && client.getOptions()) || {};
405+
406+
// eslint-disable-next-line deprecation/deprecation
407+
let transaction = new Transaction(transactionContext, getCurrentHub());
408+
transaction = sampleTransaction(transaction, options, {
409+
name: transactionContext.name,
410+
parentSampled: transactionContext.parentSampled,
411+
transactionContext,
412+
attributes: {
413+
// eslint-disable-next-line deprecation/deprecation
414+
...transactionContext.data,
415+
...transactionContext.attributes,
416+
},
417+
});
418+
if (client) {
419+
client.emit('startTransaction', transaction);
420+
client.emit('spanStart', transaction);
421+
}
422+
return transaction;
423+
}

packages/core/src/tracing/transaction.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ export class Transaction extends SentrySpan implements TransactionInterface {
4545
private _metadata: Partial<TransactionMetadata>;
4646

4747
/**
48-
* This constructor should never be called manually. Those instrumenting tracing should use
49-
* `Sentry.startTransaction()`, and internal methods should use `hub.startTransaction()`.
48+
* This constructor should never be called manually.
5049
* @internal
5150
* @hideconstructor
5251
* @hidden

packages/core/test/lib/tracing/trace.test.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
SEMANTIC_ATTRIBUTE_SENTRY_OP,
44
Scope,
55
addTracingExtensions,
6-
getCurrentHub,
76
getCurrentScope,
87
getGlobalScope,
98
getIsolationScope,
@@ -83,23 +82,6 @@ describe('startSpan', () => {
8382
}
8483
});
8584

86-
it('should return the same value as the callback if transactions are undefined', async () => {
87-
// @ts-expect-error we are force overriding the transaction return to be undefined
88-
// The `startTransaction` types are actually wrong - it can return undefined
89-
// if tracingExtensions are not enabled
90-
// eslint-disable-next-line deprecation/deprecation
91-
jest.spyOn(getCurrentHub(), 'startTransaction').mockImplementationOnce(() => undefined);
92-
93-
try {
94-
const result = await startSpan({ name: 'GET users/[id]' }, () => {
95-
return callback();
96-
});
97-
expect(result).toEqual(expected);
98-
} catch (e) {
99-
expect(e).toEqual(expected);
100-
}
101-
});
102-
10385
it('creates a transaction', async () => {
10486
let _span: Span | undefined = undefined;
10587
client.on('finishTransaction', transaction => {

packages/node-experimental/src/sdk/hub.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,6 @@ export function getCurrentHub(): Hub {
7676
return getClient().getIntegration(integration);
7777
},
7878

79-
startTransaction(
80-
_context: TransactionContext,
81-
_customSamplingContext?: CustomSamplingContext,
82-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
83-
): any {
84-
// eslint-disable-next-line no-console
85-
console.warn('startTransaction is a noop in @sentry/node. Use `startSpan` instead.');
86-
// We return an object here as hub.ts checks for the result of this
87-
// and renders a different warning if this is empty
88-
return {};
89-
},
90-
9179
startSession,
9280

9381
endSession,

packages/node/src/handlers.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type * as http from 'http';
33
import {
44
SEMANTIC_ATTRIBUTE_SENTRY_OP,
55
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
6+
Transaction,
67
captureException,
78
continueTrace,
89
flush,
@@ -12,7 +13,7 @@ import {
1213
getIsolationScope,
1314
hasTracingEnabled,
1415
setHttpStatus,
15-
startTransaction,
16+
startInactiveSpan,
1617
withIsolationScope,
1718
withScope,
1819
} from '@sentry/core';
@@ -21,7 +22,6 @@ import type { AddRequestDataToEventOptions } from '@sentry/utils';
2122
import {
2223
addRequestDataToTransaction,
2324
extractPathForTransaction,
24-
extractRequestData,
2525
isString,
2626
isThenable,
2727
logger,
@@ -59,14 +59,14 @@ export function tracingHandler(): (
5959
}
6060

6161
const [name, source] = extractPathForTransaction(req, { path: true, method: true });
62-
const transaction = continueTrace({ sentryTrace, baggage }, ctx =>
63-
// TODO: Refactor this to use `startSpan()`
64-
// eslint-disable-next-line deprecation/deprecation
65-
startTransaction(
66-
{
62+
const transaction = continueTrace(
63+
{ sentryTrace, baggage },
64+
ctx =>
65+
startInactiveSpan({
6766
name,
6867
op: 'http.server',
6968
origin: 'auto.http.node.tracingHandler',
69+
forceTransaction: true,
7070
...ctx,
7171
attributes: {
7272
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
@@ -80,10 +80,7 @@ export function tracingHandler(): (
8080
// be sure
8181
request: req,
8282
},
83-
},
84-
// extra context passed to the tracesSampler
85-
{ request: extractRequestData(req) },
86-
),
83+
}) as Transaction,
8784
);
8885

8986
// We put the transaction on the scope so users can attach children to it

0 commit comments

Comments
 (0)