Instrument a React App
Here’s how you can instrument a front-end React application to generate traces, metrics, and logs, and send them to KloudMate using OpenTelemetry.
Step 1: Set Up the Example React Application
Section titled “Step 1: Set Up the Example React Application”For this tutorial, we’ll be working with a simple single-page application (SPA) built using React and Vite. While the instrumentation approach shown here uses React, the same principles apply to other front-end frameworks.
We’ll use Vite to scaffold the project and npm (version 5.2+ or higher) as the package manager.
Step 2: Set up a React App with Vite
Section titled “Step 2: Set up a React App with Vite”We’ll create a simple React + TypeScript application using Vite.
- Select
reactas the framework. - Select
typescriptas the variant.
Then run:
Step 3: Install the required OpenTelemetry packages:
Section titled “Step 3: Install the required OpenTelemetry packages:”Step 4: Instrumentation
Section titled “Step 4: Instrumentation”1. Create a file named tracer.ts inside the src folder and add the following code to it:
2. Replace Placeholders
- Replace “YOUR_PUBLIC_KEY” with your actual KloudMate public key.
- Enter your Backend URL in the form of a regex in place of
propagateTraceHeaderCorsUrlsto both fetch and XMLHttpRequest configuration
Step 5: Import the Tracer
Section titled “Step 5: Import the Tracer”In your main.tsx or index.tsx, import the tracer config before your app renders:
Step 6: Run the app
Section titled “Step 6: Run the app”Once the app is running, traces will be sent to KloudMate.