@@ -21,6 +21,8 @@ import {
2121 ATTRIBUTE_HTTP_STATUS_CODE ,
2222 ATTRIBUTE_HTTP_METHOD ,
2323 WindowWithOcwGlobals ,
24+ LinkType ,
25+ Link ,
2426} from '@opencensus/web-core' ;
2527import {
2628 InteractionTracker ,
@@ -32,18 +34,27 @@ import {
3234} from '../src/monkey-patching' ;
3335import { spanContextToTraceParent } from '@opencensus/web-propagation-tracecontext' ;
3436import { createFakePerfResourceEntry , spyPerfEntryByType } from './util' ;
35- import { getInitialLoadSpanContext } from '@opencensus/web-initial-load' ;
3637
3738describe ( 'InteractionTracker' , ( ) => {
3839 doPatching ( ) ;
3940 InteractionTracker . startTracking ( ) ;
4041 let onEndSpanSpy : jasmine . Spy ;
4142 const windowWithOcwGlobals = window as WindowWithOcwGlobals ;
42- // Sample 100% of interactions for the testing. Necessary as the sampling
43- // decision is supposed to be done in the initial load page and the
44- // interaction tracker uses the same sampling decision.
45- windowWithOcwGlobals . ocSampleRate = 1.0 ;
46- getInitialLoadSpanContext ( ) ;
43+ // Set the traceparent to fake the initial load Span Context. Also, Sample
44+ // 100% of interactions for the testing. Necessary as this is supposed to be
45+ // done by the initial load page.
46+ const INITIAL_LOAD_TRACE_ID = '0af7651916cd43dd8448eb211c80319c' ;
47+ const INITIAL_LOAD_SPAN_ID = 'b7ad6b7169203331' ;
48+ windowWithOcwGlobals . traceparent = `00-${ INITIAL_LOAD_TRACE_ID } -${ INITIAL_LOAD_SPAN_ID } -01` ;
49+
50+ const EXPECTED_LINKS : Link [ ] = [
51+ {
52+ traceId : INITIAL_LOAD_TRACE_ID ,
53+ spanId : INITIAL_LOAD_SPAN_ID ,
54+ type : LinkType . PARENT_LINKED_SPAN ,
55+ attributes : { } ,
56+ } ,
57+ ] ;
4758
4859 // Use Buffer time as we expect that these interactions take
4960 // a little extra time to complete due to the setTimeout that
@@ -66,10 +77,14 @@ describe('InteractionTracker', () => {
6677 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
6778 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
6879 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
80+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
81+ INITIAL_LOAD_TRACE_ID
82+ ) ;
83+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
6984 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
7085 // As there is another setTimeOut that completes the interaction, the
71- // span duraction is not precise, then only test if the interaction duration
72- // finishes within a range.
86+ // span duraction is not precise, then only test if the interaction
87+ // duration finishes within a range.
7388 expect ( rootSpan . duration ) . toBeLessThan ( TIME_BUFFER ) ;
7489 done ( ) ;
7590 } ) ;
@@ -85,6 +100,10 @@ describe('InteractionTracker', () => {
85100 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
86101 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
87102 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
103+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
104+ INITIAL_LOAD_TRACE_ID
105+ ) ;
106+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
88107 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
89108 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( SET_TIMEOUT_TIME ) ;
90109 expect ( rootSpan . duration ) . toBeLessThanOrEqual (
@@ -105,6 +124,10 @@ describe('InteractionTracker', () => {
105124 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
106125 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
107126 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
127+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
128+ INITIAL_LOAD_TRACE_ID
129+ ) ;
130+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
108131 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
109132 expect ( rootSpan . duration ) . toBeLessThanOrEqual ( TIME_BUFFER ) ;
110133 done ( ) ;
@@ -125,6 +148,10 @@ describe('InteractionTracker', () => {
125148 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
126149 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
127150 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
151+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
152+ INITIAL_LOAD_TRACE_ID
153+ ) ;
154+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
128155 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
129156 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( interactionTime ) ;
130157 //The duration has to be less than set to the canceled timeout.
@@ -152,6 +179,10 @@ describe('InteractionTracker', () => {
152179 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
153180 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
154181 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
182+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
183+ INITIAL_LOAD_TRACE_ID
184+ ) ;
185+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
155186 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
156187 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( SET_TIMEOUT_TIME ) ;
157188 expect ( rootSpan . duration ) . toBeLessThanOrEqual (
@@ -172,6 +203,10 @@ describe('InteractionTracker', () => {
172203 expect ( rootSpan . name ) . toBe ( 'button#test_element click' ) ;
173204 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
174205 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
206+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
207+ INITIAL_LOAD_TRACE_ID
208+ ) ;
209+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
175210 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
176211 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( SET_TIMEOUT_TIME ) ;
177212 expect ( rootSpan . duration ) . toBeLessThanOrEqual (
@@ -193,6 +228,10 @@ describe('InteractionTracker', () => {
193228 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
194229 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
195230 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
231+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
232+ INITIAL_LOAD_TRACE_ID
233+ ) ;
234+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
196235 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
197236 expect ( rootSpan . duration ) . toBeLessThanOrEqual ( TIME_BUFFER ) ;
198237 done ( ) ;
@@ -217,6 +256,10 @@ describe('InteractionTracker', () => {
217256 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
218257 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
219258 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
259+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
260+ INITIAL_LOAD_TRACE_ID
261+ ) ;
262+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
220263 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
221264 // As this click is done at 'RESET_TRACING_ZONE_DELAY - 10' and this click has a
222265 // setTimeout, the minimum time taken by this click is the sum of these values.
@@ -248,6 +291,10 @@ describe('InteractionTracker', () => {
248291 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
249292 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
250293 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
294+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
295+ INITIAL_LOAD_TRACE_ID
296+ ) ;
297+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
251298 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
252299 // Test related to the first interaction
253300 if ( onEndSpanSpy . calls . count ( ) === 1 ) {
@@ -288,6 +335,10 @@ describe('InteractionTracker', () => {
288335 expect ( rootSpan . name ) . toBe ( 'Navigation /test_navigation' ) ;
289336 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
290337 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
338+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
339+ INITIAL_LOAD_TRACE_ID
340+ ) ;
341+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
291342 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
292343 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( SET_TIMEOUT_TIME ) ;
293344 expect ( rootSpan . duration ) . toBeLessThanOrEqual (
@@ -311,6 +362,10 @@ describe('InteractionTracker', () => {
311362 expect ( rootSpan . name ) . toBe ( 'Test navigation' ) ;
312363 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
313364 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
365+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
366+ INITIAL_LOAD_TRACE_ID
367+ ) ;
368+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
314369 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
315370 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( SET_TIMEOUT_TIME ) ;
316371 expect ( rootSpan . duration ) . toBeLessThanOrEqual (
@@ -338,6 +393,10 @@ describe('InteractionTracker', () => {
338393 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
339394 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
340395 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
396+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
397+ INITIAL_LOAD_TRACE_ID
398+ ) ;
399+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
341400 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
342401 expect ( rootSpan . spans . length ) . toBe ( 1 ) ;
343402 const childSpan = rootSpan . spans [ 0 ] ;
@@ -381,6 +440,10 @@ describe('InteractionTracker', () => {
381440 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
382441 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
383442 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
443+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
444+ INITIAL_LOAD_TRACE_ID
445+ ) ;
446+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
384447 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
385448 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( XHR_TIME ) ;
386449 expect ( rootSpan . duration ) . toBeLessThanOrEqual ( XHR_TIME + TIME_BUFFER ) ;
@@ -432,6 +495,10 @@ describe('InteractionTracker', () => {
432495 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
433496 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
434497 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
498+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
499+ INITIAL_LOAD_TRACE_ID
500+ ) ;
501+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
435502 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
436503 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( XHR_TIME ) ;
437504 expect ( rootSpan . duration ) . toBeLessThanOrEqual ( XHR_TIME + TIME_BUFFER ) ;
@@ -509,6 +576,10 @@ describe('InteractionTracker', () => {
509576 expect ( rootSpan . name ) . toBe ( 'test interaction' ) ;
510577 expect ( rootSpan . attributes [ 'EventType' ] ) . toBe ( 'click' ) ;
511578 expect ( rootSpan . attributes [ 'TargetElement' ] ) . toBe ( BUTTON_TAG_NAME ) ;
579+ expect ( rootSpan . attributes [ 'initial_load_trace_id' ] ) . toBe (
580+ INITIAL_LOAD_TRACE_ID
581+ ) ;
582+ expect ( rootSpan . links ) . toEqual ( EXPECTED_LINKS ) ;
512583 expect ( rootSpan . ended ) . toBeTruthy ( ) ;
513584 expect ( rootSpan . duration ) . toBeGreaterThanOrEqual ( interactionTime ) ;
514585 expect ( rootSpan . duration ) . toBeLessThanOrEqual (
0 commit comments