sql

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2018 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package sql provides functions to trace the database/sql package (https://golang.org/pkg/database/sql). It will automatically augment operations such as connections, statements and transactions with tracing.

We start by telling the package which driver we will be using. For example, if we are using "github.com/lib/pq", we would do as follows:

sqltrace.Register("pq", pq.Driver{})
db, err := sqltrace.Open("pq", "postgres://pqgotest:password@localhost...")

The rest of our application would continue as usual, but with tracing enabled.

Example
package main

import (
	"log"

	sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"

	"github.com/lib/pq"
)

func main() {
	// The first step is to register the driver that we will be using.
	sqltrace.Register("postgres", &pq.Driver{})

	// Followed by a call to Open.
	db, err := sqltrace.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}

	// Then, we continue using the database/sql package as we normally would, with tracing.
	rows, err := db.Query("SELECT name FROM users WHERE age=?", 27)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
}
Output:

Example (Context)
package main

import (
	"context"
	"log"

	sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

	"github.com/go-sql-driver/mysql"
)

func main() {
	// Register the driver that we will be using (in this case mysql) under a custom service name.
	sqltrace.Register("mysql", &mysql.MySQLDriver{}, sqltrace.WithServiceName("my-db"))

	// Open a connection to the DB using the driver we've just registered with tracing.
	db, err := sqltrace.Open("mysql", "user:password@/dbname")
	if err != nil {
		log.Fatal(err)
	}

	// Create a root span, giving name, server and resource.
	_, ctx := tracer.StartSpanFromContext(context.Background(), "my-query",
		tracer.SpanType(ext.SpanTypeSQL),
		tracer.ServiceName("my-db"),
		tracer.ResourceName("initial-access"),
	)

	// Subsequent spans inherit their parent from context.
	rows, err := db.QueryContext(ctx, "SELECT * FROM city LIMIT 5")
	if err != nil {
		log.Fatal(err)
	}
	rows.Close()
}
Output:

Example (Sqlite)
package main

import (
	"context"
	"log"

	sqlite "github.com/mattn/go-sqlite3"
	sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
	// Register the driver that we will be using (in this case Sqlite) under a custom service name.
	sqltrace.Register("sqlite", &sqlite.SQLiteDriver{}, sqltrace.WithServiceName("sqlite-example"))

	// Open a connection to the DB using the driver we've just registered with tracing.
	db, err := sqltrace.Open("sqlite", "./test.db")
	if err != nil {
		log.Fatal(err)
	}

	// Create a root span, giving name, server and resource.
	_, ctx := tracer.StartSpanFromContext(context.Background(), "my-query",
		tracer.SpanType("example"),
		tracer.ServiceName("sqlite-example"),
		tracer.ResourceName("initial-access"),
	)

	// Subsequent spans inherit their parent from context.
	rows, err := db.QueryContext(ctx, "SELECT * FROM city LIMIT 5")
	if err != nil {
		log.Fatal(err)
	}
	rows.Close()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

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

Open returns connection to a DB using a the traced version of the given driver. In order for Open to work, the driver must first be registered using Register or RegisterWithServiceName. If this did not occur, Open will return an error.

func Register

func Register(driverName string, driver driver.Driver, opts ...RegisterOption)

Register tells the sql integration package about the driver that we will be tracing. It must be called before Open, if that connection is to be traced. It uses the driverName suffixed with ".db" as the default service name.

Types

type RegisterOption added in v1.0.0

type RegisterOption func(*registerConfig)

RegisterOption represents an option that can be passed to Register.

func WithServiceName added in v1.0.0

func WithServiceName(name string) RegisterOption

WithServiceName sets the given service name for the registered driver.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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