{"modulePath":"github.com/OpenSourceProjects/go-agent","version":"v3.19.2+incompatible","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package main\n\nAn application that illustrates how to instrument jmoiron/sqlx with DatastoreSegments\n\nTo run this example, be sure the environment varible NEW\\_RELIC\\_LICENSE\\_KEY is set to your license key.  Postgres must be running on the default port 5432 and have a user \"foo\" and a database \"bar\".\n\nAdding instrumentation for the SQLx package is easy.  It means you can make database calls without having to manually create DatastoreSegments. Setup can be done in two steps:\n\n### Set up your driver {#hdr-Set_up_your_driver}\n\nIf you are using one of our currently supported database drivers (see [https://docs.newrelic.com/docs/agents/go-agent/get-started/go-agent-compatibility-requirements#frameworks](https://docs.newrelic.com/docs/agents/go-agent/get-started/go-agent-compatibility-requirements#frameworks)), follow the instructions on installing the driver.\n\nAs an example, for the \\`lib/pq\\` driver, you will use the newrelic integration's driver in place of the postgres driver.  If your code is using sqlx.Open with \\`lib/pq\\` like this:\n\n\timport (\n\t\t\"github.com/jmoiron/sqlx\"\n\t\t_ \"github.com/lib/pq\"\n\t)\n\n\tfunc main() {\n\t\tdb, err := sqlx.Open(\"postgres\", \"user=pqgotest dbname=pqgotest sslmode=verify-full\")\n\t}\n\nThen change the side-effect import to the integration package, and open \"nrpostgres\" instead:\n\n\timport (\n\t\t\"github.com/jmoiron/sqlx\"\n\t\t_ \"github.com/newrelic/go-agent/_integrations/nrpq\"\n\t)\n\n\tfunc main() {\n\t\tdb, err := sqlx.Open(\"nrpostgres\", \"user=pqgotest dbname=pqgotest sslmode=verify-full\")\n\t}\n\nIf you are not using one of the supported database drivers, use the \\`InstrumentSQLDriver\\` ([https://godoc.org/github.com/newrelic/go-agent#InstrumentSQLDriver](https://godoc.org/github.com/newrelic/go-agent#InstrumentSQLDriver)) API. See [https://github.com/newrelic/go-agent/blob/master/\\_integrations/nrmysql/nrmysql.go](https://github.com/newrelic/go-agent/blob/master/_integrations/nrmysql/nrmysql.go) for a full example.\n\n### Add context to your database calls {#hdr-Add_context_to_your_database_calls}\n\nNext, you must provide a context containing a newrelic.Transaction to all methods on sqlx.DB, sqlx.NamedStmt, sqlx.Stmt, and sqlx.Tx that make a database call.  For example, instead of the following:\n\n\terr := db.Get(\u0026jason, \"SELECT * FROM person WHERE first_name=$1\", \"Jason\")\n\nDo this:\n\n\tctx := newrelic.NewContext(context.Background(), txn)\n\terr := db.GetContext(ctx, \u0026jason, \"SELECT * FROM person WHERE first_name=$1\", \"Jason\")\n\n## Types\n\n```go\ntype Person struct {\n\tFirstName string `db:\"first_name\"`\n\tLastName  string `db:\"last_name\"`\n\tEmail     string\n}\n```\nPerson is a person in the database\n\n","path":"github.com/OpenSourceProjects/go-agent/_integrations/nrpq/example/sqlx","name":"main","synopsis":"An application that illustrates how to instrument jmoiron/sqlx with DatastoreSegments","isRedistributable":true}
