sentrysql

package module
v0.47.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package sentrysql provides Sentry instrumentation for database/sql drivers.

It wraps an existing driver so that queries and executions become Sentry spans and are surfaced in the Queries module.

Example:

import (
    _ "modernc.org/sqlite"
    "github.com/getsentry/sentry-go"
    sentrysql "github.com/getsentry/sentry-go/sql"
)

db, err := sentrysql.Open("sqlite", ":memory:",
    sentrysql.WithDatabaseSystem(sentrysql.SystemSQLite),
    sentrysql.WithDatabaseName("main"),
)

For well-known drivers (postgres, pgx, mysql, sqlite, sqlite3, sqlserver, mssql, mariadb, oracle, godror, clickhouse, snowflake, ...), Open and Register can infer db.system from the registration name, so the option is optional:

db, err := sentrysql.Open("postgres", dsn) // db.system = "postgresql"

OpenDB, WrapDriver, and WrapConnector cannot see a driver name and always require WithDatabaseSystem.

Index

Constants

View Source
const (
	SystemPostgreSQL = dbsystem.PostgreSQL
	SystemMySQL      = dbsystem.MySQL
	SystemMariaDB    = dbsystem.MariaDB
	SystemSQLite     = dbsystem.SQLite
	SystemMSSQL      = dbsystem.MSSQL
	SystemOracle     = dbsystem.Oracle
	SystemClickhouse = dbsystem.Clickhouse
	SystemSnowflake  = dbsystem.Snowflake
)
View Source
const SpanOrigin sentry.SpanOrigin = "auto.db.sentrysql"

SpanOrigin identifies spans emitted by this package.

Variables

This section is empty.

Functions

func Open

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

Open is a wrapper over sql.Open that provides Sentry instrumentation.

func OpenDB

func OpenDB(c driver.Connector, opts ...Option) (*sql.DB, error)

OpenDB wraps an existing driver.Connector and returns a *sql.DB.

func Register

func Register(name string, drv driver.Driver, opts ...Option) error

Register registers a wrapped version of drv under the name "sentrysql-<name>". It is safe to call once per process; subsequent calls with the same name return nil without re-registering.

func WrapConnector

func WrapConnector(c driver.Connector, opts ...Option) (driver.Connector, error)

WrapConnector wraps a driver.Connector so connections it hands out are instrumented. WithDatabaseSystem is required.

func WrapDriver

func WrapDriver(drv driver.Driver, opts ...Option) (driver.Driver, error)

WrapDriver wraps a driver.Driver so connections it hands out are instrumented.

Types

type DatabaseSystem

type DatabaseSystem = dbsystem.Name

type Option

type Option func(*config)

Option configures sql wrappers.

func WithDatabaseName

func WithDatabaseName(name string) Option

WithDatabaseName sets the db.namespace span attribute (logical database name).

func WithDatabaseSystem

func WithDatabaseSystem(system DatabaseSystem) Option

WithDatabaseSystem sets the db.system span attribute. Prefer one of the System* constants; pass DatabaseSystem("...") for databases not enumerated.

When Open is used and this option is omitted, sentrysql attempts to detect the system from the driver registration name. For OpenDB / WrapDriver / WrapConnector this option is required because the driver name is not available to the wrapper.

func WithDatabaseUser

func WithDatabaseUser(user string) Option

WithDatabaseUser sets the db.user span attribute.

func WithDriverName

func WithDriverName(name string) Option

WithDriverName sets the db.driver.name span attribute. Open and Register populate this automatically from the registered driver name; pass it explicitly when using OpenDB / WrapDriver / WrapConnector.

func WithServerAddress

func WithServerAddress(host string, port int) Option

WithServerAddress sets the server.address (logical hostname) and server.port (logical port) span attributes. Pass port = 0 to omit it.

func WithServerSocketAddress

func WithServerSocketAddress(addr string, port int) Option

WithServerSocketAddress sets the server.socket.address (physical IP or Unix socket path) and server.socket.port (physical port) span attributes. Pass port = 0 to omit it.

Directories

Path Synopsis
internal
fakedriver
Package fakedriver is a minimal in-memory database/sql driver used by the sentrysql tests.
Package fakedriver is a minimal in-memory database/sql driver used by the sentrysql tests.

Jump to

Keyboard shortcuts

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