Bun Performance Monitoring

What is OpenTelemetry?

Bun relies on OpenTelemetry to monitor database performance and errors using OpenTelemetry tracingopen in new window and OpenTelemetry metricsopen in new window.

OpenTelemetry is designed to be language- and framework-agnostic, supporting multiple programming languages and frameworks. It offers language-specific software development kits (SDKs) that make it easier to integrate telemetry collection into applications written in different languages.

OpenTelemetry also provides exporters and integrations to send telemetry data to various OpenTelemetry backendopen in new window systems and observability platforms, including popular tools like Prometheus, Grafana, Jaeger, Zipkin, Elasticsearch, and more.

By using OpenTelemetry, developers can adopt a standardized approach to observability, making it easier to collect and analyze telemetry data across different components of a distributed system. It helps improve troubleshooting, performance optimization, and monitoring of applications, providing valuable insights into their behavior and performance.

OpenTelemetry instrumentaton

Bun comes with an OpenTelemetry instrumentation called bunotelopen in new window that is distributed as a separate module:

go get github.com/uptrace/bun/extra/bunotel

To instrument Bun database, you need to add the hook provided by bunotel:

import "github.com/uptrace/bun/extra/bunotel"

db := bun.NewDB(sqldb, dialect)

To make tracing work, you must use an active span contextopen in new window when executing queries, for example:

ctx := req.Context()
err := db.NewSelect().Scan(ctx)


Uptrace is an open-source APMopen in new window and a popular DataDog competitoropen in new window that supports distributed tracing, metrics, and logs. You can use it to monitor applications and set up automatic alerts to receive notifications via email, Slack, Telegram, and more.

You can install Uptraceopen in new window by downloading a DEB/RPM package or a pre-compiled binary.

As expected, otelbun creates spansopen in new window for processed queries and records any errors as they occur. Here is how the collected information is displayed at Uptraceopen in new window:

Bun trace

You can find a runnable example at GitHubopen in new window.


OpenTelemetry can also integrate with Prometheus, a popular monitoring and alerting system, to collect and export telemetry data.

By integrating OpenTelemetry with Prometheus, you can leverage the powerful monitoring and alerting capabilities of Prometheus while benefiting from the flexibility and standardization provided by OpenTelemetry. This integration enables you to collect, store, visualize, and analyze metrics from your applications and systems, gaining valuable insights into their performance and behavior.

You can send OpenTelemetry metrics to Prometheus using OpenTelemetry Prometheus exporteropen in new window.


Overall, monitoring SQL performance is crucial for optimizing query execution, improving application responsiveness, ensuring scalability, troubleshooting issues, and maintaining the security and compliance of your database environment. It enables you to proactively manage and optimize your SQL infrastructure, leading to better application performance, efficient resource utilization, and enhanced user satisfaction.