Skip to content

Commit f0c0626

Browse files
authored
chore: clean up span naming (#162)
* chore: clean up span naming * chore: _ name prefix for allowEventType * chore: remove dblclick from traced events
1 parent 00ea26e commit f0c0626

3 files changed

Lines changed: 25 additions & 17 deletions

File tree

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import { AttributeNames } from './enums/AttributeNames';
3434
import { VERSION } from './version';
3535

3636
const ZONE_CONTEXT_KEY = 'OT_ZONE_CONTEXT';
37-
const EVENT_CLICK_NAME = 'event_click:';
3837
const EVENT_NAVIGATION_NAME = 'Navigation:';
3938

4039
/**
@@ -80,6 +79,12 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
8079
}
8180
}
8281

82+
/**
83+
* Controls whether or not to create a span, based on the event type.
84+
*/
85+
protected _allowEventType(eventType: string): boolean {
86+
return eventType === 'click';
87+
}
8388
/**
8489
* Creates a new span
8590
* @param element
@@ -95,9 +100,12 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
95100
if (element.hasAttribute('disabled')) {
96101
return undefined;
97102
}
103+
if (!this._allowEventType(eventName)) {
104+
return undefined;
105+
}
98106
const xpath = getElementXPath(element, true);
99107
try {
100-
const span = this._tracer.startSpan(`${EVENT_CLICK_NAME} ${xpath}`, {
108+
const span = this._tracer.startSpan(eventName, {
101109
attributes: {
102110
[AttributeNames.COMPONENT]: this.component,
103111
[AttributeNames.EVENT_TYPE]: eventName,
@@ -147,17 +155,6 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
147155
return context;
148156
}
149157

150-
/**
151-
* It gets the element that has been clicked when zone tries to run a new task
152-
* @param task
153-
*/
154-
private _getClickedElement(task: AsyncTask): HTMLElement | undefined {
155-
if (task.eventName === 'click') {
156-
return task.target;
157-
}
158-
return undefined;
159-
}
160-
161158
/**
162159
* Increment number of tasks that are run within the same zone.
163160
* This is needed to be able to end span when no more tasks left
@@ -245,7 +242,7 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
245242
if (once) {
246243
plugin.removePatchedListener(this, type, listener);
247244
}
248-
const span = plugin._createSpan(target, 'click');
245+
const span = plugin._createSpan(target, type);
249246
if (span) {
250247
return plugin._tracer.withSpan(span, () => {
251248
const result = listener.apply(target, args);
@@ -405,11 +402,11 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
405402
applyThis?: any,
406403
applyArgs?: any
407404
): Zone {
408-
const target: HTMLElement | undefined = plugin._getClickedElement(task);
405+
const target: HTMLElement | undefined = task.target;
409406
let span: api.Span | undefined;
410407
const activeZone = this;
411408
if (target) {
412-
span = plugin._createSpan(target, 'click');
409+
span = plugin._createSpan(target, task.eventName);
413410
if (span) {
414411
plugin._incrementTask(span);
415412
return activeZone.run(() => {
@@ -568,6 +565,7 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
568565
shimmer.unwrap(ZoneWithPrototype.prototype, 'cancelTask');
569566
} else {
570567
shimmer.unwrap(HTMLElement.prototype, 'addEventListener');
568+
shimmer.unwrap(HTMLElement.prototype, 'removeEventListener');
571569
}
572570
this._unpatchHistoryApi();
573571
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function fakeInteraction(
4646
}
4747

4848
export function assertClickSpan(span: tracing.ReadableSpan, id = 'testBtn') {
49-
assert.equal(span.name, `event_click: //*[@id="${id}"]`);
49+
assert.equal(span.name, 'click');
5050

5151
const attributes = span.attributes;
5252
assert.equal(attributes.component, 'user-interaction');

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ describe('UserInteractionPlugin', () => {
368368
true,
369369
'addEventListener should be wrapped'
370370
);
371+
assert.strictEqual(
372+
isWrapped(HTMLElement.prototype.removeEventListener),
373+
true,
374+
'removeEventListener should be wrapped'
375+
);
371376

372377
assert.strictEqual(
373378
isWrapped(history.replaceState),
@@ -398,6 +403,11 @@ describe('UserInteractionPlugin', () => {
398403
false,
399404
'addEventListener should be unwrapped'
400405
);
406+
assert.strictEqual(
407+
isWrapped(HTMLElement.prototype.removeEventListener),
408+
false,
409+
'removeEventListener should be unwrapped'
410+
);
401411

402412
assert.strictEqual(
403413
isWrapped(history.replaceState),

0 commit comments

Comments
 (0)