Skip to content

Commit 9a9a360

Browse files
committed
ref: Remove getSpan and use scope.getTransaction
1 parent a116d0f commit 9a9a360

File tree

11 files changed

+47
-79
lines changed

11 files changed

+47
-79
lines changed

packages/apm/src/hubextensions.ts

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

22-
/**
23-
* {@see Hub.getSpan}
24-
*/
25-
function getSpan(this: Hub, callback: (span: Span) => void): void {
26-
const scope = this.getScope();
27-
if (scope) {
28-
const span = scope.getSpan();
29-
if (span) {
30-
callback(span);
31-
}
32-
}
33-
}
34-
3522
/**
3623
* {@see Hub.startTransaction}
3724
*/
@@ -109,9 +96,6 @@ export function addExtensionMethods(): void {
10996
if (!carrier.__SENTRY__.extensions.startSpan) {
11097
carrier.__SENTRY__.extensions.startSpan = startSpan;
11198
}
112-
if (!carrier.__SENTRY__.extensions.getSpan) {
113-
carrier.__SENTRY__.extensions.getSpan = getSpan;
114-
}
11599
if (!carrier.__SENTRY__.extensions.traceHeaders) {
116100
carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;
117101
}

packages/apm/src/span.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ export class Span implements SpanInterface, SpanContext {
156156
/**
157157
* @inheritDoc
158158
*/
159-
public getTransaction(): Transaction {
159+
public getTransaction(callback: (transaction: Transaction) => void): void {
160160
const recorder = this.spanRecorder;
161-
if (!recorder) {
161+
if (!recorder || !recorder.spans[0]) {
162162
logger.warn('This Span has no reference to a Transaction. Returning an instance to itself.');
163163
logger.warn('It means that the Transaction has been sampled or the Span did not originate from a Transaction.');
164-
return (this as unknown) as Transaction;
164+
return;
165165
}
166-
return recorder.spans[0] as Transaction;
166+
callback(recorder.spans[0] as Transaction);
167167
}
168168

169169
/**

packages/apm/test/hub.test.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ describe('Hub', () => {
1818
hub.configureScope(scope => {
1919
scope.setSpan(transaction);
2020
});
21-
hub.getSpan(t => {
22-
expect(t).toBe(transaction);
21+
hub.configureScope(s => {
22+
s.getTransaction(t => {
23+
expect(t).toBe(transaction);
24+
});
2325
});
2426
});
2527

2628
test('not invoke', () => {
2729
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
2830
const transaction = hub.startTransaction({ name: 'foo' });
29-
hub.getSpan(_ => {
30-
expect(true).toBe(false);
31+
hub.configureScope(s => {
32+
s.getTransaction(_ => {
33+
expect(true).toBe(false);
34+
});
3135
});
3236
transaction.finish();
3337
});

packages/apm/test/span.test.ts

-13
Original file line numberDiff line numberDiff line change
@@ -366,19 +366,6 @@ describe('Span', () => {
366366
});
367367
});
368368

369-
test('getTransaction on Span from Transaction', () => {
370-
const transaction = hub.startTransaction({ name: 'test' });
371-
const childSpan = transaction.startChild();
372-
childSpan.finish();
373-
transaction.finish();
374-
expect(childSpan.getTransaction()).toBe(transaction);
375-
});
376-
377-
test('getTransaction on new Span()', () => {
378-
const span = new Span({});
379-
expect(span.getTransaction()).toBe(span);
380-
});
381-
382369
describe('getTraceContext', () => {
383370
test('should have status attribute undefined if no status tag is available', () => {
384371
const span = new Span({});

packages/hub/src/hub.ts

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

383-
/**
384-
* @inheritDoc
385-
*/
386-
public getSpan(callback: (span: Span) => void): void {
387-
this._callExtensionMethod<void>('getSpan', callback);
388-
}
389-
390383
/**
391384
* @inheritDoc
392385
*/

packages/hub/src/scope.ts

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ScopeContext,
99
Severity,
1010
Span,
11+
Transaction,
1112
User,
1213
} from '@sentry/types';
1314
import { getGlobalObject, isPlainObject, isThenable, SyncPromise, timestampWithMs } from '@sentry/utils';
@@ -217,6 +218,18 @@ export class Scope implements ScopeInterface {
217218
return this._span;
218219
}
219220

221+
/**
222+
* @inheritDoc
223+
*/
224+
public getTransaction(callback: (transaction: Transaction) => void): void {
225+
const span = this.getSpan() as Span & { spanRecorder: { spans: Span[] } };
226+
if (span) {
227+
if (span.spanRecorder && span.spanRecorder.spans[0]) {
228+
callback(span.spanRecorder.spans[0] as Transaction);
229+
}
230+
}
231+
}
232+
220233
/**
221234
* Inherit values from the parent scope.
222235
* @param scope to clone.

packages/minimal/src/index.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import { getCurrentHub, Hub, Scope } from '@sentry/hub';
2-
import {
3-
Breadcrumb,
4-
CaptureContext,
5-
Event,
6-
Severity,
7-
Span,
8-
Transaction,
9-
TransactionContext,
10-
User,
11-
} from '@sentry/types';
2+
import { Breadcrumb, CaptureContext, Event, Severity, Transaction, TransactionContext, User } from '@sentry/types';
123

134
/**
145
* This calls a function on the current hub.
@@ -205,11 +196,3 @@ export function _callOnClient(method: string, ...args: any[]): void {
205196
export function startTransaction(context: TransactionContext): Transaction {
206197
return callOnHub('startTransaction', { ...context });
207198
}
208-
209-
/**
210-
* Callback that receives a Span if there is one on the Scope.
211-
* @param callback Will only be invoked in case there is a Span on the Scope
212-
*/
213-
export function getSpan(callback: (span: Span) => void): void {
214-
callOnHub<void>('getSpan', callback);
215-
}

packages/node/src/integrations/http.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,14 @@ function createHandlerWrapper(
7979
}
8080

8181
let span: Span | undefined;
82-
let transaction: Transaction | undefined;
8382

8483
const scope = getCurrentHub().getScope();
85-
if (scope) {
86-
transaction = scope.getSpan() as Transaction;
87-
}
88-
89-
if (tracingEnabled && transaction) {
90-
span = transaction.startChild({
91-
description: `${typeof options === 'string' || !options.method ? 'GET' : options.method} ${requestUrl}`,
92-
op: 'request',
84+
if (scope && tracingEnabled) {
85+
scope.getTransaction(transaction => {
86+
span = transaction.startChild({
87+
description: `${typeof options === 'string' || !options.method ? 'GET' : options.method} ${requestUrl}`,
88+
op: 'request',
89+
});
9390
});
9491
}
9592

packages/types/src/hub.ts

-6
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,4 @@ export interface Hub {
199199
* @param context Properties of the new `Transaction`.
200200
*/
201201
startTransaction(context: TransactionContext): Transaction;
202-
203-
/**
204-
* Callback that receives a Span if there is one on the Scope.
205-
* @param callback Will only be invoked in case there is a Span on the Scope
206-
*/
207-
getSpan(callback: (span: Span) => void): void;
208202
}

packages/types/src/scope.ts

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Breadcrumb } from './breadcrumb';
22
import { EventProcessor } from './eventprocessor';
33
import { Severity } from './severity';
44
import { Span } from './span';
5+
import { Transaction } from './transaction';
56
import { User } from './user';
67

78
/** JSDocs */
@@ -89,6 +90,17 @@ export interface Scope {
8990
*/
9091
setSpan(span?: Span): this;
9192

93+
/**
94+
* Returns the current set span if there is one
95+
*/
96+
getSpan(): Span | undefined;
97+
98+
/**
99+
* Callback to retrieve an ongoing Transaction in case there is one.
100+
* @param callback Will only be invoked in case there is an active transaction
101+
*/
102+
getTransaction(callback: (transaction: Transaction) => void): void;
103+
92104
/**
93105
* Updates the scope with provided data. Can work in three variations:
94106
* - plain object containing updatable attributes

packages/types/src/span.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ export interface Span extends SpanContext {
136136
): Span;
137137

138138
/**
139-
* Retruns the reference to the root Span (Transaction).
139+
* Callback to retrieve the root Span (Transaction) in case there is one.
140+
* @param callback Will only be invoked in case there is an active transaction
140141
*/
141-
getTransaction(): Transaction;
142+
getTransaction(callback: (transaction: Transaction) => void): void;
142143

143144
/**
144145
* Determines whether span was successful (HTTP200)

0 commit comments

Comments
 (0)