Bun Performance Monitoring
What is OpenTelemetry?
Bun relies on OpenTelemetry to monitor database performance and errors using OpenTelemetry tracing and OpenTelemetry metrics.
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 backend 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.
Bun comes with an OpenTelemetry instrumentation called bunotel 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) db.AddQueryHook(bunotel.NewQueryHook(bunotel.WithDBName("mydb")))
To make tracing work, you must use an active span context when executing queries, for example:
ctx := req.Context() err := db.NewSelect().Scan(ctx)
Uptrace is an open-source APM and a popular DataDog competitor 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 Uptrace by downloading a DEB/RPM package or a pre-compiled binary.
As expected, otelbun creates spans for processed queries and records any errors as they occur. Here is how the collected information is displayed at Uptrace:
You can find a runnable example at GitHub.
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 exporter.
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.