Performance and errors monitoring

OpenTelemetry

You can monitor DB performance and errors using distributed tracingopen in new window and metricsopen in new window. Tracing allows you to see how a request progresses through different services and systems, timings of every operation, any logs and errors as they occur.

Bun supports tracing and metrics using OpenTelemetry API. OpenTelemetry is a vendor-neutral API for distributed traces and metrics. It specifies how to collect and send telemetry data to backend platforms. It means that you can instrument your application once and then add or change vendors (backends) as required.

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)
db.AddQueryHook(bunotel.NewQueryHook(bunotel.WithDBName("mydb")))

As expected, Bun 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 on GitHubopen in new window.

Prometheus

You can send OpenTelemetry metrics to Prometheus using the corresponding exporteropen in new window. Alternatively, you can also use j2gg0s/otsqlopen in new window that directly works with Prometheus.

Get insights and updates in your inbox: