( DOCUMENTATION )Developer Reference

TraceMole Docs

SDK Integration/Next.js Integration Guide

Next.js Integration Guide

Two-line setup with @tracemole/next: traces, MongoDB query spans and explain stats with no changes to your database code.

Install one package, add one line to `instrumentation.ts`, and set your API key. You don't need to change your MongoDB or Mongoose code: TraceMole turns on query monitoring for every MongoClient your app creates and attaches explain stats (docs scanned vs returned, index used, COLLSCAN) to your query spans.

Official Example Application

To speed up your implementation, you can clone and run our working demo app on GitHub: TraceMole Next.js Example App

1. Install the SDK Install

Requires Next.js 15 or newer and Node.js 22.15 or newer. The package bundles a pinned OpenTelemetry SDK, so you don't install or version OpenTelemetry yourself.

npm install @tracemole/next

2. Add `instrumentation.ts` Create

Create `instrumentation.ts` at the root of your project (or `src/instrumentation.ts` if you use a `src/` directory). No `next.config` change is needed.

export { register } from "@tracemole/next";

3. Set your API key (`.env.local`) Config

Copy the key from your project's settings in TraceMole. Add it to your hosting provider's environment variables as well (for example Vercel → Settings → Environment Variables).

TRACEMOLE_API_KEY=tm_live_your_api_key_here

Run your app and open a page or API route that queries MongoDB. Traces appear in your project within a few seconds.

What gets traced

  • Next.js request spans (route handlers, pages, server components), created by Next.js itself.
  • Outgoing `fetch` and HTTP calls.
  • Every MongoDB command inside a request, from the official driver or Mongoose.
  • Explain stats on slow queries. On in development, off in production by default, because explain runs the query a second time.
  • On Vercel, spans are sent when each request finishes so they aren't lost when the function freezes. The Edge runtime is skipped (MongoDB drivers can't run there).

Optional Configure the SDK Options

Replace the one-line export with `registerTraceMole` to set a service name or tune explain. Set `explain: false` to turn explain off, or `TRACE_MOLE_MONGO_EXPLAIN=0` / `1` to override it per environment. `TRACEMOLE_ENDPOINT` sends traces to a different OTLP endpoint (for example a self-hosted ingest).

import { registerTraceMole } from "@tracemole/next";

export const register = () =>
  registerTraceMole({
    serviceName: "checkout-api", // default: OTEL_SERVICE_NAME or package.json name
    explain: {
      slowThresholdMs: 50, // explain queries slower than this (default 100)
      sampleRate: 1, // fraction of slow queries to explain (default 1)
      inProduction: false, // explain re-runs the query; off in production by default
    },
  });

Already have OpenTelemetry set up, or on Next.js 13–14?

TraceMole accepts standard OTLP/HTTP. Keep your own OpenTelemetry setup, point the trace exporter at your TraceMole ingest URL with an `x-api-key` header, and add @tracemole/nextjs-mongodb-explain for explain stats. See the Node.js guide for a full manual setup.