Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 1597ba7

Browse files
crdgonzalezcadraffensperger
authored andcommitted
Fix schedule and cancel task patch (#150)
1 parent 34b5bc1 commit 1597ba7

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

packages/opencensus-web-instrumentation-zone-peer-dep/src/interaction-tracker.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
startOnPageInteraction,
2727
} from './on-page-interaction-stop-watch';
2828

29-
import { isTrackedTask, getTraceId, resolveInteractionName } from './util';
29+
import { isTracingZone, getTraceId, resolveInteractionName } from './util';
3030
import { interceptXhrTask } from './xhr-interceptor';
3131

3232
// Allows us to monkey patch Zone prototype without TS compiler errors.
@@ -99,7 +99,7 @@ export class InteractionTracker {
9999
} finally {
100100
if (
101101
interceptingElement ||
102-
(shouldCountTask(task) && isTrackedTask(task))
102+
(shouldCountTask(task) && isTracingZone(task.zone))
103103
) {
104104
this.decrementTaskCount(getTraceId(task.zone));
105105
}
@@ -110,14 +110,16 @@ export class InteractionTracker {
110110
private patchZoneScheduleTask() {
111111
const scheduleTask = Zone.prototype.scheduleTask;
112112
Zone.prototype.scheduleTask = <T extends Task>(task: T) => {
113-
let taskZone = Zone.current;
114-
if (isTrackedTask(task)) {
115-
taskZone = task.zone;
113+
const currentZone = Zone.current;
114+
if (isTracingZone(currentZone)) {
115+
// Cast first as Task and then as AsyncTask because the direct
116+
// cast from type `T` is not possible.
117+
((task as Task) as AsyncTask)._zone = currentZone;
116118
}
117119
try {
118-
return scheduleTask.call(taskZone as {}, task) as T;
120+
return scheduleTask.call(currentZone as {}, task) as T;
119121
} finally {
120-
if (shouldCountTask(task) && isTrackedTask(task)) {
122+
if (shouldCountTask(task) && isTracingZone(task.zone)) {
121123
this.incrementTaskCount(getTraceId(task.zone));
122124
}
123125
}
@@ -127,15 +129,15 @@ export class InteractionTracker {
127129
private patchZoneCancelTask() {
128130
const cancelTask = Zone.prototype.cancelTask;
129131
Zone.prototype.cancelTask = (task: AsyncTask) => {
130-
let taskZone = Zone.current;
131-
if (isTrackedTask(task)) {
132-
taskZone = task.zone;
132+
const currentZone = Zone.current;
133+
if (isTracingZone(currentZone)) {
134+
task._zone = currentZone;
133135
}
134136

135137
try {
136-
return cancelTask.call(taskZone as {}, task);
138+
return cancelTask.call(currentZone as {}, task);
137139
} finally {
138-
if (isTrackedTask(task) && shouldCountTask(task)) {
140+
if (isTracingZone(task.zone) && shouldCountTask(task)) {
139141
this.decrementTaskCount(getTraceId(task.zone));
140142
}
141143
}

packages/opencensus-web-instrumentation-zone-peer-dep/src/util.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ export function traceOriginMatchesOrSameOrigin(xhrUrl: string): boolean {
3131
/**
3232
* Whether or not a task is being tracked as part of an interaction.
3333
*/
34-
export function isTrackedTask(task: Task): boolean {
35-
return !!(
36-
task.zone &&
37-
task.zone.get('data') &&
38-
task.zone.get('data').isTracingZone
39-
);
34+
export function isTracingZone(zone: Zone): boolean {
35+
return !!(zone && zone.get('data') && zone.get('data').isTracingZone);
4036
}
4137

4238
/**

packages/opencensus-web-instrumentation-zone-peer-dep/src/xhr-interceptor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
SpanKind,
2525
} from '@opencensus/web-core';
2626
import { getXhrPerfomanceData } from './perf-resource-timing-selector';
27-
import { traceOriginMatchesOrSameOrigin, isTrackedTask } from './util';
27+
import { traceOriginMatchesOrSameOrigin, isTracingZone } from './util';
2828
import { spanContextToTraceParent } from '@opencensus/web-propagation-tracecontext';
2929
import {
3030
annotationsForPerfTimeFields,
@@ -61,7 +61,7 @@ export const alreadyAssignedPerfEntries = new Set<PerformanceResourceTiming>();
6161
* In case the XHR is DONE, end the child span.
6262
*/
6363
export function interceptXhrTask(task: AsyncTask) {
64-
if (!isTrackedTask(task)) return;
64+
if (!isTracingZone(task.zone)) return;
6565
if (!(task.target instanceof XMLHttpRequest)) return;
6666

6767
const xhr = task.target as XhrWithOcWebData;

0 commit comments

Comments
 (0)