55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * gRPC ://www.apache.org/licenses/LICENSE-2.0
8+ * https ://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1515 */
1616
1717import React from 'react' ;
18+ import { tracing } from '@opencensus/web-core' ;
1819
1920class App extends React . Component {
2021
@@ -29,21 +30,34 @@ class App extends React.Component {
2930 handleClick ( ) {
3031 // Use promises to test behavior on MicroTasks.
3132 const promise = new Promise ( resolve => {
33+ // Start a child span for the setTimeout as this is the operation we want
34+ // to measure.
35+ // This span will be child of the current root span related to the
36+ // current user interaction. Additionally, these spans should be created
37+ // in the code the click handler will run.
38+ const setTimeoutCustomSpan = tracing . tracer . startChildSpan ( { name : 'setTimeout custom span' } ) ;
3239 setTimeout ( function ( ) {
3340 resolve ( ) ;
41+ // End the span as the setTimeout has finished running the callback.
42+ setTimeoutCustomSpan . end ( ) ;
3443 } , 1000 ) ;
3544 } ) ;
3645
3746 promise . then ( ( ) => {
38- console . log ( "Resolving promise" ) ;
3947 this . callSleepApi ( ) ;
4048 } ) ;
4149 }
4250
4351 callSleepApi ( ) {
4452 const xhr = new XMLHttpRequest ( ) ;
53+ // Create a child span for the XHR. It is possible to create your own spans
54+ // even if the involved task or operation already generates an automatic
55+ // span. In this case, automatic spans are generated for XHRs.
56+ const callSleepApiCustomSpan = tracing . tracer . startChildSpan ( { name : 'Call Sleep API' } ) ;
4557 xhr . onreadystatechange = ( ) => {
4658 if ( xhr . readyState === XMLHttpRequest . DONE ) {
59+ // End the XHR span once it is DONE.
60+ callSleepApiCustomSpan . end ( ) ;
4761 this . callPrimeNumbersApi ( ) ;
4862 }
4963 } ;
@@ -66,10 +80,11 @@ class App extends React.Component {
6680 }
6781
6882 callCalculatePi ( ) {
83+ // Start span for synchronous code.
84+ const calculatePiCustomSpan = tracing . tracer . startChildSpan ( { name : 'Calculate PI' } ) ;
6985 const time = Date . now ( ) ;
70- console . log ( "Calculating PI" ) ;
7186 const pi = this . calculatePi ( ) ;
72- console . log ( "Finished calculating PI" ) ;
87+ calculatePiCustomSpan . end ( ) ;
7388 return { time : ( Date . now ( ) - time ) , value : pi } ;
7489 }
7590
0 commit comments