The agent is configured in config/monocle.ts. The file is created for you by node ace add. This page lists every option and the most common customizations.
Default configuration
This is the file generated by node ace add:
import { defineConfig } from '@monocle.sh/adonisjs-agent'
import env from '#start/env'
export default defineConfig({
// Send telemetry to Monocle Studio instead of Monocle cloud
dev: process.env.NODE_ENV === 'development',
dsn: env.get('MONOCLE_DSN'),
environment: env.get('APP_ENV'),
})| Option | Description |
|---|---|
dev | When true, telemetry goes to Monocle Studio on localhost:4200 and the DSN is ignored. |
dsn | Connection string of your service. Falls back to the MONOCLE_DSN environment variable. |
serviceVersion | Version of the deployed code. When unset or a placeholder, the agent detects it. |
environment | Monocle environment: production, staging, or development. Falls back to NODE_ENV, then development. |
apiKey and serviceName still work for existing setups but are deprecated for Monocle cloud.
See Migrate to DSN.
Neither APP_NAME nor APP_VERSION is generated or required. To pin a version, set MONOCLE_VERSION or APP_VERSION in the environment, or set serviceVersion explicitly. See the detection order.
With a DSN, the collector uses the service name attached to the DSN. In Studio, without a DSN, or for extra OTLP destinations, the agent uses serviceName, then OTEL_SERVICE_NAME, then APP_NAME, then the app's package.json name without its npm scope. If no name is available, it uses unknown_service.
Existing apps need no change. The old serviceVersion: env.get('APP_VERSION') line still works with its existing validator. Placeholder versions such as 0.0.1 are skipped.
Full reference
Every other option is optional. The values below are the defaults.
import { defineConfig } from '@monocle.sh/adonisjs-agent'
import env from '#start/env'
export default defineConfig({
dev: process.env.NODE_ENV === 'development',
dsn: env.get('MONOCLE_DSN'),
environment: env.get('APP_ENV'),
// Custom ingestion endpoint (default: https://ingest.monocle.sh)
endpoint: 'https://ingest.monocle.sh',
// Skip OPTIONS/CORS preflight requests (default: true)
ignoreOptionsRequests: true,
// Enable gzip compression (default: true)
compression: true,
// Trace batching configuration
batch: {
maxExportBatchSize: 512,
scheduledDelayMillis: 5000,
exportTimeoutMillis: 30000,
maxQueueSize: 2048,
},
// Host metrics: CPU, memory, network (default: enabled)
hostMetrics: {
enabled: true,
},
// Sampling ratio (0.0 to 1.0). Use 0.1 for high-traffic production.
// Default: 1.0 (100% of traces sampled)
samplingRatio: 1.0,
// Adonis features (enabled by default when the corresponding libraries are available)
mail: { enabled: true },
queue: { enabled: true },
cli: { enabled: true },
// Install and register optional integrations below as needed
integrations: [],
})Customizing instrumentations
You can configure individual OpenTelemetry instrumentations by passing options keyed by their package name.
export default defineConfig({
// ...
instrumentations: {
'@opentelemetry/instrumentation-http': {
// Add URLs to ignore (merged with defaults by default)
ignoredUrls: ['/internal/*', '/api/ping'],
// Set to false to replace default ignored URLs instead of merging
mergeIgnoredUrls: true,
// Ignore static files like css, js, images (default: true)
ignoreStaticFiles: true,
},
// Disable a specific instrumentation entirely
'@opentelemetry/instrumentation-pg': { enabled: false },
},
})Default ignored URLs include /health, /healthz, /ready, /readiness, /metrics, /favicon.ico and other common health check paths.
Sampling
samplingRatio sets a global trace rate. sampling(ctx) selects traces by HTTP request, Ace command, or job.
Global rate
Set samplingRatio: 0.05 to record about 5% of traces. OpenTelemetry makes this decision before the execution starts. A discarded trace cannot be recovered if the execution later fails.
sampling(ctx) sets the default recording ratio to 1, overriding samplingRatio. Choose rates in the callback. A custom OpenTelemetry sampler still takes precedence.
HTTP rules
import { defineConfig, Sample } from '@monocle.sh/adonisjs-agent'
export default defineConfig({
// ...
sampling(ctx) {
if (ctx.kind === 'http' && ctx.method === 'GET' && ctx.path.startsWith('/products/')) {
return Sample.rate(0.05).keepErrors()
}
return Sample.default()
},
})This keeps about 5% of successful matching requests. It also keeps requests whose completed spans contain an error.
The callback receives these HTTP fields:
| Field | Type | Value |
|---|---|---|
ctx.kind | 'http' | Incoming HTTP request |
ctx.method | string | Request method, such as GET or POST |
ctx.path | string | Request pathname without the query string |
ctx.route | string | undefined | Not available when the HTTP span starts |
Adonis resolves routes after the callback runs, so ctx.route is unavailable. Match ctx.method and ctx.path, not route patterns such as /products/:id.
Outgoing HTTP spans inherit the incoming request's decision without invoking the callback. Requests excluded by HTTP instrumentation do not invoke it either.
Nested local HTTP or Ace spans inherit the enclosing execution's rule. Separate incoming requests have independent decisions, even with the same distributed trace ID.
Ace rules
Match a traced Ace command with ctx.kind === 'command' and ctx.name:
sampling(ctx) {
if (ctx.kind === 'command' && ctx.name === 'reports:sync') {
return Sample.rate(0.1).keepErrors()
}
return Sample.default()
}ctx.name is a string such as reports:sync. CLI include and exclude filters decide which commands produce spans before sampling runs. See Ace commands.
Defaults and error retention
TypeScript infers ctx from sampling(ctx). Checking ctx.kind selects the HTTP, command, or job fields. Methods, paths, and command names are strings, not literal types derived from your app.
Sample.default(), Sample.always(), and returning undefined keep traces admitted by the OpenTelemetry sampler. They do not restore a separate samplingRatio value.
Use Sample.rate(0).keepErrors() to keep only failed executions. Sample.never() drops failures too, even with keepErrors().
keepErrors() buffers spans until the entry-point span ends. It exports traces selected by the rate or containing a completed span with an error status or exception event. Errors on spans that end later cannot change the decision.
The default sampler respects an upstream parent's sampling decision. keepErrors() cannot recover traces discarded by that decision or by a custom OpenTelemetry sampler.
If an HTTP or Ace sampling callback throws, the agent reports an OpenTelemetry diagnostic and keeps the execution's trace.
See Job Sampling for job fields, supported queue integrations, and buffer limits.
Sending to other OTLP backends
You can send telemetry to additional OTLP backends alongside Monocle. The Monocle destination is always injected automatically.
import { defineConfig, destinations } from '@monocle.sh/adonisjs-agent'
export default defineConfig({
// ...
destinations: [
destinations.otlp({
name: 'grafana',
url: 'https://otlp-gateway.grafana.net/otlp',
headers: {
Authorization: `Basic ${btoa('instance_id:api_key')}`,
},
}),
],
})Each destination receives the full telemetry data (traces, metrics, logs).
Disabling telemetry
Outside of dev mode, telemetry is disabled when no DSN is set. Leave MONOCLE_DSN empty in any environment that should not report to Monocle:
MONOCLE_DSN=Telemetry is also disabled automatically when NODE_ENV=test, so your test suite never sends data.
Debugging the agent
Set debug: true to also print every span to the console, which helps checking what the agent records before looking at Monocle:
export default defineConfig({
// ...
debug: true,
})For more on missing data, see Troubleshooting.