Skip to content

Latest commit

 

History

History

otelsqlx

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

PkgGoDev

sqlx instrumentation for OpenTelemetry Go

otelsqlx instrumentation records sqlx queries (including Tx and Stmt queries) and reports DBStats metrics.

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

Usage

To instrument sqlx, you need to connect to a database using the API provided by this package:

sqlx otelsqlx
sqlx.Connect otelsqlx.Connect
sqlx.ConnectContext otelsqlx.ConnectContext
sqlx.MustConnect otelsqlx.MustConnect
sqlx.Open otelsqlx.Open
sqlx.MustOpen otelsqlx.MustOpen
sqlx.NewDb not supported
import (
    "github.com/uptrace/opentelemetry-go-extra/otelsqlx"
    semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
    _ "modernc.org/sqlite"
)

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

// db is *sqlx.DB

And then use context-aware API to propagate the active span via context:

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

See example for details.

Options

otelsqlx accepts all the options from otelsql package, for example:

import (
    "github.com/uptrace/opentelemetry-go-extra/otelsqlx"
    semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
    _ "modernc.org/sqlite"
)

db, err := otelsqlx.Open("sqlite", "file::memory:?cache=shared",
	otelsql.WithAttributes(semconv.DBSystemSqlite),
	otelsql.WithDBName("mydb"))