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 🙏

© 2026 – Pkg Stats / Ryan Hefner

sb_opentelemetry

v1.0.3

Published

A simple, batteries-included OpenTelemetry setup package for Node.js applications with automatic instrumentation, logging, and graceful shutdown handling.

Readme

OpenTelemetry Node.js Setup

A simple, batteries-included OpenTelemetry setup package for Node.js applications with automatic instrumentation, logging, and graceful shutdown handling.

Features

  • Easy Setup: One-line initialization with sensible defaults
  • Comprehensive Observability: Traces, metrics, and logs all configured
  • Auto-instrumentation: Automatically instruments popular Node.js libraries
  • Console Patching: Automatically forwards console logs to OpenTelemetry
  • Graceful Shutdown: Handles process termination signals properly
  • Configurable: Customizable export intervals and batch sizes
  • Safe Logging: Handles circular references and large objects gracefully

Installation

npm install sb_opentelemetry

Quick Start

const { setupOpenTelemetry } = require('sb_opentelemetry');

// Basic setup
setupOpenTelemetry('http://localhost:4318', 'my-service');

// Your application code here
console.log('Hello, OpenTelemetry!'); // This will be sent as a log

API Reference

setupOpenTelemetry(url, serviceName, config?)

Initializes OpenTelemetry with the specified configuration.

Parameters

  • url (string, required): The OTLP endpoint URL (e.g., 'http://localhost:4318')
  • serviceName (string, required): The name of your service
  • config (object, optional): Configuration options

Configuration Options

const config = {
  traces: {
    maxQueueSize: 500,           // Maximum queue size for spans
    maxExportBatchSize: 100,     // Maximum batch size for export
    scheduledDelayMillis: 5000,  // Delay between exports
    exportTimeoutMillis: 30000   // Export timeout
  },
  metrics: {
    exportIntervalMillis: 6000   // Metrics export interval
  },
  logs: {
    maxQueueSize: 100,           // Maximum queue size for logs
    maxExportBatchSize: 20,      // Maximum batch size for export
    scheduledDelayMillis: 5000,  // Delay between exports
    exportTimeoutMillis: 30000   // Export timeout
  }
};

setupOpenTelemetry('http://localhost:4318', 'my-service', config);

Return Value

Returns an object with:

  • logger: OpenTelemetry logger instance
  • sdk: OpenTelemetry SDK instance

Returns undefined if setup fails.

Usage Examples

Basic Setup

const { setupOpenTelemetry } = require('sb_opentelemetry');

// Initialize OpenTelemetry
const otel = setupOpenTelemetry('http://localhost:4318', 'user-service');

if (otel) {
  console.log('OpenTelemetry initialized successfully');
} else {
  console.error('Failed to initialize OpenTelemetry');
}

// Your Express app or other code
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  console.log('Handling root request'); // Automatically logged
  res.json({ message: 'Hello World' });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

With Custom Configuration

const { setupOpenTelemetry } = require('your-otel-package-name');

const config = {
  traces: {
    maxQueueSize: 1000,
    scheduledDelayMillis: 3000
  },
  metrics: {
    exportIntervalMillis: 10000
  }
};

setupOpenTelemetry('https://api.honeycomb.io', 'production-service', config);

Console Logging

The package automatically patches console.log, console.error, console.warn, and console.debug to forward logs to OpenTelemetry with appropriate severity levels:

  • console.log → INFO level
  • console.error → ERROR level
  • console.warn → WARN level
  • console.debug → DEBUG level

Large objects are automatically truncated and circular references are handled safely.

Auto-instrumentation

The package automatically instruments popular Node.js libraries including:

  • HTTP/HTTPS
  • Express
  • Fastify
  • MongoDB
  • PostgreSQL
  • MySQL
  • Redis
  • And many more

No additional configuration is required for basic instrumentation.

Graceful Shutdown

The package automatically handles process termination signals (SIGTERM, SIGINT) and ensures that all telemetry data is properly flushed before the process exits.

Error Handling

The setup function includes comprehensive error handling and will:

  • Log setup errors to console
  • Prevent duplicate initialization
  • Continue running even if OpenTelemetry setup fails

Environment Variables

While not required, you can use environment variables for configuration:

OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_SERVICE_NAME=my-service

Troubleshooting

Common Issues

  1. "setupOpenTelemetry() was already called"

    • The setup function can only be called once per process
    • This warning indicates duplicate initialization attempts
  2. No data appearing in collector

    • Verify the OTLP endpoint URL is correct
    • Check network connectivity to the collector
    • Ensure the collector is running and accepting data
  3. Performance impact

    • Adjust batch sizes and export intervals if needed
    • Monitor memory usage with large applications

Requirements

  • Node.js 14 or higher
  • An OpenTelemetry-compatible collector (Jaeger, Zipkin, Honeycomb, etc.)

Dependencies

This package includes all necessary OpenTelemetry dependencies:

  • @opentelemetry/sdk-node
  • @opentelemetry/auto-instrumentations-node
  • @opentelemetry/exporter-trace-otlp-http
  • @opentelemetry/exporter-metrics-otlp-http
  • @opentelemetry/exporter-logs-otlp-http