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

Commit e3050d2

Browse files
committed
Refactoring
1 parent acfaf15 commit e3050d2

1 file changed

Lines changed: 43 additions & 39 deletions

File tree

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

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -61,44 +61,10 @@ export class InteractionTracker {
6161
let taskZone = Zone.current;
6262
if (interceptingElement) {
6363
console.log('Click detected');
64+
6465
if (this.currentEventTracingZone === undefined) {
65-
const traceId = randomTraceId();
66-
const spanOptions = {
67-
name: resolveInteractionName(interceptingElement, task.eventName),
68-
spanContext: {
69-
traceId,
70-
// This becomes the parentSpanId field of the root span, and the actual
71-
// span ID for the root span gets assigned to a random number.
72-
spanId: '',
73-
},
74-
kind: SpanKind.UNSPECIFIED,
75-
};
76-
// Start a new RootSpan for a new user interaction.
77-
tracing.tracer.startRootSpan(spanOptions, root => {
78-
// As startRootSpan creates the zone and Zone.current corresponds to the
79-
// new zone, we have to set the currentEventTracingZone with the Zone.current
80-
// to capture the new zone.
81-
this.currentEventTracingZone = Zone.current;
82-
});
83-
84-
this.interactions[traceId] = startOnPageInteraction({
85-
eventType: task.eventName,
86-
target: task.target,
87-
rootSpan: getRootSpan(this.currentEventTracingZone),
88-
});
89-
// Timeout to reset currentEventTracingZone to allow the creation of a new
90-
// zone for a new user interaction.
91-
Zone.root.run(() =>
92-
setTimeout(
93-
() => (this.currentEventTracingZone = undefined),
94-
RESET_TRACING_ZONE_DELAY
95-
)
96-
);
97-
98-
console.log('New zone:');
99-
console.log(this.currentEventTracingZone);
66+
this.startNewInteraction(interceptingElement, task.eventName);
10067
}
101-
10268
// Change the zone task.
10369
if (this.currentEventTracingZone) {
10470
task._zone = this.currentEventTracingZone;
@@ -162,6 +128,46 @@ export class InteractionTracker {
162128
};
163129
}
164130

131+
private startNewInteraction(
132+
interceptingElement: HTMLElement,
133+
eventName: string
134+
) {
135+
const traceId = randomTraceId();
136+
const spanOptions = {
137+
name: resolveInteractionName(interceptingElement, eventName),
138+
spanContext: {
139+
traceId,
140+
// This becomes the parentSpanId field of the root span, and the actual
141+
// span ID for the root span gets assigned to a random number.
142+
spanId: '',
143+
},
144+
kind: SpanKind.UNSPECIFIED,
145+
};
146+
// Start a new RootSpan for a new user interaction.
147+
tracing.tracer.startRootSpan(spanOptions, root => {
148+
// As startRootSpan creates the zone and Zone.current corresponds to the
149+
// new zone, we have to set the currentEventTracingZone with the Zone.current
150+
// to capture the new zone.
151+
this.currentEventTracingZone = Zone.current;
152+
});
153+
154+
this.interactions[traceId] = startOnPageInteraction({
155+
eventType: eventName,
156+
target: interceptingElement,
157+
rootSpan: getRootSpan(this.currentEventTracingZone),
158+
});
159+
// Timeout to reset currentEventTracingZone to allow the creation of a new
160+
// zone for a new user interaction.
161+
Zone.root.run(() =>
162+
setTimeout(
163+
() => (this.currentEventTracingZone = undefined),
164+
RESET_TRACING_ZONE_DELAY
165+
)
166+
);
167+
console.log('New zone:');
168+
console.log(this.currentEventTracingZone);
169+
}
170+
165171
/** Increments the count of outstanding tasks for a given interaction id. */
166172
private incrementTaskCount(interactionId: string) {
167173
const stopWatch = this.getStopwatch(interactionId);
@@ -277,9 +283,7 @@ function resolveInteractionName(
277283
}
278284
let interactionName = element.getAttribute('data-ocweb-id');
279285
if (!interactionName) {
280-
const elementId = element.getAttribute('id')
281-
? element.getAttribute('id')
282-
: '';
286+
const elementId = element.getAttribute('id') || '';
283287
const tagName = element.tagName;
284288
if (!tagName) return '';
285289
interactionName =

0 commit comments

Comments
 (0)