1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build go1.10
// 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
package nrsqlite3
import (
"database/sql"
"database/sql/driver"
"path/filepath"
"strings"
sqlite3 "github.com/mattn/go-sqlite3"
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/internal"
"github.com/newrelic/go-agent/internal/sqlparse"
)
var (
baseBuilder = newrelic.SQLDriverSegmentBuilder{
BaseSegment: newrelic.DatastoreSegment{
Product: newrelic.DatastoreSQLite,
},
ParseQuery: sqlparse.ParseQuery,
ParseDSN: parseDSN,
}
)
func init() {
sql.Register("nrsqlite3", InstrumentSQLDriver(&sqlite3.SQLiteDriver{}))
internal.TrackUsage("integration", "driver", "sqlite3")
}
// 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",
// },
// }))
//
func InstrumentSQLDriver(d *sqlite3.SQLiteDriver) driver.Driver {
return newrelic.InstrumentSQLDriver(d, baseBuilder)
}
func getPortPathOrID(dsn string) (ppoid string) {
ppoid = strings.Split(dsn, "?")[0]
ppoid = strings.TrimPrefix(ppoid, "file:")
if ":memory:" != ppoid && "" != ppoid {
if abs, err := filepath.Abs(ppoid); nil == err {
ppoid = abs
}
}
return
}
// ParseDSN accepts a DSN string and sets the Host, PortPathOrID, and
// DatabaseName fields on a newrelic.DatastoreSegment.
func parseDSN(s *newrelic.DatastoreSegment, dsn string) {
// See https://godoc.org/github.com/mattn/go-sqlite3#SQLiteDriver.Open
s.Host = "localhost"
s.PortPathOrID = getPortPathOrID(dsn)
s.DatabaseName = ""
}
|