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

Commit 816fd22

Browse files
crdgonzalezcadraffensperger
authored andcommitted
Create a new zone when click is detected (#87)
1 parent 9f49da0 commit 816fd22

File tree

6 files changed

+94
-77
lines changed

6 files changed

+94
-77
lines changed

examples/user_interaction/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
"last 1 safari version"
4040
]
4141
}
42-
}
42+
}

examples/user_interaction/client/src/App.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,50 @@ class App extends React.Component {
2222
super(props);
2323
this.state = { pi: { time: 0, value: "unknown" }, prime_numbers: { time: 0, value: [] } };
2424
this.handleClick = this.handleClick.bind(this);
25-
this.runSecondTask = this.runSecondTask.bind(this);
26-
this.http = require('http');
25+
this.runSecondTask = this.callPrimeNumbersApi.bind(this);
26+
this.host = 'http://localhost:8088';
2727
}
2828

2929
handleClick() {
30-
const host = 'localhost';
31-
const port = 8088;
32-
this.http.get({
33-
host,
34-
port,
35-
path: '/sleep'
36-
}, (response) => {
37-
let data = [];
38-
response.on('data', chunk => data.push(chunk));
39-
response.on('end', this.runSecondTask);
40-
});
30+
console.log("Entering handle click.");
31+
32+
this.callSleepApi();
33+
}
34+
35+
callSleepApi() {
36+
const xhr = new XMLHttpRequest();
37+
xhr.onreadystatechange = () => {
38+
if (xhr.readyState === XMLHttpRequest.DONE) {
39+
this.callPrimeNumbersApi();
40+
}
41+
};
42+
xhr.open('GET', this.host + "/sleep");
43+
xhr.send();
4144
}
4245

43-
runSecondTask() {
44-
const host = 'localhost';
45-
const port = 8088;
46-
this.http.get({
47-
host,
48-
port,
49-
path: '/prime_numbers'
50-
}, (response) => {
51-
let data = [];
52-
response.on('data', chunk => data.push(chunk));
53-
response.on('end', () => {
54-
const result = this.runThridTask();
55-
this.setState({ pi: result, prime_numbers: JSON.parse(data.toString()) });
56-
});
57-
});
46+
callPrimeNumbersApi() {
47+
const xhr = new XMLHttpRequest();
48+
xhr.onreadystatechange = () => {
49+
if (xhr.readyState === XMLHttpRequest.DONE) {
50+
const data = JSON.parse(xhr.responseText)
51+
const result = this.callCalculatePi();
52+
this.setState({ pi: result, prime_numbers: data });
53+
}
54+
};
55+
56+
xhr.open('GET', this.host + "/prime_numbers");
57+
xhr.send();
5858
}
5959

60-
runThridTask() {
60+
callCalculatePi() {
6161
const time = Date.now();
62+
console.log("Calculating PI");
6263
const pi = this.calculatePi();
64+
console.log("Finished calculating PI");
6365
return { time: (Date.now() - time), value: pi };
6466
}
6567

66-
// Calculates Pi using Gregory-Leibniz series, just to make the process longer.
68+
// Calculates PI using Gregory-Leibniz series, just to make the process longer.
6769
calculatePi() {
6870
let result = 0.0;
6971
let divisor = 1.0;

examples/user_interaction/server/package-lock.json

Lines changed: 0 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/user_interaction/server/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"@opencensus/exporter-zipkin": "^0.0.12",
2525
"@opencensus/nodejs": "^0.0.12",
2626
"@opencensus/propagation-tracecontext": "^0.0.12",
27-
"axios": "^0.19.0",
2827
"http": "*",
2928
"sleep": "^6.1.0"
3029
},

examples/user_interaction/server/server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,17 @@ function handleRequest(request, response) {
7979
let result = '';
8080
let code = 200;
8181
if (url.parse(request.url).pathname === '/sleep') {
82+
console.log("Sleeping...")
8283
const time = Date.now();
83-
sleep.sleep(2);
84+
sleep.sleep(5);
8485
result = { time: Date.now() - time, value: "" };
86+
console.log("Finished.")
8587
} else if (url.parse(request.url).pathname === '/prime_numbers') {
88+
console.log("Calculate prime numbers...")
8689
const time = Date.now();
8790
const prime_numbers = JSON.stringify(calculatePrimeNumbers());
8891
result = { time: Date.now() - time, value: prime_numbers };
92+
console.log("Finished.")
8993
} else {
9094
result = { time: 0, value: "unknown url" };
9195
code = 404;

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

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { randomTraceId } from '@opencensus/web-core';
18+
1719
// Allows us to monkey patch Zone prototype without TS compiler errors.
1820
declare const Zone: ZoneType & { prototype: Zone };
1921

@@ -26,42 +28,89 @@ export type AsyncTask = Task & {
2628
data: AsyncTaskData;
2729
eventName: string;
2830
target: HTMLElement;
31+
// Allows access to the private `_zone` property of a Zone.js Task.
32+
_zone: Zone;
2933
};
3034

3135
export class InteractionTracker {
3236
constructor() {
37+
3338
const runTask = Zone.prototype.runTask;
3439
Zone.prototype.runTask = function(
3540
task: AsyncTask,
3641
applyThis: unknown,
3742
applyArgs: unknown
3843
) {
39-
if (task.eventName && task.eventName.toString().indexOf('click') !== -1) {
40-
console.log('Running task');
44+
const time = Date.now();
45+
46+
console.warn('Running task');
47+
console.log(task.zone);
48+
49+
let taskZone = this;
50+
if (isTrackedElement(task)) {
4151
console.log('Click detected');
52+
53+
const traceId = randomTraceId();
54+
const tracingZone = Zone.root.fork({
55+
name: traceId,
56+
properties: {
57+
isTracingZone: true,
58+
traceId,
59+
},
60+
});
61+
62+
// Change the zone task.
63+
task._zone = tracingZone;
64+
taskZone = tracingZone;
65+
console.log('New zone:');
66+
console.log(taskZone);
67+
} else {
68+
// If we already are in a tracing zone, just run the task in our tracing zone.
69+
if (task.zone && task.zone.get('isTracingZone')) {
70+
taskZone = task.zone;
71+
}
4272
}
4373
try {
44-
return runTask.call(this as {}, task, applyThis, applyArgs);
74+
return runTask.call(taskZone as {}, task, applyThis, applyArgs);
4575
} finally {
76+
console.log('Run task finished.');
77+
console.log('Time to complete: ' + (Date.now() - time));
4678
}
4779
};
4880

4981
const scheduleTask = Zone.prototype.scheduleTask;
5082
Zone.prototype.scheduleTask = function<T extends Task>(task: T): T {
51-
console.log('Scheduling task');
83+
console.warn('Scheduling task');
84+
console.log(task);
85+
86+
let taskZone: Zone = this;
87+
if (task.zone && task.zone && task.zone.get('isTracingZone')) {
88+
taskZone = task.zone;
89+
}
5290
try {
53-
return scheduleTask.call(this as {}, task) as T;
91+
return scheduleTask.call(taskZone as {}, task) as T;
5492
} finally {
5593
}
5694
};
5795

5896
const cancelTask = Zone.prototype.cancelTask;
5997
Zone.prototype.cancelTask = function(task: AsyncTask) {
60-
console.log('cancel task');
98+
console.warn('Cancel task');
99+
console.log(task);
100+
101+
let taskZone: Zone = this;
102+
if (task.zone && task.zone.get('isTracingZone')) {
103+
taskZone = task.zone;
104+
}
105+
61106
try {
62-
return cancelTask.call(this as {}, task);
107+
return cancelTask.call(taskZone as {}, task);
63108
} finally {
64109
}
65110
};
66111
}
67112
}
113+
114+
function isTrackedElement(task: AsyncTask): boolean {
115+
return !!(task.eventName && task.eventName === 'click');
116+
}

0 commit comments

Comments
 (0)