nrpgx

package
v0.0.0-...-41bf266 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: CC0-1.0 Imports: 10 Imported by: 0

Documentation

Overview

This file was cloned from the New Relic go agent on May 24, 2023. Edits were required to enable this in our repo:

  1. Switch from pgx/v4/stdlib to pgx/stdlib to align with our current version of pgx (v3) and avoid "Register called twice for driver pgx" errors.
  2. To enable a custom connection and logging configuration, use stdlib.getDefaultDriver(). See: https://github.com/newrelic/go-agent/issues/435

Copyright 2021 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Package nrpgx instruments https://github.com/jackc/pgx/v4.

Use this package to instrument your PostgreSQL calls using the pgx library.

USING WITH PGX AS A DATABASE/SQL DRIVER

The pgx library may be used as a database/sql driver rather than making direct calls into pgx itself. In this scenario, just use the nrpgx integration in place of the pgx driver. In other words, if your code without New Relic's agent looks like this:

	import (
     "database/sql"
		_ "github.com/jackc/pgx/v4/stdlib"
	)

	func main() {
		db, err := sql.Open("pgx", "user=pqgotest dbname=pqgotest sslmode=verify-full")
	}

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

	import (
     "database/sql"

		_ "github.com/newrelic/go-agent/v3/integrations/nrpgx"
	)

	func main() {
		db, err := sql.Open("nrpgx", "user=pqgotest dbname=pqgotest sslmode=verify-full")
	}

Next, provide a context containing a newrelic.Transaction to all exec and query methods on sql.DB, sql.Conn, and sql.Tx. 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 pg_catalog.pg_tables")

Do this:

ctx := newrelic.NewContext(context.Background(), txn)
row := db.QueryRowContext(ctx, "SELECT count(*) FROM pg_catalog.pg_tables")

A working example is shown here: https://github.com/newrelic/go-agent/tree/master/v3/integrations/nrpgx/example/sql_compat/main.go

USING WITH DIRECT PGX CALLS WITHOUT DATABASE/SQL

This mode of operation is not supported by the nrpgx integration at this time.

Jump to

Keyboard shortcuts

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