Skip to content

Commit cfa2302

Browse files
committed
feat: Expose minimal API
1 parent 826d03d commit cfa2302

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

packages/browser/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export {
2020
captureEvent,
2121
captureMessage,
2222
configureScope,
23+
finishSpan,
2324
getHubFromCarrier,
2425
getCurrentHub,
2526
Hub,
@@ -30,6 +31,7 @@ export {
3031
setTag,
3132
setTags,
3233
setUser,
34+
startSpan,
3335
Span,
3436
withScope,
3537
} from '@sentry/core';

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ export {
44
captureEvent,
55
captureMessage,
66
configureScope,
7+
finishSpan,
78
setContext,
89
setExtra,
910
setExtras,
1011
setTag,
1112
setTags,
1213
setUser,
14+
startSpan,
1315
withScope,
1416
} from '@sentry/minimal';
1517
export { addGlobalEventProcessor, getCurrentHub, getHubFromCarrier, Hub, Scope, Span } from '@sentry/hub';

packages/hub/src/hub.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -410,34 +410,52 @@ export class Hub implements HubInterface {
410410
/**
411411
* @inheritDoc
412412
*/
413-
public finishSpan(span: Span): string | undefined {
414-
if (!span.timestamp) {
415-
span.finish();
413+
public finishSpan(span?: Span): string | undefined {
414+
const top = this.getStackTop();
415+
let passedSpan = span;
416+
417+
// If the passed span is undefined we try to get the span from the scope and finish it.
418+
if (passedSpan === undefined) {
419+
if (top.scope && top.client) {
420+
const scopeSpan = top.scope.getSpan();
421+
if (scopeSpan) {
422+
passedSpan = scopeSpan;
423+
}
424+
}
425+
}
426+
427+
if (passedSpan === undefined) {
428+
// We will do nothing since nothing was passed and there is no Span on the scope.
429+
return undefined;
430+
}
431+
432+
if (!passedSpan.timestamp) {
433+
passedSpan.finish();
416434
}
417435

418-
if (!span.transaction) {
436+
if (!passedSpan.transaction) {
419437
return undefined;
420438
}
421439

422-
if (!this.getClient()) {
440+
if (!top.client) {
423441
return undefined;
424442
}
425443

426444
// TODO: if sampled do what?
427445

428-
const finishedSpans = span.finishedSpans.filter(s => s !== span);
446+
const finishedSpans = passedSpan.finishedSpans.filter(s => s !== span);
429447

430448
const eventId = this.captureEvent({
431-
contexts: { trace: span.getTraceContext() },
449+
contexts: { trace: passedSpan.getTraceContext() },
432450
spans: finishedSpans,
433-
start_timestamp: span.startTimestamp,
434-
timestamp: span.timestamp,
435-
transaction: span.transaction,
451+
start_timestamp: passedSpan.startTimestamp,
452+
timestamp: passedSpan.timestamp,
453+
transaction: passedSpan.transaction,
436454
type: 'transaction',
437455
});
438456

439457
// After sending we reset the finishedSpans array
440-
span.finishedSpans = [];
458+
passedSpan.finishedSpans = [];
441459
return eventId;
442460
}
443461
}

packages/minimal/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export function withScope(callback: (scope: Scope) => void): void {
162162
*
163163
* @param method The method to call on the client/client.
164164
* @param args Arguments to pass to the client/fontend.
165+
* @hidden
165166
*/
166167
export function _callOnClient(method: string, ...args: any[]): void {
167168
callOnHub<void>('_invokeClient', method, ...args);
@@ -188,6 +189,6 @@ export function startSpan(spanContext?: SpanContext, bindOnScope?: boolean): Spa
188189
*
189190
* @param span `Span` instance that was created by {@link startSpan}
190191
*/
191-
export function finishSpan(span: Span): string | undefined {
192+
export function finishSpan(span?: Span): string | undefined {
192193
return callOnHub<string | undefined>('startSpan', span);
193194
}

packages/node/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export {
2020
captureEvent,
2121
captureMessage,
2222
configureScope,
23-
getCurrentHub,
23+
finishSpan,
2424
getHubFromCarrier,
25+
getCurrentHub,
2526
Hub,
2627
Scope,
2728
setContext,
@@ -30,6 +31,7 @@ export {
3031
setTag,
3132
setTags,
3233
setUser,
34+
startSpan,
3335
Span,
3436
withScope,
3537
} from '@sentry/core';

packages/types/src/hub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,5 @@ export interface Hub {
191191
*
192192
* @param span `Span` instance that was created by {@link startSpan}
193193
*/
194-
finishSpan(span: Span): string | undefined;
194+
finishSpan(span?: Span): string | undefined;
195195
}

0 commit comments

Comments
 (0)