Golang ORM Performance and Errors Monitoring
What is OpenTelemetry?
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 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 the active span context when executing queries, for example:
ctx := req.Context() err := db.NewSelect().Scan(ctx)
Uptrace is an open source DataDog alternative that helps developers pinpoint failures and find performance bottlenecks. Uptrace can process billions of spans on a single server and allows to monitor your software at 10x lower cost.
You can install Uptrace by downloading a DEB/RPM package or a pre-compiled binary.
You can find a runnable example at GitHub.