We recommend the backend proxy because it keeps your private API key on the server.
1. Get your BugByBug API key
Before configuring OpenTelemetry, create a project in BugByBug and retrieve its API key. You’ll need this key to authenticate telemetry requests. Keep the key somewhere secure. If you’re using the backend proxy, configure it on your server. If you’re exporting directly from the browser, use only a key specifically intended for browser ingestion. Never expose an admin or private server key in your React application.2. Install the OpenTelemetry packages
BugByBug uses OpenTelemetry to receive application telemetry. For this integration, you’ll need the OpenTelemetry API, browser tracing SDK, and OTLP/HTTP trace exporter. Run the following command from your React project directory:3. Choose how to export telemetry
Before initializing OpenTelemetry, decide how your application will send traces to BugByBug. The tracing implementation is the same for both approaches; the main difference is the destination configured for the exporter and where the API key is stored.Option A: Forward telemetry through your backend (recommended)
If your application already has a backend, you can use it as an intermediary between React and BugByBug. The browser sends telemetry to an endpoint on your backend, and your backend forwards the request to BugByBug with the required authentication header. This keeps your private API key out of the frontend bundle. Your backend should provide aPOST /api/traces endpoint that:
- Accepts incoming trace requests from the React application.
- Forwards the request body unchanged to
{OTEL_EXPORTER_OTLP_ENDPOINT}/v1/traces. - Preserves the incoming
Content-Typeheader. - Adds the
x-api-keyauthentication header using your BugByBug API key. - Returns the status code received from BugByBug.
/api to your backend.
In vite.config.ts:
4000 with the port your backend uses.
This proxy configuration is for local development. Your deployed application must also be configured to route /api/traces requests to the backend endpoint.
Option B: Send telemetry directly to BugByBug
If you don’t have a backend proxy, you can configure the browser exporter to send traces directly to BugByBug. This approach requires a browser-ingestion key. Since frontend environment variables are included in the application bundle, the key will be visible to anyone inspecting the application. Never use an admin key or private server key for this setup. Add the correct environment variable to your project. For Vite:4. Create the tracing module
Once you’ve selected an export method, create a file namedsrc/tracing.ts.
This file contains the OpenTelemetry configuration for your React application. It initializes the browser tracer, configures the exporter, and registers listeners for two types of browser errors:
- Uncaught errors, such as JavaScript errors that aren’t handled by your application.
- Unhandled promise rejections, which occur when a rejected promise has no rejection handler.
src/tracing.ts:
- Backend proxy: Use the
/api/tracesURL. The backend handles authentication and forwards the request. - Direct browser export: Use the BugByBug endpoint and provide your browser-ingestion key in the request headers.
5. Initialize tracing in your React application
After creating the tracing module, import and initialize it before rendering your React application. This ensures that the browser error listeners are registered early, so errors occurring after initialization can be captured. Insrc/main.tsx:
initTracing() once at the application entry point rather than from an individual component. This prevents the tracing setup and event listeners from being registered repeatedly as components render.
6. Capture React rendering errors (optional but recommended)
The browser event listeners in the tracing module capture uncaught JavaScript errors and unhandled promise rejections. However, React handles errors caught by its Error Boundaries differently. To capture errors that occur while React components render, you can add an Error Boundary to your application. React Error Boundaries are class components. The following implementation records the error and its component stack through OpenTelemetry. Createsrc/AppErrorBoundary.tsx:
7. Verify the connection
Once you’ve completed the configuration, run your React application and send a test error to check that telemetry reaches BugByBug.- Restart your development server. This is necessary if you’ve changed environment variables.
- Open your React application in a browser.
-
Open the browser developer console and run:
-
Open the browser’s Network tab and look for the outgoing telemetry request:
- Backend proxy: Look for a
POSTrequest to/api/traces. - Direct browser export: Look for a
POSTrequest tohttps://nexus.bugbybug.com/v1/traces.
- Backend proxy: Look for a
- Check the response status and open your BugByBug dashboard to confirm that the test error has arrived.
Troubleshooting
If your test error doesn’t appear in BugByBug, use the browser’s Network tab to check whether the telemetry request was sent and what response it received.Security considerations
Keep the following points in mind when deploying the integration:- Use the backend proxy when possible. It keeps your private API key on the server rather than exposing it in the browser.
- Never expose an admin or private server key. Frontend environment variables are bundled into the application and should not be treated as secrets.
- Treat browser-ingestion keys as public. Use only keys intended for browser ingestion, and rotate them if they are abused.
- Protect your proxy endpoint. Apply request-size limits and rate limiting to reduce unwanted traffic.