Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fixing running task in a proper zone preserving the active zone #88

Merged
merged 4 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@opentelemetry/context-zone-peer-dep": "0.8.3",
"@opentelemetry/plugin-xml-http-request": "0.8.3",
"@opentelemetry/tracing": "0.8.3",
"@opentelemetry/context-base": "0.8.3",
obecny marked this conversation as resolved.
Show resolved Hide resolved
"@types/jquery": "3.3.38",
"@types/mocha": "7.0.2",
"@types/node": "12.12.47",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,28 +313,31 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
): Zone {
const target: HTMLElement | undefined = plugin._getClickedElement(task);
let span: api.Span | undefined;
const activeZone = this;
if (target) {
span = plugin._createSpan(target, 'click');
if (span) {
plugin._incrementTask(span);
try {
return plugin._tracer.withSpan(span, () => {
const currentZone = Zone.current;
task._zone = currentZone;
return original.call(currentZone, task, applyThis, applyArgs);
});
} finally {
plugin._decrementTask(span);
}
return activeZone.run(() => {
try {
return plugin._tracer.withSpan(span as api.Span, () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have to do Type Assertion here (span as api.Span)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be required as the assertion on line 319 should remove undefined from the type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a compiler error / issue. the internal _createSpan returns api.Span or undefined. Even though there is if check for span existence the compiler still complains. If you have a better idea how to change it I'm open for suggestion.

const currentZone = Zone.current;
task._zone = currentZone;
return original.call(currentZone, task, applyThis, applyArgs);
});
} finally {
plugin._decrementTask(span as api.Span);
}
});
}
} else {
span = plugin._getCurrentSpan(this);
span = plugin._getCurrentSpan(activeZone);
}

try {
return original.call(this, task, applyThis, applyArgs);
return original.call(activeZone, task, applyThis, applyArgs);
} finally {
if (span && plugin._shouldCountTask(task, Zone.current)) {
if (span && plugin._shouldCountTask(task, activeZone)) {
plugin._decrementTask(span);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// because of zone original timeout needs to be patched to be able to run
// code outside zone.js. This needs to be done before all
const originalSetTimeout = window.setTimeout;

import { Context } from '@opentelemetry/context-base';
import { context } from '@opentelemetry/api';
import { isWrapped, LogLevel } from '@opentelemetry/core';
import { XMLHttpRequestPlugin } from '@opentelemetry/plugin-xml-http-request';
Expand All @@ -40,6 +40,21 @@ import {
const FILE_URL =
'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/package.json';

// this is needed until this is merged and released
// https://github.com/open-telemetry/opentelemetry-js/pull/1209
obecny marked this conversation as resolved.
Show resolved Hide resolved
const ZONE_CONTEXT_KEY = 'OT_ZONE_CONTEXT';
function createZone(zoneName: string, context: unknown): Zone {
return Zone.current.fork({
name: zoneName,
properties: {
[ZONE_CONTEXT_KEY]: context,
},
});
}
interface ContextManagerWithPrivate {
_createZone: Function;
}

describe('UserInteractionPlugin', () => {
describe('when zone.js is available', () => {
let contextManager: ZoneContextManager;
Expand All @@ -51,6 +66,12 @@ describe('UserInteractionPlugin', () => {
let requests: sinon.SinonFakeXMLHttpRequest[] = [];
beforeEach(() => {
contextManager = new ZoneContextManager().enable();

// this is needed until this is merged and released
// https://github.com/open-telemetry/opentelemetry-js/pull/1209
const contextManagerWithPrivate = (contextManager as unknown) as ContextManagerWithPrivate;
contextManagerWithPrivate._createZone = createZone;

context.setGlobalContextManager(contextManager);
sandbox = sinon.createSandbox();
history.pushState({ test: 'testing' }, '', `${location.pathname}`);
Expand Down Expand Up @@ -200,6 +221,38 @@ describe('UserInteractionPlugin', () => {
});
});

it('should run task from different zone - angular test', done => {
const context = Context.ROOT_CONTEXT;
const rootZone = Zone.current;
const newZone = createZone('test', context);

const element = createButton();
element.addEventListener('click', () => {
assert.ok(
Zone.current !== newZone,
'Current zone for 2nd listener click is wrong'
);
assert.ok(
Zone.current.parent === rootZone,
'Parent Zone for 2nd listener click is wrong'
);
});

newZone.run(() => {
assert.ok(Zone.current === newZone, 'New zone is wrong');
fakeInteraction(() => {
assert.ok(
Zone.current.parent === newZone,
'Parent zone for click is wrong'
);
const spanClick: tracing.ReadableSpan = exportSpy.args[0][0][0];
assertClickSpan(spanClick);

done();
}, element);
});
});

it('should ignore interaction when element is disabled', done => {
const btn = createButton(true);
let called = false;
Expand Down