1+ /*
2+ * Copyright The OpenTelemetry Authors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ 'use strict' ;
18+
119import { context , trace } from '@opentelemetry/api' ;
2- import { ConsoleSpanExporter , SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base' ;
20+ import {
21+ ConsoleSpanExporter ,
22+ SimpleSpanProcessor ,
23+ } from '@opentelemetry/sdk-trace-base' ;
324import { WebTracerProvider } from '@opentelemetry/sdk-trace-web' ;
425import { DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load' ;
526import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request' ;
627import { ZoneContextManager } from '@opentelemetry/context-zone' ;
728import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http' ;
829import { B3Propagator } from '@opentelemetry/propagator-b3' ;
9- import { CompositePropagator , W3CTraceContextPropagator } from '@opentelemetry/core' ;
30+ import {
31+ CompositePropagator ,
32+ W3CTraceContextPropagator ,
33+ } from '@opentelemetry/core' ;
1034import { registerInstrumentations } from '@opentelemetry/instrumentation' ;
1135import { Resource } from '@opentelemetry/resources' ;
1236import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions' ;
@@ -24,68 +48,66 @@ const provider = new WebTracerProvider({
2448provider . register ( {
2549 contextManager : new ZoneContextManager ( ) ,
2650 propagator : new CompositePropagator ( {
27- propagators : [
28- new B3Propagator ( ) ,
29- new W3CTraceContextPropagator ( ) ,
30- ] ,
51+ propagators : [ new B3Propagator ( ) , new W3CTraceContextPropagator ( ) ] ,
3152 } ) ,
3253} ) ;
3354registerInstrumentations ( {
3455 instrumentations : [
3556 new DocumentLoadInstrumentation ( ) ,
3657 new XMLHttpRequestInstrumentation ( {
3758 ignoreUrls : [ / l o c a l h o s t / ] ,
38- propagateTraceHeaderCorsUrls : [
39- 'http://localhost:8090' ,
40- ] ,
59+ propagateTraceHeaderCorsUrls : [ 'http://localhost:8090' ] ,
4160 } ) ,
4261 ] ,
4362 tracerProvider : provider ,
4463} ) ;
4564
4665const tracer = provider . getTracer ( 'example-document-load' ) ;
4766
48- const getData = ( url ) => new Promise ( ( resolve , reject ) => {
49- // eslint-disable-next-line no-undef
50- const req = new XMLHttpRequest ( ) ;
51- req . open ( 'GET' , url , true ) ;
52- req . send ( ) ;
53- req . onload = ( ) => {
54- let json ;
55- try {
56- json = JSON . parse ( req . responseText ) ;
57- } catch ( e ) {
58- reject ( e ) ;
59- }
60- resolve ( json ) ;
61- } ;
62- } ) ;
67+ const getData = url =>
68+ new Promise ( ( resolve , reject ) => {
69+ // eslint-disable-next-line no-undef
70+ const req = new XMLHttpRequest ( ) ;
71+ req . open ( 'GET' , url , true ) ;
72+ req . send ( ) ;
73+ req . onload = ( ) => {
74+ let json ;
75+ try {
76+ json = JSON . parse ( req . responseText ) ;
77+ } catch ( e ) {
78+ reject ( e ) ;
79+ }
80+ resolve ( json ) ;
81+ } ;
82+ } ) ;
6383
6484// example of keeping track of context between async operations
6585const prepareClickEvent = ( ) => {
66- const url1 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json' ;
67- const url2 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/packages/opentelemetry-sdk-trace-web/package.json' ;
86+ const url1 =
87+ 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json' ;
88+ const url2 =
89+ 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/packages/opentelemetry-sdk-trace-web/package.json' ;
6890
6991 const element = document . getElementById ( 'button1' ) ;
7092
7193 const onClick = ( ) => {
7294 let count = 0 ;
95+ const mainSpan = tracer . startSpan ( 'click button' ) ;
7396
7497 function finish ( ) {
75- count ++ ;
98+ count += 1 ;
7699 if ( count === 2 ) {
77100 mainSpan . end ( ) ;
78101 }
79102 }
80103
81- const mainSpan = tracer . startSpan ( 'click button' ) ;
82104 context . with ( trace . setSpan ( context . active ( ) , mainSpan ) , ( ) => {
83105 const span1 = tracer . startSpan ( 'files-series-info-1' ) ;
84106
85107 const span2 = tracer . startSpan ( 'files-series-info-2' ) ;
86108
87109 context . with ( trace . setSpan ( context . active ( ) , span1 ) , ( ) => {
88- getData ( url1 ) . then ( ( data ) => {
110+ getData ( url1 ) . then ( data => {
89111 const curSpan = trace . getSpan ( context . active ( ) ) ;
90112 console . log ( 'current span is span1' , curSpan === span1 ) ;
91113 console . log ( 'info from package.json' , data . description , data . version ) ;
@@ -96,11 +118,15 @@ const prepareClickEvent = () => {
96118 } ) ;
97119
98120 context . with ( trace . setSpan ( context . active ( ) , span2 ) , ( ) => {
99- getData ( url2 ) . then ( ( data ) => {
121+ getData ( url2 ) . then ( data => {
100122 setTimeout ( ( ) => {
101123 const curSpan = trace . getSpan ( context . active ( ) ) ;
102124 console . log ( 'current span is span2' , curSpan === span2 ) ;
103- console . log ( 'info from package.json' , data . description , data . version ) ;
125+ console . log (
126+ 'info from package.json' ,
127+ data . description ,
128+ data . version
129+ ) ;
104130 curSpan . addEvent ( 'fetching-span2-completed' ) ;
105131 span2 . end ( ) ;
106132 finish ( ) ;
0 commit comments