Otel support
It would be nice to have opentelemetry support for this library to debug long running db queries etc. Any thoughts around this?
I'm interested in this. Have you already tried the auto-instrumentation of https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-pg
We decided to adopt bullmq instead, it had telemetry support and other goodies.
Perhaps it's a feature which can make it in in the future. It's nice to see which jobs leads to long Postgres queries etc 🙂
We have successfully implemented OpenTelemetry (tracing) through a wrapper on top of pg-boss. The main idea is adding the trace IDs inside a metadata field in all jobs, and manually triggering the span methods from the OTel SDK
You can easily get high level queue metrics doing something like this (after setting up the appropriate metrics)
Job level metrics are still necessary for precise numbers as these are just sampled counts.
import { metrics } from '@opentelemetry/api'
// Create a meter
const meter = metrics.getMeter('pg-boss-metrics')
// Define gauges
const queueDeferredGauge = meter.createObservableGauge('pgboss.queue.deferred', {
description: 'Number of deferred jobs in the queue'
})
const queueQueuedGauge = meter.createObservableGauge('pgboss.queue.queued', {
description: 'Number of queued jobs in the queue'
})
const queueActiveGauge = meter.createObservableGauge('pgboss.queue.active', {
description: 'Number of active jobs in the queue'
})
const queueTotalGauge = meter.createObservableGauge('pgboss.queue.total', {
description: 'Total jobs processed by the queue'
})
// Register batch observable callback for pg-boss queue metrics
meter.addBatchObservableCallback(
async observableResult => {
const queues = await boss.getQueues()
for (const queue of queues) {
const attributes = { queue: queue.name }
observableResult.observe(queueDeferredGauge, queue.deferredCount, attributes)
observableResult.observe(queueQueuedGauge, queue.queuedCount, attributes)
observableResult.observe(queueActiveGauge, queue.activeCount, attributes)
observableResult.observe(queueTotalGauge, queue.totalCount, attributes)
}
},
[
queueDeferredGauge,
queueQueuedGauge,
queueActiveGauge,
queueTotalGauge
]
)