Nest.js

TracesMetricsApp LogsCustom LogsProfiling

This guide provides instructions to set up Application Performance Monitoring (APM) in a Nest.js application. You can also find these instructions on the installation page in your Middleware account.

Prerequisites

  1. Node.js 18.17.1+: Verify with node --version
  2. Python Version 3.8+ Verify with python3 --version

Introduction

@middleware.io/nestjs-apm is the official Middleware APM client for NestJS applications that automatically instruments your application with OpenTelemetry, sending runtime metrics, traces/spans, and console logs to Middleware.io.

Installation

1 Install Nest.js APM Package

2 Import the MiddlewareApmModule in your app.module.ts

3 Add the interceptor to your main.ts:

Features

  • Automatic instrumentation of NestJS controllers and services
  • Console log capture (info, warn, error)
  • Distributed tracing
  • Performance metrics
  • Error tracking
  • Custom span attributes
  • Advanced instrumentation decorators
  • OpenTelemetry log integration
  • Exception tracking with OTEL events

Basic Usage

Ignoring Routes

You can use the @IgnoreApmTrace() decorator to exclude specific routes from tracing:

Advanced Instrumentation

Custom Attributes

Add custom attributes to spans:

Custom Spans

Create custom spans with specific names and attributes:

Parameter Recording

Automatically record method parameters as span attributes:

Logging Integration

The module automatically records all NestJS logger output to OpenTelemetry. Just use the standard NestJS logger:

You can combine multiple decorators for comprehensive instrumentation: