Skip to main content

Application metrics and logging

PostgreMQ clients can record OpenTelemetry metrics for queue operations, handler execution, and lost renewals. Instrumentation is opt-in. The application supplies the meter provider and owns its exporter, collection, and shutdown.

Database queue state is collected separately through SQL. Use both sources: client metrics explain attempts and application activity; SQL metrics show retained work across every replica.

Supply a meter provider​

These examples use an existing application meter provider. They add instrumentation to connection creation without creating another telemetry SDK or global provider. Use the observability guide and linked exporter examples when adding an exporter from scratch.

conn, err := postgremq.Dial(ctx, poolConfig,
postgremq.WithMeterProvider(meterProvider),
postgremq.WithShutdownTimeout(10*time.Second))
if err != nil { return err }

meterProvider implements the OpenTelemetry Go metrics API. The Go exporter example owns the provider lifecycle.

Omitting the provider leaves instrumentation disabled even if another library configured a global provider. Pass that global provider explicitly when you want to share it.

Interpret observations​

Operation duration includes pool acquisition and internal retries for one logical call. Sent-message counts describe attempted publish queries, including failures and operations in transactions that later roll back. They are not committed publication counts.

Consumed-message counts record deliveries decoded from claims, including prefetch and redelivery. Handler duration measures callback execution and excludes automatic settlement after the callback returns. An explicit acknowledgement awaited inside the callback is part of callback time and has its own operation observation.

Stream consumers do not emit automatic handler duration or active-handler measurements because the SDK does not know where application processing begins and ends. Instrument that boundary in the application; other client operation metrics still apply.

Renewal-loss counts indicate automatic renewal retired deliveries whose ownership was no longer confirmed. They are not a counter of every possible stale-settlement error. See Metrics for exact names, units, attributes, and SDK differences.

Add useful application logs​

Log queue identity, PostgreMQ message ID, application event ID, delivery attempt, outcome, elapsed processing time, and a bounded error category. A message ID distinguishes a delivery retry from a new publication, while the application event ID connects duplicate publications.

Do not log full payloads or claim tokens by default. Keep per-message identifiers out of metric labels; their high cardinality belongs in appropriately controlled logs. Preserve enough context to diagnose whether failure happened during business work, settlement, renewal, or shutdown.

The metrics API does not add automatic distributed tracing or propagate trace headers. If an application includes correlation or trace information in its JSON payload, it owns that convention and its processing.

Flush after queue shutdown​

Stop incoming application work, close the queue connection within its shutdown budget, then flush and close the meter provider. The queue client does not flush or shut down a supplied provider. Close an application-owned database pool after queue shutdown as well.

For database scraping and alerts, continue with Monitoring and alerting.