Still using Jaeger/Sentry? Uptrace is an open source distributed tracing tool that uses OpenTelemetry to monitor performance, errors, and logs.

database/sql OpenTelemetry Instrumentation

Installation

To install database/sql OpenTelemetry instrumentation:

go get github.com/uptrace/opentelemetry-go-extra/otelsql

Usage

To instrument database/sql, you need to connect to a database using the API provided by otelsql:

sqlotelsql
sql.Open(driverName, dsn)otelsql.Open(driverName, dsn)
sql.OpenDB(connector)otelsql.OpenDB(connector)
import (
	"github.com/uptrace/opentelemetry-go-extra/otelsql"
	semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

db, err := otelsql.Open("sqlite", "file::memory:?cache=shared",
	otelsql.WithAttributes(semconv.DBSystemSqlite),
	otelsql.WithDBName("mydb"))
if err != nil {
	panic(err)
}

// db is *sql.DB

And then use context-aware API to propagate the active span via contextopen in new window:

var num int
if err := db.QueryRowContext(ctx, "SELECT 42").Scan(&num); err != nil {
	panic(err)
}

What's next?

Next, instrument more operations, for example, database queries, errors, and logs. You can also learn about OpenTelemetry Go Tracing API to create your own instrumentations.

Popular instrumentations:

See also:

Last Updated: