npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-otel

v0.1.2-beta

Published

Flexible OpenTelemetry integration for React web apps

Downloads

8

Readme

react-otel

Flexible OpenTelemetry integration for React web applications.


Installation

npm install react-otel

SDK Initialization

You should initialize OpenTelemetry in your app's entry point (App.tsx):

// App.tsx
import React, { useEffect } from "react";
import { initOtel } from "react-otel";

initOtel({
  resourcesAttributes: {
    name: "my-react-app",
    version: "1.0.0",
  },
  traces: {
    url: "http://localhost:4318/v1/traces",
  },
  metrics: {
    url: "http://localhost:4318/v1/metrics",
  },
  logs: {
    url: "http://localhost:4318/v1/logs",
  },
});

function App() {
  return (
    <div>
      <h1>My React App</h1>
      {/* Your app content */}
    </div>
  );
}

export default App;

Note:

  • Only call initOtel once, early (ideally in your root component or just before render).
  • This sets up providers for traces, metrics, and logs, and registers instrumentations.

Usage: Meter, Tracer, Logger

You can access the telemetry primitives anywhere in your app:

// telemetry.ts
import { getMeter, getTracer, getLogger } from "react-otel";

export const meter = getMeter("my-react-app", "1.0.0");
export const tracer = getTracer("my-react-app", "1.0.0");
export const logger = getLogger("my-react-app", "1.0.0");

Example Component: Action Button with OTEL Tracking

Here’s a small React component that tracks metrics, traces, and logs on button click, with custom tags:

import React from "react";
import { meter, tracer, logger } from "./telemetry"; // import from the above setup

const actionCounter = meter.createCounter("user_action_clicks_total", {
  description: "Total user action button clicks",
});
const responseTimeHistogram = meter.createHistogram(
  "user_action_response_time",
  {
    description: "Response time for user actions in ms",
  }
);

function ActionButton() {
  const handleClick = async () => {
    // Start a trace span
    const span = tracer.startSpan("user-action", {
      attributes: {
        "user.id": "user-123",
        "action.type": "purchase",
        "ui.button": "action-btn",
      },
    });

    // Add a metric count (with custom tags)
    actionCounter.add(1, {
      "action.type": "purchase",
      environment: "test",
    });

    // Simulate some work and record response time
    const start = performance.now();
    await new Promise((res) => setTimeout(res, Math.random() * 250 + 50));
    const duration = performance.now() - start;
    responseTimeHistogram.record(duration, {
      "action.type": "purchase",
      status: "success",
    });

    // Emit a log record (with custom attributes)
    logger.emit({
      severityText: "INFO",
      body: "User performed purchase action",
      attributes: {
        "user.id": "user-123",
        "action.type": "purchase",
        "ui.button": "action-btn",
        responseTimeMs: Math.round(duration),
      },
    });

    span.end();
    // Optional: show feedback to user
    alert(`Action tracked! Response time: ${Math.round(duration)}ms`);
  };

  return <button onClick={handleClick}>Perform Purchase Action</button>;
}

export default ActionButton;

What this does:

  • Trace: Starts and ends a span with custom tags for the action.
  • Metrics: Increments a counter and records a histogram for response time, with useful tags.
  • Logs: Emits a log entry with detailed metadata.

Add <ActionButton /> anywhere in your app to test end-to-end OTEL telemetry!


Config Options

| Option | Description | Example Value | | --------------------- | -------------------------- | --------------------------------------- | | resourcesAttributes | Service name/version | { name: "my-app", version: "1.0.0" } | | traces.url | Trace collector endpoint | "http://localhost:4318/v1/traces" | | metrics.url | Metrics collector endpoint | "http://localhost:4318/v1/metrics" | | logs.url | Logs collector endpoint | "http://localhost:4318/v1/logs" | | headers | HTTP headers for exporters | { "Content-Type": "application/json"} |


Advanced Usage (for Beta Testing)

You can pass extra config for batch processors, exporters, and override instrumentations via initOtel options:

initOtel({
  resourcesAttributes: { name: "advanced-app", version: "1.0.0" },
  traces: {
    url: "http://localhost:4318/v1/traces",
    batchTraceProcessorConfig: { maxQueueSize: 200 },
  },
  metrics: {
    url: "http://localhost:4318/v1/metrics",
    meterReaderConfig: { exportIntervalMillis: 10000 },
  },
  logs: {
    url: "http://localhost:4318/v1/logs",
    batchLogProcessorConfig: { scheduledDelayMillis: 1000 },
  },
  instrumentations: { eventNames: ["click", "submit"] },
});

License

MIT


Feedback & Contributions