You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gotosocial/vendor/github.com/uptrace/opentelemetry-go-extra/otelsql
kim 1018cde107
[chore] bump bun library versions (#2837)
7 months ago
..
.golangci.yml feat: initial tracing support (#1623) 2 years ago
LICENSE feat: initial tracing support (#1623) 2 years ago
README.md [chore] bump bun v1.1.14 -> v1.1.15 (#2195) 1 year ago
driver.go [chore] update bun + extras v1.1.16 -> v1.1.17 (#2534) 10 months ago
otel.go [chore] bump bun library versions (#2837) 7 months ago
stmt.go feat: initial tracing support (#1623) 2 years ago
tx.go feat: initial tracing support (#1623) 2 years ago
version.go [chore] bump bun library versions (#2837) 7 months ago

README.md

PkgGoDev

database/sql instrumentation for OpenTelemetry Go

database/sql OpenTelemetry instrumentation records database queries (including Tx and Stmt queries) and reports DBStats metrics.

Installation

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:

sql otelsql
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.10.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 context:

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

See example for details.

Options

Both otelsql.Open and otelsql.OpenDB accept the same options:

  • WithAttributes configures attributes that are used to create a span.
  • WithDBName configures a db.name attribute.
  • WithDBSystem configures a db.system attribute. When possible, you should prefer using WithAttributes and semconv, for example, otelsql.WithAttributes(semconv.DBSystemSqlite).

sqlboiler

You can use otelsql to instrument sqlboiler ORM:

import (
    "github.com/uptrace/opentelemetry-go-extra/otelsql"
    semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
)

db, err := otelsql.Open("postgres", "dbname=fun user=abc",
    otelsql.WithAttributes(semconv.DBSystemPostgreSQL))
if err != nil {
  return err
}

boil.SetDB(db)

GORM 1

You can use otelsql to instrument GORM 1:

import (
    "github.com/jinzhu/gorm"
    "github.com/uptrace/opentelemetry-go-extra/otelsql"
    semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
)

// gormOpen is like gorm.Open, but it uses otelsql to instrument the database.
func gormOpen(driverName, dataSourceName string, opts ...otelsql.Option) (*gorm.DB, error) {
	db, err := otelsql.Open(driverName, dataSourceName, opts...)
	if err != nil {
		return nil, err
	}
	return gorm.Open(driverName, db)
}

db, err := gormOpen("mysql", "user:password@/dbname",
    otelsql.WithAttributes(semconv.DBSystemMySQL))
if err != nil {
    panic(err)
}

To instrument GORM 2, use otelgorm.

Alternatives