Job sampling lets you reduce trace volume for noisy background jobs without disabling job monitoring entirely. It is useful when a small number of jobs run very often or create very large traces. With the Monocle AdonisJS agent, sampling is configured inDocumentation Index
Fetch the complete documentation index at: https://docs.monocle.sh/llms.txt
Use this file to discover all available pages before exploring further.
config/monocle.ts with the Sample helper
config/monocle.ts
SendNewsletterEmail executions, but still exports failed executions.
Sampling rules
Keep default behavior
UseSample.default() when you do not want to override sampling for an execution.
Always keep
UseSample.always() when a specific job should always be exported.
Never keep
UseSample.never() for jobs you do not want to export.
Sample by rate
UseSample.rate(rate) to keep a percentage of executions. The rate is a number between 0 and 1.
Keep failed executions
Use.keepErrors() when successful executions should be sampled, but failed executions should still be exported.
Sample.rate(0).keepErrors() drops successful executions and keeps failed executions. This is the most aggressive setting for high-volume jobs where you mainly care about failures.
Limit buffered spans
When usingkeepErrors(), the agent buffers spans locally until the job finishes so it can decide whether to export the trace. You can cap the number of buffered spans with maxSpans.
| Attribute | Description |
|---|---|
monocle.sampling.truncated | true when spans were dropped from the local buffer |
monocle.sampling.dropped_spans | Number of spans dropped from the local buffer |
Sampling context
For job executions, thesampling(ctx) callback receives:
| Field | Description |
|---|---|
ctx.kind | Always "job" for job executions |
ctx.system | Queue system, for example "bullmq" or "boringqueue" |
ctx.name | Job name |
ctx.queue | Queue name, when available |
ctx.jobId | Job ID, when available |
ctx.attempt | Retry attempt, when available |
Supported job libraries
BullMQ
BullMQ sampling is applied before the worker body runs. When a job is dropped withSample.never() or an unsampled rate rule without keepErrors(), Monocle suppresses tracing for the worker execution, so child spans are not created.
See the BullMQ instrumentation guide for setup and span details.
@adonisjs/queue
@adonisjs/queue job executions are detected from their root job spans. Sampling works with Sample.rate(), Sample.never(), and keepErrors().
See the @boringnode/queue instrumentation guide for span details.
How keepErrors works
keepErrors() is application-side tail sampling. The agent records spans locally for the job execution, buffers them until the root job span ends, and then exports the trace if the execution was sampled or ended with an error.
Because the decision happens after spans are recorded, Monocle configures the default OpenTelemetry head sampling ratio to 1 when sampling is enabled. If you provide a custom OpenTelemetry sampler, traces dropped by that sampler cannot be recovered by keepErrors().