0Pricing
React Academy · Lesson

OpenTelemetry Traces from the Browser

Set up the OTel JavaScript SDK in React, create spans for user interactions, and export to a backend collector.

OpenTelemetry Traces from the Browser is a free React Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The OpenTelemetry JavaScript SDK

The OpenTelemetry JavaScript SDK for browsers consists of several packages: @opentelemetry/sdk-trace-web provides the core tracing implementation, @opentelemetry/instrumentation-fetch automatically creates spans for all fetch API calls, and @opentelemetry/exporter-trace-otlp-http sends spans to a collector.

TracerProvider: The Root of OTel Tracing

The TracerProvider is the central configuration object in OpenTelemetry. It holds the list of SpanProcessors (which determine what happens to completed spans), the resource describing your application (service name, version), and any sampling configuration.

BatchSpanProcessor: Efficient Export

The BatchSpanProcessor collects completed spans in memory and exports them in batches rather than sending each span individually. This dramatically reduces the number of HTTP requests to your collector, improving browser performance and reducing load on the OTel collector endpoint.

OTLPTraceExporter: Sending to a Collector

OTLPTraceExporter sends batched spans to an OpenTelemetry-compatible collector via HTTP in JSON or protobuf format. You configure the collector URL (e.g., your OTel Collector proxy endpoint), and the exporter handles serialization and retry logic automatically.

What Is a Span?

A Span represents a single unit of work within a trace: a component mount, an API call, a user interaction. Spans have a name, start time, end time, status (OK or ERROR), and a set of key-value attributes. Related spans are linked by a shared trace ID and parent-child relationships.

Starting and Ending Spans Manually

You create a tracer with provider.getTracer('react-app') and start spans with tracer.startSpan('component-mount'). After the work completes, call span.end() to record the end time. Always end spans — leaked spans waste memory and produce incomplete data in your observability backend.

Adding Attributes to Spans

Attributes enrich spans with contextual data: span.setAttribute('user.id', userId) or span.setAttribute('route', location.pathname). Attributes are searchable in your observability backend, enabling queries like "show all slow spans for user 42" or "show all errors on the /checkout route."

Automatic Fetch Instrumentation

@opentelemetry/instrumentation-fetch monkey-patches the global fetch function to automatically create spans for every outgoing HTTP request. The span captures the URL, method, HTTP status code, and duration without any changes to your existing fetch calls.

Context Propagation via W3C TraceContext

When the fetch instrumentation creates a span for an outgoing request, it injects the W3C TraceContext traceparent header into the request. The receiving backend reads this header and continues the same trace, creating a connected trace that spans browser and server.

Initializing OTel in Your React App

OTel initialization must happen before any components mount — typically in the app entry file (main.tsx) before the ReactDOM.createRoot call. Create the provider, register processors and exporters, and call provider.register() to make it the global TracerProvider.

Sampling: Controlling Trace Volume

Recording every user interaction in high-traffic apps generates enormous amounts of trace data. Configure a TraceIdRatioBased sampler (e.g., sample 10% of traces) in the TracerProvider to reduce volume. Always sample 100% of error traces using a ParentBased sampler to ensure no errors are missed.

OTel BatchSpanProcessor Purpose

Why is BatchSpanProcessor used instead of a SimpleSpanProcessor in browser applications?

Lesson Recap

The OpenTelemetry JavaScript SDK instruments browsers with TracerProvider, BatchSpanProcessor, and OTLPTraceExporter. Spans represent units of work enriched with attributes. Fetch instrumentation automatically creates spans for API calls and injects W3C TraceContext headers for end-to-end distributed tracing. Initialize early, sample appropriately, and always end spans to maintain clean trace data.

Frequently asked questions

Is the “OpenTelemetry Traces from the Browser” lesson free?

Yes — the full text of “OpenTelemetry Traces from the Browser” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “OpenTelemetry Traces from the Browser”?

Set up the OTel JavaScript SDK in React, create spans for user interactions, and export to a backend collector. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “OpenTelemetry Traces from the Browser” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Frontend Observability: What and Why
  2. OpenTelemetry Traces from the Browser
  3. Real User Monitoring and Web Vitals
  4. Correlating Frontend and Backend Traces
← Back to React Academy