otelsqlx

package module
v0.0.0-...-5231181 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2023 License: BSD-2-Clause Imports: 3 Imported by: 1

README

PkgGoDev

sqlx instrumentation for OpenTelemetry Go

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

go get github.com/middleware-labs/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/middleware-labs/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/middleware-labs/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"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(driverName, dataSourceName string, opts ...otelsql.Option) (*sqlx.DB, error)

Connect to a database and verify with a ping.

func ConnectContext

func ConnectContext(
	ctx context.Context, driverName, dataSourceName string, opts ...otelsql.Option,
) (*sqlx.DB, error)

ConnectContext to a database and verify with a ping.

func MustConnect

func MustConnect(driverName, dataSourceName string, opts ...otelsql.Option) *sqlx.DB

MustConnect connects to a database and panics on error.

func MustOpen

func MustOpen(driverName, dataSourceName string, opts ...otelsql.Option) *sqlx.DB

MustOpen is the same as sql.Open, but returns an *sqlx.DB instead and panics on error.

func Open

func Open(driverName, dataSourceName string, opts ...otelsql.Option) (*sqlx.DB, error)

Open is the same as sql.Open, but returns an *sqlx.DB instead.

Types

This section is empty.

Directories

Path Synopsis
example module
internal module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL