Skip to content

Commit

Permalink
pgxpool example thread safety fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iamemilio committed Mar 15, 2024
1 parent 2e972b5 commit 88ab1e1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 14 additions & 5 deletions v3/integrations/nrpgx5/example/pgxpool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ import (
"os"
"time"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/newrelic/go-agent/v3/integrations/nrpgx5"
"github.com/newrelic/go-agent/v3/newrelic"
)

func main() {
cfg, err := pgxpool.ParseConfig("postgres://postgres:postgres@localhost:5432")
func NewPgxPool(ctx context.Context, dbURL string) (*pgxpool.Pool, error) {
cfg, err := pgxpool.ParseConfig(dbURL)
if err != nil {
panic(err)
return nil, err
}

cfg.BeforeConnect = func(_ context.Context, config *pgx.ConnConfig) error {
config.Tracer = nrpgx5.NewTracer()
return nil
}

cfg.ConnConfig.Tracer = nrpgx5.NewTracer()
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
return pgxpool.NewWithConfig(ctx, cfg)
}

func main() {
db, err := NewPgxPool(context.Background(), "postgres://postgres:postgres@localhost:5432")
if err != nil {
panic(err)
}
Expand Down
3 changes: 0 additions & 3 deletions v3/integrations/nrpgx5/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ require (
github.com/newrelic/go-agent/v3 v3.30.0
github.com/stretchr/testify v1.8.0
)


replace github.com/newrelic/go-agent/v3 => ../..

0 comments on commit 88ab1e1

Please sign in to comment.