nrsqlite3

package
v3.16.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

_integrations/nrsqlite3 GoDoc

Package nrsqlite3 instruments https://github.com/mattn/go-sqlite3.

import "github.com/newrelic/go-agent/_integrations/nrsqlite3"

For more information, see godocs.

Documentation

Overview

Package nrsqlite3 instruments https://github.com/mattn/go-sqlite3.

Use this package to instrument your SQLite calls without having to manually create DatastoreSegments. This is done in a two step process:

1. Use this package's driver in place of the sqlite3 driver.

If your code is using sql.Open like this:

import (
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./foo.db")
}

Then change the side-effect import to this package, and open "nrsqlite3" instead:

import (
	_ "github.com/newrelic/go-agent/_integrations/nrsqlite3"
)

func main() {
	db, err := sql.Open("nrsqlite3", "./foo.db")
}

If you are registering a custom sqlite3 driver with special behavior then you must wrap your driver instance using nrsqlite3.InstrumentSQLDriver. For example, if your code looks like this:

func main() {
	sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{
		Extensions: []string{
			"sqlite3_mod_regexp",
		},
	})
	db, err := sql.Open("sqlite3_with_extensions", ":memory:")
}

Then instrument the driver like this:

func main() {
	sql.Register("sqlite3_with_extensions", nrsqlite3.InstrumentSQLDriver(&sqlite3.SQLiteDriver{
		Extensions: []string{
			"sqlite3_mod_regexp",
		},
	}))
	db, err := sql.Open("sqlite3_with_extensions", ":memory:")
}

2. Provide a context containing a newrelic.Transaction to all exec and query methods on sql.DB, sql.Conn, sql.Tx, and sql.Stmt. This requires using the context methods ExecContext, QueryContext, and QueryRowContext in place of Exec, Query, and QueryRow respectively. For example, instead of the following:

row := db.QueryRow("SELECT count(*) from tables")

Do this:

ctx := newrelic.NewContext(context.Background(), txn)
row := db.QueryRowContext(ctx, "SELECT count(*) from tables")

A working example is shown here: https://github.com/newrelic/go-agent/tree/master/_integrations/nrsqlite3/example/main.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InstrumentSQLDriver

func InstrumentSQLDriver(d *sqlite3.SQLiteDriver) driver.Driver

InstrumentSQLDriver wraps an sqlite3.SQLiteDriver to add instrumentation. For example, if you are registering a custom SQLiteDriver like this:

sql.Register("sqlite3_with_extensions",
	&sqlite3.SQLiteDriver{
		Extensions: []string{
			"sqlite3_mod_regexp",
		},
	})

Then add instrumentation like this:

sql.Register("sqlite3_with_extensions",
	nrsqlite3.InstrumentSQLDriver(&sqlite3.SQLiteDriver{
		Extensions: []string{
			"sqlite3_mod_regexp",
		},
	}))

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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