- Install and configure the agent
- Track authenticated users across traces
- Capture exceptions and custom messages
- Use tracing helpers and decorators
- Configure instrumentations (mail, cache, jobs, AI, MCP)
- Fan out telemetry to multiple destinations
Prerequisites
- AdonisJS 6.2.0+ or AdonisJS 7.0.0+
- A Monocle account with an API key
Installation
Install and configure the package using the Ace CLI.See what this command does
See what this command does
- Creates
config/monocle.tsconfiguration file - Creates
otel.tsinitialization file at project root - Adds the
otel.tsimport as the first import inbin/server.tsandbin/console.ts - Registers the Monocle provider in
adonisrc.ts - Registers the Monocle middleware as the first router middleware
- Adds required environment variables to
.envandstart/env.ts
.env:
.env
Environment Variables
The agent requires these environment variables:| Variable | Description | Required |
|---|---|---|
MONOCLE_API_KEY | Your Monocle API key | Yes |
APP_NAME | Service name for identification in Monocle | Yes |
APP_VERSION | Service version (e.g., git sha, semver) | Yes |
APP_ENV | Environment: development, staging, or production | Yes |
The
APP_ENV variable determines which environment your telemetry data is
associated with in Monocle. You can filter traces, logs, and metrics by
environment using the environment selector in the dashboard.Configuration
The configuration file is located atconfig/monocle.ts. Here is a minimal setup:
config/monocle.ts
Full configuration reference
All options are optional exceptapiKey, serviceName, serviceVersion, and environment.
config/monocle.ts
Customizing instrumentations
You can configure individual OpenTelemetry instrumentations by passing options keyed by their package name.config/monocle.ts
/health, /healthz, /ready, /readiness, /metrics, /favicon.ico and other common health check paths.
User Context
To associate telemetry data with authenticated users, callMonocle.setUser() in your authentication middleware. This lets you filter traces and exceptions by user in the dashboard.
app/middleware/silent_auth_middleware.ts
name field takes priority in the Monocle UI, falling back to email, then id.
Exception Tracking
Exceptions thrown during a request are automatically captured by the agent. It hooks into AdonisJS’sExceptionHandler.report() method and respects your ignore rules (ignoreExceptions, ignoreStatuses, ignoreCodes).
Manual exception capture
You can manually capture exceptions anywhere in your code.app/services/payment_service.ts
Capture messages
UseMonocle.captureMessage() to capture custom messages when you want to track something that is not an exception. Messages appear in the Exceptions dashboard alongside errors.
app/services/order_service.ts
debug, info, log, warning, error, fatal.
Tracing Helpers
Creating custom spans
Userecord() to wrap a section of code in a span. This is useful when you want to measure the duration of a specific operation that is not automatically instrumented.
app/services/report_service.ts
record() handles both sync and async callbacks, automatically records exceptions, and closes the span when done.
Decorators
The@span() decorator wraps a single method in a span. The @spanAll() decorator wraps every public method in a class.
app/services/order_service.ts
Other helpers
Instrumentations
HTTP
HTTP tracing is configured with the top-levelhttp option.
Alongside the standard HTTP instrumentation options like ignoredUrls, Monocle adds two HTTP-specific features:
sanitizeUrlsto redact sensitive query parameters from URL span attributescaptureBodiesto record request and response payloads on spans
sanitizeUrls to enable URL sanitization. For body capture, set captureBodies.enabled: true, or opt in per side with incoming.request.enabled, incoming.response.enabled, and so on.
config/monocle.ts
URL sanitization
sanitizeUrls rewrites URL span attributes such as http.url, url.full, http.target, and url.query.
If you do not set sanitizeUrls.censor, Monocle uses ***.
config/monocle.ts
Body capture scope
incoming captures traffic handled by your Adonis app:
incoming.requestfor the HTTP request body sent to your serverincoming.responsefor the HTTP response body returned by your server
outgoing captures traffic created by Node HTTP clients inside your app:
outgoing.requestfor the request body your app sends to another serviceoutgoing.responsefor the response body returned by that service
captureBodies.outgoing only applies to traffic instrumented by
@opentelemetry/instrumentation-http, which means node:http and
node:https. It does not cover fetch or undici.Body capture defaults
captureBodies is opt-in because payload capture can expose sensitive data.
When enabled, Monocle:
- captures
application/jsonandapplication/x-www-form-urlencodedby default - stores at most
10_000bytes per request or response unless you increasemaxBodySize - shows captured payloads in the HTTP Body tab of the trace details view
Redaction order
captureBodies.redact applies declarative redaction rules first.
captureBodies.transform runs after that and lets you mutate or replace the captured payload programmatically.
The declarative redact rules extend Monocle’s built-in safe defaults. Common secrets such as password, token, code, access_token, refresh_token, authorization, and cookie are redacted automatically even if you do not list them yourself.
Advanced overrides
If you need low-level OpenTelemetry overrides, you can still configureinstrumentations["@opentelemetry/instrumentation-http"].
When both are present, the low-level instrumentation config takes priority over the top-level http option.
@adonisjs/mail is installed, the agent automatically instruments all emails sent through the mail service. Every email appears as a span in your traces with subject, recipients, and transport info. You can monitor email activity in the dedicated Emails dashboard.
To disable:
config/monocle.ts
Cache
If@adonisjs/cache (Bentocache) is installed, the agent instruments cache operations (get, set, getOrSet, delete, etc.). You can monitor hit rates, latency, and per-key breakdowns in the Cache dashboard.
Cache keys are automatically sanitized to reduce cardinality. UUIDs, numeric IDs, and hex hashes are replaced with *. For example, users:550e8400-e29b-41d4-a716-446655440000 becomes users:*.
config/monocle.ts
config/monocle.ts
Background Jobs
@adonisjs/queue
If@adonisjs/queue (boringqueue) is installed, the agent instruments job dispatch and execution. Jobs appear in the Jobs dashboard with execution count, error rate, and latency metrics.
config/monocle.ts
BullMQ
BullMQ is also supported. The agent instrumentsQueue.add, Queue.addBulk, FlowProducer.add, FlowProducer.addBulk, and Worker processing. Jobs show up in the same Jobs dashboard.
config/monocle.ts
config/monocle.ts
AI SDK
If the Vercel AI SDK (ai package) is installed, the agent auto-instruments generateText, streamText, generateObject, streamObject, embed, embedMany, and rerank calls. You can monitor AI activity, costs, and token usage in the AI Agents dashboard.
config/monocle.ts
Conversation tracking
UsewithConversationId() to group multiple AI calls into a single conversation flow. This enables the Conversations tab in the AI dashboard.
app/services/chat_service.ts
config/monocle.ts
CLI Commands
Ace CLI command execution is traced by default. Scaffolding commands (make:*, generate:*) and long-running queue workers are excluded automatically.
Command arguments and flags are captured as span attributes (cli.args.*, cli.flags.*).
config/monocle.ts
config/monocle.ts
MCP Server
If you are building an MCP server with@modelcontextprotocol/sdk, you can instrument it to track tool calls, clients, and performance in the MCP Monitoring dashboard.
app/mcp/server.ts
Multi-Destination OTLP
You can send telemetry to additional OTLP backends alongside Monocle. The Monocle destination is always injected automatically.config/monocle.ts
Disabling Telemetry
If no API key is provided, telemetry is automatically disabled. This is useful for local development..env.development
Migrating from @adonisjs/otel
If you are already using@adonisjs/otel, switching to the Monocle agent is straightforward. The agent wraps @adonisjs/otel and provides the same API with additional capabilities.
1. Swap the package
2. Rename config file
Renameconfig/otel.ts to config/monocle.ts and replace defineConfig import:
config/monocle.ts
3. Update all imports
Replace all@adonisjs/otel imports with their Monocle equivalents:
Why use Monocle’s helpers
The Monocle agent re-exports most helpers from@adonisjs/otel unchanged (getCurrentSpan, setAttributes, recordEvent, etc.). However, record, handleError, @span, and @spanAll are enhanced versions that add source code context extraction.
When an exception is caught by these helpers, the agent reads the actual source files from disk and attaches the surrounding lines of code to the span. This is what powers the source code preview in the Monocle exception viewer. You get the exact line that errored with surrounding context, without needing source maps.
The agent also fully serializes the error cause chain (Error.cause), including AggregateError children, so nested errors are never lost.
These enhancements only work when importing from @monocle.sh/adonisjs-agent. If you keep using @adonisjs/otel imports directly, you will get standard OpenTelemetry error recording without the extra context.