The Monocle agent instruments your AdonisJS application with OpenTelemetry. Once it is installed, HTTP requests, database queries, Redis calls, outgoing HTTP calls, emails, queue jobs, Ace commands, logs, and exceptions are traced automatically.
This guide uses @monocle.sh/adonisjs-agent 2.x. Upgrading from 1.x? Follow the migration
guide.
Prerequisites
- AdonisJS 6.2.0+ or 7.0.0+
- A Monocle project with a service. See Core concepts if you are not sure what that means.
Installation
- Add the agent
Terminal node ace add @monocle.sh/adonisjs-agentWhat this command does
- Creates the
config/monocle.tsconfiguration file - Creates the
otel.tsinitialization file at the project root - Adds
import '../otel.js'as the first import ofbin/server.tsandbin/console.ts - Registers the Monocle provider in
adonisrc.ts - Registers the Monocle middleware as the first router middleware
- Adds
APP_ENVandMONOCLE_DSNto.envand their validators tostart/env.ts
- Creates the
- Copy your DSN
In Monocle, open Services, select your service, pick AdonisJS under SDK connection, then click Create DSN. You can copy it again from the same page at any time.
.env MONOCLE_DSN=https://mk_live_xxxxxxxx@ingest.monocle.sh/00000000-0000-4000-8000-000000000001 - Deploy and make a request
Set the same variables in your production environment, deploy, and hit any route. The request shows up in Monocle within a few seconds, under the environment set by
APP_ENV.
Keep otel.ts as the very first import. OpenTelemetry has to patch libraries like http,
pg, and redis before they are loaded. If import '../otel.js' is moved or removed from
bin/server.ts or bin/console.ts, auto-instrumentation stops working.
Environment variables
The configurator generates only APP_ENV and MONOCLE_DSN. It does not generate or require APP_NAME or APP_VERSION.
| Variable | Description |
|---|---|
MONOCLE_DSN | Connection string of your service. Required for Monocle cloud unless you use a legacy API key. |
APP_ENV | Monocle environment to report to: production, staging, or development. |
MONOCLE_VERSION | Optional. Pins the version of the deployed code before APP_VERSION. |
APP_VERSION | Optional. Pins the version without a config line. Otherwise, the agent detects the version. |
OTEL_SERVICE_NAME | Optional. Overrides the service name in Studio, without a DSN, or for extra OTLP destinations. |
APP_NAME | Optional. Service name fallback after OTEL_SERVICE_NAME. |
With a DSN, the collector uses the name of the service attached to that DSN. In Studio, without a DSN, or for extra OTLP destinations, the name comes from serviceName, then OTEL_SERVICE_NAME, then APP_NAME, then the app's package.json name without its npm scope. For example, @acme/api becomes api. If none is available, the name is unknown_service.
Existing apps need no change. Their config and validators still work, and an APP_VERSION=0.0.1 placeholder lets automatic detection take over.
In development, the generated config sets dev: true, so telemetry goes to Monocle
Studio on your machine instead of Monocle cloud. You don't need a DSN locally.
Next steps
Every option of config/monocle.ts
Attach users to traces and capture errors manually
Trace your own business logic with spans and decorators
Ignore routes, redact URLs, capture request bodies
Built-in instrumentations and optional integrations
Nothing showing up in Monocle?