Next.js
Instrument your Next.js app based on App Router with Laminar
1. Install Laminar
2. Initialize Laminar
We suggest to disable Next.js OpenTelemetry instrumentation to avoid excessive tracing.
Observing entire application
To instrument your entire Next.js app, place Laminar initialization in instrumentation.ts
file. Learn more about instrumentation.ts
here.
instrumentation.ts
is experimental in Next.js < 15.
If you use Next.js < 15, add the following to your next.config.js
:
Observing specific routes
If you want to instrument a specific app router route, initialize Laminar at the top of your route file.
Simple initialization may not work for all cases.
The best workaround is to selectively pass instrumentModules
to .initialize()
.
The example below shows how to do this for OpenAI and Anthropic.
If you want to try it quickly without manually importing and instrumenting all modules, you can do simple initialization, however, you will need to import and initialize Laminar before importing LLM client libraries. This is because JavaScript binds the imported module to its name at import time, so introducing instrumentation afterwards statically is not possible.