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

nestjs-observability

v1.0.1

Published

Drop-in observability toolkit for NestJS with correlation IDs, structured logging, Prometheus metrics, and distributed tracing.

Readme

NestJS Observability

A complete, production-ready, drop-in observability toolkit for NestJS applications.

This package centralizes the messy parts of maintaining microservices by providing built-in structured logging, distributed tracing, request correlations, Prometheus metrics, and global error handling all in a single configurable module.

Features Included

  • Centralized Structured Logging (Pino): Highly configurable JSON logging out-of-the-box, with pretty-printing capabilities for local development.
  • Request Correlation IDs: Automatically generates a unique x-correlation-id for every request and injects it into every log line and error trace.
  • Out-Of-The-Box Prometheus Metrics: Automatic tracking of HTTP request durations, request counts, and error counts, exposed via a native /metrics endpoint.
  • Distributed Tracing (OpenTelemetry): Built-in script for initializing Node OpenTelemetry to trace requests across microservice boundaries.
  • Global Error Handling: A centralized GlobalErrorFilter that catches unhandled exceptions, structures the response, and automatically logs the stack trace with your Correlation ID attached.

Installation

npm install nestjs-observability

(Peer Dependencies: Make sure you have @nestjs/common, @nestjs/core, and rxjs installed).


1. Quick Start (Module Registration)

Import the ObservabilityModule into your root AppModule using the .forRoot() method. This immediately sets up the correlation ID middleware, the global error filter, the Prometheus metrics endpoints, and request interceptors.

import { Module } from '@nestjs/common';
import { ObservabilityModule } from 'nestjs-observability';
import { AppController } from './app.controller';

@Module({
  imports: [
    ObservabilityModule.forRoot({
      serviceName: 'my-user-service',  // Required: Labels your logs/metrics
      prettyLogs: true,                // Highly recommended for local dev
      enableMetrics: true,             // Exposes the /metrics endpoint
      globalTracking: true,            // Tracks all controllers automatically
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

2. Using the Centralized Logger

Once registered, you can inject ObsLogger anywhere in your application.

import { Controller, Get } from '@nestjs/common';
import { ObsLogger } from 'nestjs-observability';

@Controller('users')
export class UsersController {
  constructor(private readonly logger: ObsLogger) {}

  @Get()
  getUsers() {
    // 1. Simple logging (Service, CorrelationID, and Context are appended automatically)
    this.logger.info('Fetching users from database', 'UsersController');

    // 2. Logging with extra arbitrary payload data
    this.logger.info({ 
      message: 'Found active users', 
      userCount: 42,
      tenantId: 't-123'
    }, 'UsersController');

    return [];
  }
}

Log Formatting Behavior

  • In Production (prettyLogs: false): Logs are printed as single-line NDJSON format. Ideal for ingestion by systems like Datadog, ELK, or Grafana Loki.
  • In Development (prettyLogs: true): Logs are colorized. The main message is printed on the first line, and any extra payload data is cleanly indented underneath it.

3. Prometheus Metrics (/metrics)

When enableMetrics: true is passed into the module options, this package automatically mounts a MetricsController.

You can simply navigate to http://localhost:<YOUR_PORT>/metrics to scrape the data.

Automated Default Tracking

Because globalTracking: true is enabled by default, the underlying ObservabilityInterceptor automatically tracks:

  1. http_requests_total: Total number of HTTP requests.
  2. http_request_duration_seconds: Histogram measuring route latency.
  3. http_errors_total: Counter for requests that throw an exception.
  4. Default NodeJS internal metrics (Heap size, Event loop lag, etc.)

4. OpenTelemetry Distributed Tracing

To trace requests across multiple microservices, this package exposes an initTelemetry function.

Important: OpenTelemetry must be initialized before NestJS starts. Place it at the very top of your main.ts entry point:

import { initTelemetry } from 'nestjs-observability';

// 1. Initialize tracing FIRST
initTelemetry({
  serviceName: 'my-user-service',
  // You can pass a custom exporter here (e.g. OTLPTraceExporter for Jaeger)
});

// 2. Start NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

5. Configuration Options Breakdown

Below are the options available when calling ObservabilityModule.forRoot(options):

| Property | Type | Default | Description | | :--- | :---: | :---: | :--- | | serviceName | string | Required | The name of your microservice. Injected into every log entry and telemetry metric tag. | | enableLogging | boolean | true | Allows you to completely disable standard logging outputs. | | enableMetrics | boolean | true | Enables the /metrics endpoint and automated Prometheus interceptors. | | globalTracking | boolean | true | Automatically binds the ObservabilityInterceptor to all controllers to track durations and counts. If false, you must apply @UseInterceptors(ObservabilityInterceptor) manually. | | prettyLogs | boolean | false | When true, colorizes console output and pretty-prints JSON payloads on multiple lines. Keep false in production. | | defaultLogData | Record<string, any> | {} | Hardcoded metadata injected into every log entry (e.g., { environment: 'staging' }). |


6. Accessing the Correlation ID Manually

If you need the active correlation ID (e.g., to pass it into an external HTTP request), you can access it anywhere using the RequestContext async local storage construct:

import { RequestContext } from 'nestjs-observability';

// Retrieve the correlation ID of the current execution context
const traceId = RequestContext.getCorrelationId();
console.log(traceId);