Learn how to add OpenTelemetry to your browser app
You are
viewing the English version of this page because it has not yet been fully
translated. Interested in helping out? See Contributing.
Warning
Client instrumentation for the browser is experimental and mostly
unspecified. If you are interested in helping out, get in touch with the
Client Instrumentation SIG.
While this guide uses the example application presented below, the steps to
instrument your own application should be similar.
Prerequisites
Ensure that you have the following installed locally:
This is a very simple guide, if you’d like to see more complex examples go to
examples/opentelemetry-web.
Copy the following file into an empty directory and call it index.html.
<htmllang="en"><head><metacharset="utf-8"/><title>Document Load Instrumentation Exampletitle><basehref="/"/><metaname="traceparent"content="00-ab42124a3c573678d4d8b21ba52df3bf-d21f7bc17caa5aba-01"/><metaname="viewport"content="width=device-width, initial-scale=1"/>head><body> Example of using Web Tracer with document load instrumentation with console
exporter and collector exporter
body>html>
Installation
To create traces in the browser, you will need @opentelemetry/sdk-trace-web,
and the instrumentation @opentelemetry/instrumentation-document-load:
If you are coding in TypeScript, then run the following command:
tsc --init
Then acquire parcel, which will (among other things)
let you work in TypeScript.
npm install --save-dev parcel
Create an empty code file named document-load with a .ts or .js extension,
as appropriate, based on the language you’ve chosen to write your app in. Add
the following code to your HTML right before the
We will add some code that will trace the document load timings and output those
as OpenTelemetry Spans.
Creating a Tracer Provider
Add the following code to the document-load.ts|js to create a tracer provider,
which brings the instrumentation to trace document load:
/* document-load.ts|js file - the code snippet is the same for both the languages */import{WebTracerProvider}from'@opentelemetry/sdk-trace-web';import{DocumentLoadInstrumentation}from'@opentelemetry/instrumentation-document-load';import{ZoneContextManager}from'@opentelemetry/context-zone';import{registerInstrumentations}from'@opentelemetry/instrumentation';constprovider=newWebTracerProvider();provider.register({// Changing default contextManager to use ZoneContextManager - supports asynchronous operations - optional
contextManager:newZoneContextManager(),});// Registering instrumentations
registerInstrumentations({instrumentations:[newDocumentLoadInstrumentation()],});
Now build the app with parcel:
npx parcel index.html
and open the development web server (e.g. at http://localhost:1234) to see if
your code works.
There will be no output of traces yet, for this we need to add an exporter.
Creating an Exporter
In the following example, we will use the ConsoleSpanExporter which prints all
spans to the console.
In order to visualize and analyze your traces, you will need to export them to a
tracing backend. Follow these instructions for setting up a
backend and exporter.
You may also want to use the BatchSpanProcessor to export spans in batches in
order to more efficiently use resources.
To export traces to the console, modify document-load.ts|js so that it matches
the following code snippet:
/* document-load.ts|js file - the code is the same for both the languages */import{ConsoleSpanExporter,SimpleSpanProcessor,}from'@opentelemetry/sdk-trace-base';import{WebTracerProvider}from'@opentelemetry/sdk-trace-web';import{DocumentLoadInstrumentation}from'@opentelemetry/instrumentation-document-load';import{ZoneContextManager}from'@opentelemetry/context-zone';import{registerInstrumentations}from'@opentelemetry/instrumentation';constprovider=newWebTracerProvider({spanProcessors:[newSimpleSpanProcessor(newConsoleSpanExporter())],});provider.register({// Changing default contextManager to use ZoneContextManager - supports asynchronous operations - optional
contextManager:newZoneContextManager(),});// Registering instrumentations
registerInstrumentations({instrumentations:[newDocumentLoadInstrumentation()],});
Now, rebuild your application and open the browser again. In the console of the
developer toolbar you should see some traces being exported: