[Go] Datastore segments (nrpq) not working, when transactions in general are working fine

Hi,

We’ve recently started using New Relic and I have been adjusting our code (Go) to report statistics to NR. It’s working great so far - we’ve got transactions for a few different applications working great and showing up.

However, I can’t seem to get the database integration working in particular. We are using PostgreSQL; I have imported the NRPQ driver instead of regular pq as instructed (import _ "github.com/newrelic/go-agent/v3/integrations/nrpq"), and integrated it into our general session handling with XORM. It wasn’t working, so I tried just plain doing this:

		ctx := context.Background()
		if common.NewRelicApplication != nil {
			transactionName := fmt.Sprintf("%s (%s)", strPath, r.Method)
			txn := common.NewRelicApplication.StartTransaction(transactionName, w, r)
			ctx = newrelic.NewContext(ctx, txn)
			defer txn.End()
		}
		db, err := sql.Open("nrpostgres", common.Cfg.Database.ConnString())
		webcommon.CheckError(err)
		row := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM pg_catalog.pg_tables")

		var count int64
		row.Scan(&count)
		common.Log.Debugf("Table count: %d", count)

This should be completely undisturbed by our ORM. The query works fine, but we still aren’t getting anything under Databases in New Relic. I have already verified that the NewRelicApplication is there and that the transaction itself works. If I manually create a segment (including a datastore segment) around the query here, that also works, but obviously we want it to work for all the queries we run without doing that manually for each one.

Is there something I’m missing to make it work?

1 Like

@ahall -

It looks like you’ve got things wired up correctly.

Have you looked at Go agent debug logs? I wonder if they might surface something.

To do that add newrelic.ConfigDebugLogger(os.Stdout) to your agent init section, for example:

	app, err := newrelic.NewApplication(
		newrelic.ConfigAppName("HTTP Server App"),
		newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
		newrelic.ConfigDebugLogger(os.Stdout),
		newrelic.ConfigDistributedTracerEnabled(true),
	)

Thanks,

t.

@ahall,

Just for my own curiosity I stood up a postgres db and tried the example shown here and I see the datastore metric shown here:

I know that’s not helpful other than to demonstrate the integration seems to work.

I haven’t spotted anything incorrect about your code unless the connection string is incorrect in some way.

@Tjd I can confirm the nrpq integration official example does not work.

The nrmysql integration works well though.

I’m running postgres on docker using:

docker run -it -p 5432:5432 --net "bridge" -e POSTGRES_PASSWORD=mysecretpassword postgres

main.go

package main

import (
	"context"
	"database/sql"
	"fmt"
	"os"
	"time"

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

func main() {
	db, err := sql.Open("nrpostgres", "host=localhost port=5432 user=postgres dbname=postgres password=mysecretpassword sslmode=disable")
	if err != nil {
		panic(err)
	}

	app, err := newrelic.NewApplication(
		newrelic.ConfigAppName("PostgreSQL App"),
		newrelic.ConfigLicense("REDACTED"),
		newrelic.ConfigDebugLogger(os.Stdout),
	)
	if nil != err {
		panic(err)
	}
	app.WaitForConnection(5 * time.Second)
	txn := app.StartTransaction("postgresQuery")

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

	txn.End()
	app.Shutdown(5 * time.Second)

	fmt.Println("number of entries in pg_catalog.pg_tables", count)
}

go.mod:

go 1.16

require (
	github.com/google/go-cmp v0.3.1 // indirect
	github.com/lib/pq v1.10.2 // indirect
	github.com/newrelic/go-agent/v3 v3.13.0
	github.com/newrelic/go-agent/v3/integrations/nrpq v1.1.1
	golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
)

Output: go run main.go(81288) 2021/07/09 09:13:05.752270 {"level":"info","msg":"applic - Pastebin.com

While switching between different tests, I was looking at the wrong app… my bad!

The nrpq example does work well

1 Like

@Stephane.Bruckert Thanks for following up on this and confirming its working well :slight_smile: