-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update and fix tracer-web examples (#2661)
- Loading branch information
Showing
12 changed files
with
425 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Fetch Plugin Example</title> | ||
<base href="/"> | ||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
|
||
<body> | ||
Example of using Web Tracer with Fetch and XMLHttpRequest plugins with console exporter and collector exporter without the B3 Propagator | ||
<script type="text/javascript" src="fetchXhr.js"></script> | ||
<br/> | ||
<button id="button1">Fetch Test</button> | ||
<button id="button2">Xhr Test</button> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { context, trace } from '@opentelemetry/api'; | ||
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'; | ||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; | ||
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web'; | ||
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch'; | ||
import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request'; | ||
import { ZoneContextManager } from '@opentelemetry/context-zone'; | ||
import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
|
||
const provider = new WebTracerProvider(); | ||
|
||
// Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests | ||
// to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the | ||
// exporter without delay | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); | ||
provider.register({ | ||
contextManager: new ZoneContextManager(), | ||
}); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [ | ||
new FetchInstrumentation({ | ||
ignoreUrls: [/localhost:8090\/sockjs-node/], | ||
propagateTraceHeaderCorsUrls: [ | ||
'https://cors-test.appspot.com/test', | ||
'https://httpbin.org/get', | ||
], | ||
clearTimingResources: true, | ||
}), | ||
new XMLHttpRequestInstrumentation({ | ||
ignoreUrls: [/localhost:8090\/sockjs-node/], | ||
propagateTraceHeaderCorsUrls: [ | ||
'https://httpbin.org/get', | ||
], | ||
}), | ||
], | ||
}); | ||
|
||
const webTracerWithZone = provider.getTracer('example-tracer-web'); | ||
|
||
const getData = (url) => fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
const getDataXhr = (url) => new Promise((resolve, reject) => { | ||
const req = new XMLHttpRequest(); | ||
req.open('GET', url, true); | ||
req.setRequestHeader('Content-Type', 'application/json'); | ||
req.setRequestHeader('Accept', 'application/json'); | ||
req.onload = () => { | ||
resolve(); | ||
}; | ||
req.onerror = () => { | ||
reject(); | ||
}; | ||
req.send(); | ||
}); | ||
|
||
// example of keeping track of context between async operations | ||
const prepareClickEvent = () => { | ||
const url = 'https://httpbin.org/get'; | ||
|
||
const element1 = document.getElementById('button1'); | ||
const element2 = document.getElementById('button2'); | ||
|
||
const clickHandler = (fetchFn) => () => { | ||
const singleSpan = webTracerWithZone.startSpan('files-series-info'); | ||
context.with(trace.setSpan(context.active(), singleSpan), () => { | ||
fetchFn(url).then((_data) => { | ||
trace.getSpan(context.active()).addEvent('fetching-single-span-completed'); | ||
singleSpan.end(); | ||
}); | ||
}); | ||
for (let i = 0, j = 5; i < j; i += 1) { | ||
const span = webTracerWithZone.startSpan(`files-series-info-${i}`); | ||
context.with(trace.setSpan(context.active(), span), () => { | ||
fetchFn(url).then((_data) => { | ||
trace.getSpan(context.active()).addEvent(`fetching-span-${i}-completed`); | ||
span.end(); | ||
}); | ||
}); | ||
} | ||
}; | ||
element1.addEventListener('click', clickHandler(getData)); | ||
element2.addEventListener('click', clickHandler(getDataXhr)); | ||
}; | ||
|
||
window.addEventListener('load', prepareClickEvent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Fetch Plugin Example</title> | ||
<base href="/"> | ||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
|
||
<body> | ||
Example of using Web Tracer with Fetch and XMLHttpRequest plugins with console exporter and collector exporter with B3 Propagator | ||
<script type="text/javascript" src="fetchXhr.js"></script> | ||
<br/> | ||
<button id="button1">Fetch Test</button> | ||
<button id="button2">Xhr Test</button> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { context, trace } from '@opentelemetry/api'; | ||
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'; | ||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; | ||
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web'; | ||
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch'; | ||
import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request'; | ||
import { ZoneContextManager } from '@opentelemetry/context-zone'; | ||
import { B3Propagator } from '@opentelemetry/propagator-b3'; | ||
import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
|
||
const provider = new WebTracerProvider(); | ||
|
||
// Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests | ||
// to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the | ||
// exporter without delay | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); | ||
provider.register({ | ||
contextManager: new ZoneContextManager(), | ||
propagator: new B3Propagator(), | ||
}); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [ | ||
new FetchInstrumentation({ | ||
ignoreUrls: [/localhost:8090\/sockjs-node/], | ||
propagateTraceHeaderCorsUrls: [ | ||
'https://cors-test.appspot.com/test', | ||
'https://httpbin.org/get', | ||
], | ||
clearTimingResources: true, | ||
}), | ||
new XMLHttpRequestInstrumentation({ | ||
ignoreUrls: [/localhost:8090\/sockjs-node/], | ||
propagateTraceHeaderCorsUrls: [ | ||
'https://httpbin.org/get', | ||
], | ||
}), | ||
], | ||
}); | ||
|
||
const webTracerWithZone = provider.getTracer('example-tracer-web'); | ||
|
||
const getData = (url) => fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
const getDataXhr = (url) => new Promise((resolve, reject) => { | ||
const req = new XMLHttpRequest(); | ||
req.open('GET', url, true); | ||
req.setRequestHeader('Content-Type', 'application/json'); | ||
req.setRequestHeader('Accept', 'application/json'); | ||
req.onload = () => { | ||
resolve(); | ||
}; | ||
req.onerror = () => { | ||
reject(); | ||
}; | ||
req.send(); | ||
}); | ||
|
||
// example of keeping track of context between async operations | ||
const prepareClickEvent = () => { | ||
const url = 'https://httpbin.org/get'; | ||
|
||
const element1 = document.getElementById('button1'); | ||
const element2 = document.getElementById('button2'); | ||
|
||
const clickHandler = (fetchFn) => () => { | ||
const singleSpan = webTracerWithZone.startSpan('files-series-info'); | ||
context.with(trace.setSpan(context.active(), singleSpan), () => { | ||
fetchFn(url).then((_data) => { | ||
trace.getSpan(context.active()).addEvent('fetching-single-span-completed'); | ||
singleSpan.end(); | ||
}); | ||
}); | ||
for (let i = 0, j = 5; i < j; i += 1) { | ||
const span = webTracerWithZone.startSpan(`files-series-info-${i}`); | ||
context.with(trace.setSpan(context.active(), span), () => { | ||
fetchFn(url).then((_data) => { | ||
trace.getSpan(context.active()).addEvent(`fetching-span-${i}-completed`); | ||
span.end(); | ||
}); | ||
}); | ||
} | ||
}; | ||
element1.addEventListener('click', clickHandler(getData)); | ||
element2.addEventListener('click', clickHandler(getDataXhr)); | ||
}; | ||
|
||
window.addEventListener('load', prepareClickEvent); |
Oops, something went wrong.