Skip to content

Commit 9b36c14

Browse files
committed
feat(apm): Add ability to cancel activity
1 parent 755d4d8 commit 9b36c14

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

packages/apm/src/integrations/tracing.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// tslint:disable: max-file-line-count
12
import { Hub } from '@sentry/hub';
23
import { Event, EventProcessor, Integration, Severity, Span, SpanContext, TransactionContext } from '@sentry/types';
34
import {
@@ -116,6 +117,7 @@ interface Activity {
116117

117118
const global = getGlobalObject<Window>();
118119
const defaultTracingOrigins = ['localhost', /^\//];
120+
const SPAN_IGNORE_KEY = '__sentry_delete_span';
119121

120122
/**
121123
* Tracing Integration
@@ -474,6 +476,11 @@ export class Tracing implements Integration {
474476
return span;
475477
}
476478

479+
// if a span is supposed to be ignored, don't add it to the transaction
480+
if (span.data[SPAN_IGNORE_KEY]) {
481+
return false;
482+
}
483+
477484
// We cancel all pending spans with status "cancelled" to indicate the idle transaction was finished early
478485
if (!span.endTimestamp) {
479486
span.endTimestamp = endTimestamp;
@@ -764,7 +771,7 @@ export class Tracing implements Integration {
764771
}
765772

766773
/**
767-
* Starts tracking for a specifc activity
774+
* Starts tracking for a specific activity
768775
*
769776
* @param name Name of the activity, can be any string (Only used internally to identify the activity)
770777
* @param spanContext If provided a Span with the SpanContext will be created.
@@ -866,6 +873,31 @@ export class Tracing implements Integration {
866873
}, timeout);
867874
}
868875
}
876+
877+
/**
878+
* Cancels an activity if it exists
879+
*/
880+
public static cancelActivity(id: number): void {
881+
if (!id) {
882+
return;
883+
}
884+
885+
const activity = Tracing._activities[id];
886+
887+
if (activity) {
888+
Tracing._log(`[Tracing] cancelActivity ${activity.name}#${id}`);
889+
if (activity.span) {
890+
// Ignore the span in the transaction
891+
activity.span.setData(SPAN_IGNORE_KEY, true);
892+
}
893+
894+
// tslint:disable-next-line: no-dynamic-delete
895+
delete Tracing._activities[id];
896+
897+
const count = Object.keys(Tracing._activities).length;
898+
Tracing._log('[Tracing] activies count', count);
899+
}
900+
}
869901
}
870902

871903
/**

0 commit comments

Comments
 (0)