Skip to content

Commit 6763375

Browse files
committed
2 parents 5241daa + d6fbe5f commit 6763375

5 files changed

Lines changed: 57 additions & 15 deletions

File tree

plugins/node/opentelemetry-plugin-dns/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"@opentelemetry/tracing": "0.9.0",
4646
"@types/mocha": "7.0.2",
4747
"@types/node": "12.12.47",
48-
"@types/semver": "7.2.0",
48+
"@types/semver": "7.3.0",
4949
"@types/shimmer": "1.0.1",
5050
"@types/sinon": "9.0.4",
5151
"codecov": "3.7.0",

plugins/node/opentelemetry-plugin-mongodb/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"@opentelemetry/node": "0.9.0",
4646
"@opentelemetry/tracing": "0.9.0",
4747
"@types/mocha": "7.0.2",
48-
"@types/mongodb": "3.5.22",
48+
"@types/mongodb": "3.5.25",
4949
"@types/node": "12.12.47",
5050
"@types/shimmer": "1.0.1",
5151
"codecov": "3.7.0",

plugins/web/opentelemetry-plugin-user-interaction/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
},
4444
"devDependencies": {
4545
"@babel/core": "7.10.3",
46+
"@opentelemetry/context-base": "0.9.0",
4647
"@opentelemetry/context-zone-peer-dep": "0.9.0",
4748
"@opentelemetry/plugin-xml-http-request": "0.9.0",
4849
"@opentelemetry/tracing": "0.9.0",

plugins/web/opentelemetry-plugin-user-interaction/src/userInteraction.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,28 +313,31 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
313313
): Zone {
314314
const target: HTMLElement | undefined = plugin._getClickedElement(task);
315315
let span: api.Span | undefined;
316+
const activeZone = this;
316317
if (target) {
317318
span = plugin._createSpan(target, 'click');
318319
if (span) {
319320
plugin._incrementTask(span);
320-
try {
321-
return plugin._tracer.withSpan(span, () => {
322-
const currentZone = Zone.current;
323-
task._zone = currentZone;
324-
return original.call(currentZone, task, applyThis, applyArgs);
325-
});
326-
} finally {
327-
plugin._decrementTask(span);
328-
}
321+
return activeZone.run(() => {
322+
try {
323+
return plugin._tracer.withSpan(span as api.Span, () => {
324+
const currentZone = Zone.current;
325+
task._zone = currentZone;
326+
return original.call(currentZone, task, applyThis, applyArgs);
327+
});
328+
} finally {
329+
plugin._decrementTask(span as api.Span);
330+
}
331+
});
329332
}
330333
} else {
331-
span = plugin._getCurrentSpan(this);
334+
span = plugin._getCurrentSpan(activeZone);
332335
}
333336

334337
try {
335-
return original.call(this, task, applyThis, applyArgs);
338+
return original.call(activeZone, task, applyThis, applyArgs);
336339
} finally {
337-
if (span && plugin._shouldCountTask(task, Zone.current)) {
340+
if (span && plugin._shouldCountTask(task, activeZone)) {
338341
plugin._decrementTask(span);
339342
}
340343
}

plugins/web/opentelemetry-plugin-user-interaction/test/userInteraction.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// because of zone original timeout needs to be patched to be able to run
1818
// code outside zone.js. This needs to be done before all
1919
const originalSetTimeout = window.setTimeout;
20-
20+
import { Context } from '@opentelemetry/context-base';
2121
import { context } from '@opentelemetry/api';
2222
import { isWrapped, LogLevel } from '@opentelemetry/core';
2323
import { XMLHttpRequestPlugin } from '@opentelemetry/plugin-xml-http-request';
@@ -200,6 +200,44 @@ describe('UserInteractionPlugin', () => {
200200
});
201201
});
202202

203+
it('should run task from different zone - angular test', done => {
204+
const context = Context.ROOT_CONTEXT;
205+
const rootZone = Zone.current;
206+
207+
interface CtxMngrWithPrv {
208+
_createZone: Function;
209+
}
210+
211+
const ctxMngrWithPrv = (contextManager as unknown) as CtxMngrWithPrv;
212+
const newZone = ctxMngrWithPrv._createZone('test', context);
213+
214+
const element = createButton();
215+
element.addEventListener('click', () => {
216+
assert.ok(
217+
Zone.current !== newZone,
218+
'Current zone for 2nd listener click is wrong'
219+
);
220+
assert.ok(
221+
Zone.current.parent === rootZone,
222+
'Parent Zone for 2nd listener click is wrong'
223+
);
224+
});
225+
226+
newZone.run(() => {
227+
assert.ok(Zone.current === newZone, 'New zone is wrong');
228+
fakeInteraction(() => {
229+
assert.ok(
230+
Zone.current.parent === newZone,
231+
'Parent zone for click is wrong'
232+
);
233+
const spanClick: tracing.ReadableSpan = exportSpy.args[0][0][0];
234+
assertClickSpan(spanClick);
235+
236+
done();
237+
}, element);
238+
});
239+
});
240+
203241
it('should ignore interaction when element is disabled', done => {
204242
const btn = createButton(true);
205243
let called = false;

0 commit comments

Comments
 (0)