Skip to content

Commit

Permalink
adding examples
Browse files Browse the repository at this point in the history
  • Loading branch information
egon12 committed Nov 2, 2022
1 parent 8acec7e commit ea8ed91
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 17 deletions.
53 changes: 53 additions & 0 deletions v3/integrations/nrpgx5/example/pgx/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"fmt"
"log"
"os"
"time"

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

func main() {
cfg, err := pgx.ParseConfig("postgres://postgres:postgres@localhost:5432")
if err != nil {
panic(err)
}

cfg.Tracer = nrpgx5.NewTracer()
conn, err := pgx.ConnectConfig(context.Background(), cfg)
if err != nil {
panic(err)
}

app, err := newrelic.NewApplication(
newrelic.ConfigAppName("PostgreSQL App"),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
newrelic.ConfigDebugLogger(os.Stdout),
)
if err != nil {
panic(err)
}
//
// N.B.: We do not recommend using app.WaitForConnection in production code.
//
app.WaitForConnection(5 * time.Second)
txn := app.StartTransaction("postgresQuery")

ctx := newrelic.NewContext(context.Background(), txn)
row := conn.QueryRow(ctx, "SELECT count(*) FROM pg_catalog.pg_tables")
count := 0
err = row.Scan(&count)
if err != nil {
log.Println(err)
}

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

fmt.Println("number of entries in pg_catalog.pg_tables", count)
}
53 changes: 53 additions & 0 deletions v3/integrations/nrpgx5/example/pgxpool/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"fmt"
"log"
"os"
"time"

"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")
if err != nil {
panic(err)
}

cfg.ConnConfig.Tracer = nrpgx5.NewTracer()
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
if err != nil {
panic(err)
}

app, err := newrelic.NewApplication(
newrelic.ConfigAppName("PostgreSQL App"),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
newrelic.ConfigDebugLogger(os.Stdout),
)
if err != nil {
panic(err)
}
//
// N.B.: We do not recommend using app.WaitForConnection in production code.
//
app.WaitForConnection(5 * time.Second)
txn := app.StartTransaction("postgresQuery")

ctx := newrelic.NewContext(context.Background(), txn)
row := db.QueryRow(ctx, "SELECT count(*) FROM pg_catalog.pg_tables")
count := 0
err = row.Scan(&count)
if err != nil {
log.Println(err)
}

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

fmt.Println("number of entries in pg_catalog.pg_tables", count)
}
33 changes: 16 additions & 17 deletions v3/integrations/nrpgx5/nrpgx5.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,44 @@
// you can set the tracer in the pgx.Config like this
// ```go
// import (
// "context"
// "github.com/jackc/pgx/v5"
// "github.com/newrelic/go-agent/v3/integrations/nrpgx5"
// "github.com/newrelic/go-agent/v3/newrelic"
// )
//
// func main() {
// config, err := pgx.ParseConfig("postgres://user:password@localhost:5432/database")
// cfg, err := pgx.ParseConfig("postgres://postgres:postgres@localhost:5432")
// if err != nil {
// panic(err)
// }
// }
//
// cfg.Tracer = nrpgx5.NewTracer()
// conn, err := pgx.ConnectConfig(context.Background(), cfg)
// if err != nil {
// panic(err)
// cfg.Tracer = nrpgx5.NewTracer()
// conn, err := pgx.ConnectConfig(context.Background(), cfg)
// if err != nil {
// panic(err)
// }
// }
// ...
// ```
// or you can set the tracer in the pgxpool.Config like this
// ```go
// import (
// "context"
// "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() {
// config, err := pgxpool.ParseConfig("postgres://user:password@localhost:5432/database")
// cfg, err := pgxpool.ParseConfig("postgres://postgres:postgres@localhost:5432")
// if err != nil {
// panic(err)
// }
// }
//
// cfg.ConnConfig.Tracer = nrpgx5.NewTracer()
// conn, err := pgxpool.ConnectConfig(context.Background(), cfg)
// if err != nil {
// panic(err)
// cfg.ConnConfig.Tracer = nrpgx5.NewTracer()
// db, err := pgxpool.NewWithConfig(context.Background(), cfg)
// if err != nil {
// panic(err)
// }
// }
// ...
// ```

package nrpgx5
Expand Down

0 comments on commit ea8ed91

Please sign in to comment.