diff --git a/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts b/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts index 0a5ef04153..2f5663f36c 100644 --- a/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts +++ b/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts @@ -98,10 +98,13 @@ export class UserInteractionInstrumentation extends InstrumentationBase * @param eventName */ private _createSpan( - element: HTMLElement, + element: EventTarget | null | undefined, eventName: string, parentSpan?: api.Span | undefined ): api.Span | undefined { + if (!(element instanceof HTMLElement)) { + return undefined; + } if (!element.getAttribute) { return undefined; } @@ -274,10 +277,7 @@ export class UserInteractionInstrumentation extends InstrumentationBase if (once) { plugin.removePatchedListener(this, type, listener); } - const span = - target instanceof HTMLElement - ? plugin._createSpan(target, type, parentSpan) - : undefined; + const span = plugin._createSpan(target, type, parentSpan); if (span) { if (event) { plugin._eventsSpanMap.set(event, span); @@ -449,7 +449,7 @@ export class UserInteractionInstrumentation extends InstrumentationBase const target = event?.target; let span: api.Span | undefined; const activeZone = this; - if (target instanceof HTMLElement) { + if (target) { span = plugin._createSpan(target, task.eventName); if (span) { plugin._incrementTask(span); diff --git a/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.test.ts b/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.test.ts index 3e38df8600..cf83d3045d 100644 --- a/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.test.ts +++ b/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.test.ts @@ -41,6 +41,7 @@ const FILE_URL = describe('UserInteractionInstrumentation', () => { afterEach(() => { + // clear body from elements created by some tests to make sure they are independent while (document.body.lastChild) { document.body.removeChild(document.body.lastChild); }