Skip to content

Commit

Permalink
chore: update and fix tracer-web examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MSNev committed Dec 9, 2021
1 parent a35f093 commit 774346a
Show file tree
Hide file tree
Showing 12 changed files with 436 additions and 29 deletions.
72 changes: 72 additions & 0 deletions examples/tracer-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,91 @@ npm start

By default, the application will run on port `8090`.

Other options for running the application, this serves the same examples using different source file processing so you can review the different effects on the resulting bundle sizes that are loaded via the browser.

| Command | Description
|---------|------------
| `npm start` (Default) | Serve the raw development bundles compressed via GZip
| `npm run start-nc` | Serve the raw development bundles uncompressed
| `npm run start-prod` | Serve the minified production bundles compressed via GZip
| `npm run start-prodnc` | Serve the minified production bundles uncompressed

The development modes includes source maps via the webpack devtool `eval-source-map` mode which substantially increases the size of the bundles.

## Examples

The examples includes several variants so that you can see how to mix and match individual components and the impact this can have on the resulting bundle size.

### XMLHttpRequest

This example shows how to use the XMLHttpRequest Instrumentation with the OTLP Trace exporter and with the B3 Propagator.

Included Components
- XMLHttpRequestInstrumentation
- ZoneContextManager
- OTLPTraceExporter
- WebTracerProvider
- B3Propagator

To see the results, open the browser at <http://localhost:8090/xml-http-request/> and make sure you have the browser console open. The application is using the `ConsoleSpanExporter` and will post the created spans to the browser console.
The screen will look as follows:

![Screenshot of the running example](images/xml-http-request.png)

### Fetch

This example shows how to use the Fetch Instrumentation with the OTLP Trace exporter and with the B3 Propagator.

Included Components
- FetchInstrumentation
- ZoneContextManager
- OTLPTraceExporter
- WebTracerProvider
- B3Propagator

To see the results, open the browser at <http://localhost:8090/fetch/> and make sure you have the browser console open. The application is using the `ConsoleSpanExporter` and will post the created spans to the browser console.

### FetchXhr

This example shows how to use both the XMLHttpRequest and Fetch Instrumentations with the OTLP Trace exporter but without the B3 Propagator.

Included Components
- XMLHttpRequestInstrumentation
- FetchInstrumentation
- ZoneContextManager
- OTLPTraceExporter
- WebTracerProvider

### FetchXhrB3

This example shows how to use both the XMLHttpRequest and Fetch Instrumentations with the OTLP Trace exporter and with the B3 Propagator

Included Components
- XMLHttpRequestInstrumentation
- FetchInstrumentation
- ZoneContextManager
- OTLPTraceExporter
- WebTracerProvider
- B3Propagator

### Metrics

This example shows how to use the OTLP Metric Exporter, it does not include the Trace Exporter. Does not include traces

Included Components
- OTLPMetricExporter
- MeterProvider
- Resource
- SemanticResourceAttributes

### Zipkin

This example show a simple usage of the ZipKin Exporter with the Web Tracer Provider

Included Components
- WebTracerProvider
- ZipkinExporter

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
10 changes: 6 additions & 4 deletions examples/tracer-web/examples/fetch/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
import { context, trace } from '@opentelemetry/api';
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { OTLPTraceExporter } from '@opentelemetry/exporter-otlp-http';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
import { ZoneContextManager } from '@opentelemetry/context-zone';
Expand Down Expand Up @@ -31,10 +30,11 @@ registerInstrumentations({

const webTracerWithZone = provider.getTracer('example-tracer-web');

// eslint-disable-next-line no-undef
const getData = (url) => fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
Expand All @@ -43,10 +43,11 @@ const getData = (url) => fetch(url, {
const prepareClickEvent = () => {
const url = 'https://httpbin.org/get';

// eslint-disable-next-line no-undef
const element = document.getElementById('button1');

const onClick = () => {
const singleSpan = webTracerWithZone.startSpan(`files-series-info`);
const singleSpan = webTracerWithZone.startSpan('files-series-info');
context.with(trace.setSpan(context.active(), singleSpan), () => {
getData(url).then((_data) => {
trace.getSpan(context.active()).addEvent('fetching-single-span-completed');
Expand All @@ -66,4 +67,5 @@ const prepareClickEvent = () => {
element.addEventListener('click', onClick);
};

// eslint-disable-next-line no-undef
window.addEventListener('load', prepareClickEvent);
20 changes: 20 additions & 0 deletions examples/tracer-web/examples/fetchXhr/index.html
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>
100 changes: 100 additions & 0 deletions examples/tracer-web/examples/fetchXhr/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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();
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,
}),
],
});

registerInstrumentations({
instrumentations: [
new XMLHttpRequestInstrumentation({
ignoreUrls: [/localhost:8090\/sockjs-node/],
propagateTraceHeaderCorsUrls: [
'https://httpbin.org/get',
],
}),
],
});

const webTracerWithZone = provider.getTracer('example-tracer-web');

// eslint-disable-next-line no-undef
const getData = (url) => fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});

const getDataXhr = (url) => new Promise((resolve, reject) => {
// eslint-disable-next-line no-undef
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';

// eslint-disable-next-line no-undef
const element1 = document.getElementById('button1');

// eslint-disable-next-line no-undef
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));
};

// eslint-disable-next-line no-undef
window.addEventListener('load', prepareClickEvent);
20 changes: 20 additions & 0 deletions examples/tracer-web/examples/fetchXhrB3/index.html
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>
102 changes: 102 additions & 0 deletions examples/tracer-web/examples/fetchXhrB3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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();
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,
}),
],
});

registerInstrumentations({
instrumentations: [
new XMLHttpRequestInstrumentation({
ignoreUrls: [/localhost:8090\/sockjs-node/],
propagateTraceHeaderCorsUrls: [
'https://httpbin.org/get',
],
}),
],
});

const webTracerWithZone = provider.getTracer('example-tracer-web');

// eslint-disable-next-line no-undef
const getData = (url) => fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});

const getDataXhr = (url) => new Promise((resolve, reject) => {
// eslint-disable-next-line no-undef
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';

// eslint-disable-next-line no-undef
const element1 = document.getElementById('button1');

// eslint-disable-next-line no-undef
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));
};

// eslint-disable-next-line no-undef
window.addEventListener('load', prepareClickEvent);
Loading

0 comments on commit 774346a

Please sign in to comment.