Skip to content

Commit

Permalink
chore: update and fix tracer-web examples (#2661)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSNev authored Dec 15, 2021
1 parent f59c5b2 commit 10cd916
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 32 deletions.
5 changes: 3 additions & 2 deletions examples/tracer-web/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"env": {
"node": true
"browser": true
},
"extends": "airbnb-base",
"parserOptions": {
Expand All @@ -11,6 +11,7 @@
"no-use-before-define": ["error", "nofunc"],
"no-console": "off",
"import/no-unresolved": "off",
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"import/no-extraneous-dependencies": [ "error", { "devDependencies": true } ]
}
}
78 changes: 78 additions & 0 deletions examples/tracer-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,97 @@ 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
11 changes: 7 additions & 4 deletions examples/tracer-web/examples/fetch/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'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';
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({
Expand All @@ -34,7 +37,7 @@ const webTracerWithZone = provider.getTracer('example-tracer-web');
const getData = (url) => fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
Expand All @@ -46,7 +49,7 @@ const prepareClickEvent = () => {
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 Down
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>
93 changes: 93 additions & 0 deletions examples/tracer-web/examples/fetchXhr/index.js
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);
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>
95 changes: 95 additions & 0 deletions examples/tracer-web/examples/fetchXhrB3/index.js
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);
Loading

0 comments on commit 10cd916

Please sign in to comment.