Skip to content

Commit a116d0f

Browse files
committed
ref: Introduce Sentry.getSpan
1 parent 4cd4146 commit a116d0f

File tree

9 files changed

+63
-20
lines changed

9 files changed

+63
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [core] feat: Export `makeMain` (#2665)
1010
- [core] fix: Call `bindClient` when creating new `Hub` to make integrations work automatically (#2665)
1111
- [gatsby] feat: Add @sentry/gatsby package (#2652)
12+
- [apm] feat: Add `Sentry.getSpan` to return the Span on the Scope (#2668)
1213

1314
## 5.17.0
1415

packages/apm/src/hubextensions.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ function traceHeaders(this: Hub): { [key: string]: string } {
2020
}
2121

2222
/**
23-
* {@see Hub.getTransaction}
23+
* {@see Hub.getSpan}
2424
*/
25-
function getTransaction(this: Hub, callback: (transaction: Transaction) => void): void {
25+
function getSpan(this: Hub, callback: (span: Span) => void): void {
2626
const scope = this.getScope();
2727
if (scope) {
28-
const span = scope.getSpan() as Transaction;
28+
const span = scope.getSpan();
2929
if (span) {
3030
callback(span);
3131
}
@@ -109,8 +109,8 @@ export function addExtensionMethods(): void {
109109
if (!carrier.__SENTRY__.extensions.startSpan) {
110110
carrier.__SENTRY__.extensions.startSpan = startSpan;
111111
}
112-
if (!carrier.__SENTRY__.extensions.getTransaction) {
113-
carrier.__SENTRY__.extensions.getTransaction = getTransaction;
112+
if (!carrier.__SENTRY__.extensions.getSpan) {
113+
carrier.__SENTRY__.extensions.getSpan = getSpan;
114114
}
115115
if (!carrier.__SENTRY__.extensions.traceHeaders) {
116116
carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;

packages/apm/src/span.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Span as SpanInterface, SpanContext } from '@sentry/types';
2-
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
1+
import { Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
2+
import { dropUndefinedKeys, logger, timestampWithMs, uuid4 } from '@sentry/utils';
33

44
import { SpanStatus } from './spanstatus';
55
import { SpanRecorder } from './transaction';
@@ -153,6 +153,19 @@ export class Span implements SpanInterface, SpanContext {
153153
return span;
154154
}
155155

156+
/**
157+
* @inheritDoc
158+
*/
159+
public getTransaction(): Transaction {
160+
const recorder = this.spanRecorder;
161+
if (!recorder) {
162+
logger.warn('This Span has no reference to a Transaction. Returning an instance to itself.');
163+
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;
165+
}
166+
return recorder.spans[0] as Transaction;
167+
}
168+
156169
/**
157170
* Continues a trace from a string (usually the header).
158171
* @param traceparent Traceparent string

packages/apm/test/hub.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ describe('Hub', () => {
1111
jest.useRealTimers();
1212
});
1313

14-
describe('getTransaction', () => {
14+
describe('getSpan', () => {
1515
test('simple invoke', () => {
1616
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
1717
const transaction = hub.startTransaction({ name: 'foo' });
1818
hub.configureScope(scope => {
1919
scope.setSpan(transaction);
2020
});
21-
hub.getTransaction(t => {
21+
hub.getSpan(t => {
2222
expect(t).toBe(transaction);
2323
});
2424
});
2525

2626
test('not invoke', () => {
2727
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
2828
const transaction = hub.startTransaction({ name: 'foo' });
29-
hub.getTransaction(_ => {
29+
hub.getSpan(_ => {
3030
expect(true).toBe(false);
3131
});
3232
transaction.finish();

packages/apm/test/span.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,19 @@ 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+
369382
describe('getTraceContext', () => {
370383
test('should have status attribute undefined if no status tag is available', () => {
371384
const span = new Span({});

packages/hub/src/hub.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ export class Hub implements HubInterface {
383383
/**
384384
* @inheritDoc
385385
*/
386-
public getTransaction(callback: (transaction: Transaction) => void): void {
387-
this._callExtensionMethod<void>('getTransaction', callback);
386+
public getSpan(callback: (span: Span) => void): void {
387+
this._callExtensionMethod<void>('getSpan', callback);
388388
}
389389

390390
/**

packages/minimal/src/index.ts

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

413
/**
514
* This calls a function on the current hub.
@@ -198,9 +207,9 @@ export function startTransaction(context: TransactionContext): Transaction {
198207
}
199208

200209
/**
201-
* Callback to retrieve an ongoing Transaction in case there is one.
202-
* @param callback Will only be invoked in case there is an active transaction
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
203212
*/
204-
export function getTransaction(callback: (transaction: Transaction) => void): void {
205-
callOnHub<void>('getTransaction', callback);
213+
export function getSpan(callback: (span: Span) => void): void {
214+
callOnHub<void>('getSpan', callback);
206215
}

packages/types/src/hub.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ export interface Hub {
201201
startTransaction(context: TransactionContext): Transaction;
202202

203203
/**
204-
* Callback to retrieve an ongoing Transaction in case there is one.
205-
* @param callback Will only be invoked in case there is an active transaction
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
206206
*/
207-
getTransaction(callback: (transaction: Transaction) => void): void;
207+
getSpan(callback: (span: Span) => void): void;
208208
}

packages/types/src/span.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Transaction } from './transaction';
2+
13
/** Interface holding all properties that can be set on a Span on creation. */
24
export interface SpanContext {
35
/**
@@ -133,6 +135,11 @@ export interface Span extends SpanContext {
133135
spanContext?: Pick<SpanContext, Exclude<keyof SpanContext, 'spanId' | 'sampled' | 'traceId' | 'parentSpanId'>>,
134136
): Span;
135137

138+
/**
139+
* Retruns the reference to the root Span (Transaction).
140+
*/
141+
getTransaction(): Transaction;
142+
136143
/**
137144
* Determines whether span was successful (HTTP200)
138145
*/

0 commit comments

Comments
 (0)