dd-trace-go.v1: gopkg.in/DataDog/dd-trace-go.v1/contrib/jmoiron/sqlx Index | Examples | Files

package sqlx

import "gopkg.in/DataDog/dd-trace-go.v1/contrib/jmoiron/sqlx"

Package sqlx provides functions to trace the jmoiron/sqlx package (https://github.com/jmoiron/sqlx). To enable tracing, first use one of the "Register*" functions to register the sql driver that you will be using, then continue using the package as you normally would.

For more information on registering and why this needs to happen, please check the github.com/DataDog/dd-trace-go/contrib/database/sql package.

Index

Examples

Package Files

sql.go

func Connect Uses

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

Connect connects to the data source using the given driver. To get tracing, the driver must be formerly registered using the database/sql integration's Register.

func MustConnect Uses

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

MustConnect connects to a database and panics on error. To get tracing, the driver must be formerly registered using the database/sql integration's Register.

func MustOpen Uses

func MustOpen(driverName, dataSourceName string) (*sqlx.DB, error)

MustOpen is the same as Open, but panics on error. To get tracing, the driver must be formerly registered using the database/sql integration's Register.

func Open Uses

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

Open opens a new (traced) connection to the database using the given driver and source. Note that the driver must formerly be registered using database/sql integration's Register.

Code:

// Register informs the sqlxtrace package of the driver that we will be using in our program.
// It uses a default service name, in the below case "postgres.db". To use a custom service
// name use RegisterWithServiceName.
sqltrace.Register("postgres", &pq.Driver{}, sqltrace.WithServiceName("my-service"))
db, err := sqlxtrace.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable")
if err != nil {
    log.Fatal(err)
}

// All calls through sqlx API will then be traced.
query, args, err := sqlx.In("SELECT * FROM users WHERE level IN (?);", []int{4, 6, 7})
if err != nil {
    log.Fatal(err)
}
query = db.Rebind(query)
rows, err := db.Queryx(query, args...)
if err != nil {
    log.Fatal(err)
}
defer rows.Close()

Package sqlx imports 2 packages (graph) and is imported by 1 packages. Updated 2020-08-05. Refresh now. Tools for package owners.