|
| 1 | +# golang-sample-with-nrpgx5 |
| 2 | + |
| 3 | +This repository is for demo how to use nrpgx5 for trace postgresql |
| 4 | + |
| 5 | +## setup postgresql database |
| 6 | + |
| 7 | +1. use docker compose with follow setup |
| 8 | +```yaml |
| 9 | +services: |
| 10 | + postgres: |
| 11 | + restart: always |
| 12 | + image: postgres:16 |
| 13 | + container_name: postgres_docker_instance |
| 14 | + volumes: |
| 15 | + - ${HOST_DIR}:/var/lib/postgresql/data |
| 16 | + expose: |
| 17 | + - 5432 |
| 18 | + ports: |
| 19 | + - ${POSTGRES_PORT}:5432 |
| 20 | + environment: |
| 21 | + - POSTGRES_DB=${POSTGRES_DB} |
| 22 | + - POSTGRES_USER=${POSTGRES_USER} |
| 23 | + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} |
| 24 | + logging: |
| 25 | + driver: "json-file" |
| 26 | + options: |
| 27 | + max-size: "1k" |
| 28 | + max-file: "3" |
| 29 | +``` |
| 30 | +
|
| 31 | +2. run up postgresql instance |
| 32 | +
|
| 33 | +```shell |
| 34 | +docker compose up -d postgres |
| 35 | +``` |
| 36 | + |
| 37 | +## implement instructment with nrpgx5 |
| 38 | + |
| 39 | +```golang |
| 40 | +package main |
| 41 | + |
| 42 | +import ( |
| 43 | + "context" |
| 44 | + "fmt" |
| 45 | + "log" |
| 46 | + "time" |
| 47 | + |
| 48 | + "github.com/jackc/pgx/v5" |
| 49 | + "github.com/leetcode-golang-classroom/golang-sample-with-nrpgx5/internal/config" |
| 50 | + "github.com/newrelic/go-agent/v3/integrations/nrpgx5" |
| 51 | + "github.com/newrelic/go-agent/v3/newrelic" |
| 52 | +) |
| 53 | + |
| 54 | +func main() { |
| 55 | + cfg, err := pgx.ParseConfig(config.AppConfig.DBURL) |
| 56 | + if err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + |
| 60 | + cfg.Tracer = nrpgx5.NewTracer(nrpgx5.WithQueryParameters(true)) |
| 61 | + conn, err := pgx.ConnectConfig(context.Background(), cfg) |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + |
| 66 | + app, err := newrelic.NewApplication( |
| 67 | + newrelic.ConfigAppName(config.AppConfig.AppName), |
| 68 | + newrelic.ConfigLicense(config.AppConfig.NewRelicLicenseKey), |
| 69 | + // newrelic.ConfigDebugLogger(os.Stdout), |
| 70 | + ) |
| 71 | + if err != nil { |
| 72 | + panic(err) |
| 73 | + } |
| 74 | + // |
| 75 | + // N.B.: We do not recommend using app.WaitForConnection in production code. |
| 76 | + // |
| 77 | + app.WaitForConnection(5 * time.Second) |
| 78 | + // root transaction |
| 79 | + txn := app.StartTransaction("postgresQuery") |
| 80 | + |
| 81 | + // pass the root ctx into query |
| 82 | + ctx := newrelic.NewContext(context.Background(), txn) |
| 83 | + row := conn.QueryRow(ctx, "SELECT count(*) FROM pg_catalog.pg_tables") |
| 84 | + count := 0 |
| 85 | + err = row.Scan(&count) |
| 86 | + if err != nil { |
| 87 | + log.Println(err) |
| 88 | + } |
| 89 | + |
| 90 | + // pass the root ctx into query |
| 91 | + var a, b int |
| 92 | + rows, _ := conn.Query(ctx, "select n, n*2 from generate_series(1, $1) n", 3) |
| 93 | + _, err = pgx.ForEachRow(rows, []any{&a, &b}, func() error { |
| 94 | + fmt.Printf("%v %v\n", a, b) |
| 95 | + return nil |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + txn.End() |
| 101 | + app.Shutdown(5 * time.Second) |
| 102 | + |
| 103 | + fmt.Println("number of entries in pg_catalog.pg_tables", count) |
| 104 | +} |
| 105 | +``` |
0 commit comments