Setup your JavaScript application to propagate context. This way your OpenTelemetry enabled application is able to "connect" traces between various services.
- Web Trace Provider - Distributed tracing with OTLP HTTP exporter
- Metrics - Application metrics with OTLP HTTP exporter
- Logs - Structured logging with OTLP HTTP exporter
- Document Load Instrumentation - Automatic page load tracing
- Fetch Instrumentation - Optional: Trace HTTP requests with W3C Trace Context propagation
- TypeScript - Full type safety (strict mode)
- Lightweight - Minimal dependencies, tree-shakeable
npm install @makomweb/otel-sdk-reactPeer dependencies (should already be in your project):
{
"react": ">=18.0.0",
"@opentelemetry/api": ">=1.7.0",
"@opentelemetry/sdk-trace-web": ">=1.17.0",
"@opentelemetry/sdk-metrics": ">=1.17.0",
"@opentelemetry/sdk-logs": ">=0.50.0",
"@opentelemetry/exporter-trace-otlp-http": ">=0.43.0",
"@opentelemetry/exporter-metrics-otlp-http": ">=0.43.0",
"@opentelemetry/exporter-logs-otlp-http": ">=0.50.0",
"@opentelemetry/instrumentation": ">=0.43.0",
"@opentelemetry/instrumentation-fetch": ">=0.51.0",
"@opentelemetry/instrumentation-document-load": ">=0.33.0"
}Initialize OTEL SDK with Trace, Metric, and Log providers + Document Load instrumentation.
import { setupOTelSDK } from '@makomweb/otel-sdk-react';
setupOTelSDK('http://localhost:4318');Trace HTTP requests with W3C Trace Context propagation to your backend.
import { setupFetchInstrumentation } from '@makomweb/otel-sdk-react';
setupFetchInstrumentation('http://api.example.com');In your main.tsx or main.jsx:
import React from 'react';
import ReactDOM from 'react-dom/client';
import { setupOTelSDK, setupFetchInstrumentation } from '@makomweb/otel-sdk-react';
import App from './App';
// Initialize OTEL (required, before React renders)
setupOTelSDK('http://localhost:4318');
// Optional: enable fetch tracing
setupFetchInstrumentation('http://localhost:8081');
const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);Create .env.local:
VITE_OTEL_COLLECTOR_ADDRESS=http://localhost:4318
VITE_BACKEND_API_URL=http://localhost:8081Then in main.tsx:
setupOTelSDK(import.meta.env.VITE_OTEL_COLLECTOR_ADDRESS);
setupFetchInstrumentation(import.meta.env.VITE_BACKEND_API_URL);For larger applications, organize configuration separately:
// config.ts
export function getConfig() {
return {
otelCollectorAddress: import.meta.env.VITE_OTEL_COLLECTOR_ADDRESS || 'http://localhost:4318',
apiUrl: import.meta.env.VITE_BACKEND_API_URL || 'http://localhost:8081',
};
}// main.tsx
import { setupOTelSDK, setupFetchInstrumentation } from '@makomweb/otel-sdk-react';
import { getConfig } from './config';
const config = getConfig();
setupOTelSDK(config.otelCollectorAddress);
setupFetchInstrumentation(config.apiUrl);- Verify setupOTelSDK() is called before React renders (must be first in main.tsx)
- Check collector is running:
curl http://localhost:4318/v1/traces -X POST - Check network in DevTools: Look for requests to
/v1/tracesendpoint
- Verify setupFetchInstrumentation(backendUrl) is called
- Check backendUrl matches your API domain
- Check CORS headers: Collector must accept cross-origin requests
See CONTRIBUTING.md