Skip to content

Commit 790ac56

Browse files
HazATkamilogorek
authored andcommitted
feat: Expose minimal API
1 parent 494a6f4 commit 790ac56

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
@@ -418,34 +418,52 @@ export class Hub implements HubInterface {
418418
/**
419419
* @inheritDoc
420420
*/
421-
public finishSpan(span: Span): string | undefined {
422-
if (!span.timestamp) {
423-
span.finish();
421+
public finishSpan(span?: Span): string | undefined {
422+
const top = this.getStackTop();
423+
let passedSpan = span;
424+
425+
// If the passed span is undefined we try to get the span from the scope and finish it.
426+
if (passedSpan === undefined) {
427+
if (top.scope && top.client) {
428+
const scopeSpan = top.scope.getSpan();
429+
if (scopeSpan) {
430+
passedSpan = scopeSpan;
431+
}
432+
}
433+
}
434+
435+
if (passedSpan === undefined) {
436+
// We will do nothing since nothing was passed and there is no Span on the scope.
437+
return undefined;
438+
}
439+
440+
if (!passedSpan.timestamp) {
441+
passedSpan.finish();
424442
}
425443

426-
if (!span.transaction) {
444+
if (!passedSpan.transaction) {
427445
return undefined;
428446
}
429447

430-
if (!this.getClient()) {
448+
if (!top.client) {
431449
return undefined;
432450
}
433451

434452
// TODO: if sampled do what?
435453

436-
const finishedSpans = span.finishedSpans.filter(s => s !== span);
454+
const finishedSpans = passedSpan.finishedSpans.filter(s => s !== span);
437455

438456
const eventId = this.captureEvent({
439-
contexts: { trace: span.getTraceContext() },
457+
contexts: { trace: passedSpan.getTraceContext() },
440458
spans: finishedSpans,
441-
start_timestamp: span.startTimestamp,
442-
timestamp: span.timestamp,
443-
transaction: span.transaction,
459+
start_timestamp: passedSpan.startTimestamp,
460+
timestamp: passedSpan.timestamp,
461+
transaction: passedSpan.transaction,
444462
type: 'transaction',
445463
});
446464

447465
// After sending we reset the finishedSpans array
448-
span.finishedSpans = [];
466+
passedSpan.finishedSpans = [];
449467
return eventId;
450468
}
451469
}

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)