Skip to content

Commit 81e7e50

Browse files
committed
feat: ✨ add nrpgx5 sample
add nrpgx5 sample add nrpgx5 sample
0 parents  commit 81e7e50

File tree

11 files changed

+525
-0
lines changed

11 files changed

+525
-0
lines changed

.github/workflows/go.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
jobs:
11+
build_and_test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Setup go-task
15+
uses: pnorton5432/setup-task@v1
16+
with:
17+
task-version: 3.29.1
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
- name: Setup Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: 'stable'
24+
check-latest: true
25+
- name: Task Build for mage
26+
run: task build-gg
27+
- name: Test with gg build
28+
run: ./gg build

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work
22+
go.work.sum
23+
24+
# env file
25+
.env
26+
bin
27+
gg
28+
mage

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
```

Taskfile.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
version: '3'
2+
3+
tasks:
4+
default:
5+
cmds:
6+
- echo "This is task cmd"
7+
silent: true
8+
9+
build:
10+
cmds:
11+
- CGO_ENABLED=0 GOOS=linux go build -o bin/main cmd/main.go
12+
silent: true
13+
run:
14+
cmds:
15+
- ./bin/main
16+
deps:
17+
- build
18+
silent: true
19+
20+
build-mage:
21+
cmds:
22+
- CGO_ENABLED=0 GOOS=linux go build -o ./mage mage-tools/mage.go
23+
silent: true
24+
25+
build-gg:
26+
cmds:
27+
- ./mage -d mage-tools -compile ../gg
28+
deps:
29+
- build-mage
30+
silent: true
31+
32+
coverage:
33+
cmds:
34+
- go test -v -cover ./...
35+
silent: true
36+
test:
37+
cmds:
38+
- go test -v ./...
39+
silent: true
40+

cmd/main.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/jackc/pgx/v5"
10+
"github.com/leetcode-golang-classroom/golang-sample-with-nrpgx5/internal/config"
11+
"github.com/newrelic/go-agent/v3/integrations/nrpgx5"
12+
"github.com/newrelic/go-agent/v3/newrelic"
13+
)
14+
15+
func main() {
16+
cfg, err := pgx.ParseConfig(config.AppConfig.DBURL)
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
cfg.Tracer = nrpgx5.NewTracer(nrpgx5.WithQueryParameters(true))
22+
conn, err := pgx.ConnectConfig(context.Background(), cfg)
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
app, err := newrelic.NewApplication(
28+
newrelic.ConfigAppName(config.AppConfig.AppName),
29+
newrelic.ConfigLicense(config.AppConfig.NewRelicLicenseKey),
30+
// newrelic.ConfigDebugLogger(os.Stdout),
31+
)
32+
if err != nil {
33+
panic(err)
34+
}
35+
//
36+
// N.B.: We do not recommend using app.WaitForConnection in production code.
37+
//
38+
app.WaitForConnection(5 * time.Second)
39+
txn := app.StartTransaction("postgresQuery")
40+
41+
ctx := newrelic.NewContext(context.Background(), txn)
42+
row := conn.QueryRow(ctx, "SELECT count(*) FROM pg_catalog.pg_tables")
43+
count := 0
44+
err = row.Scan(&count)
45+
if err != nil {
46+
log.Println(err)
47+
}
48+
49+
var a, b int
50+
rows, _ := conn.Query(ctx, "select n, n*2 from generate_series(1, $1) n", 3)
51+
_, err = pgx.ForEachRow(rows, []any{&a, &b}, func() error {
52+
fmt.Printf("%v %v\n", a, b)
53+
return nil
54+
})
55+
if err != nil {
56+
panic(err)
57+
}
58+
txn.End()
59+
app.Shutdown(5 * time.Second)
60+
61+
fmt.Println("number of entries in pg_catalog.pg_tables", count)
62+
}

docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
services:
2+
postgres:
3+
restart: always
4+
image: postgres:16
5+
container_name: postgres_docker_instance
6+
volumes:
7+
- ${HOST_DIR}:/var/lib/postgresql/data
8+
expose:
9+
- 5432
10+
ports:
11+
- ${POSTGRES_PORT}:5432
12+
environment:
13+
- POSTGRES_DB=${POSTGRES_DB}
14+
- POSTGRES_USER=${POSTGRES_USER}
15+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
16+
logging:
17+
driver: "json-file"
18+
options:
19+
max-size: "1k"
20+
max-file: "3"

go.mod

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module github.com/leetcode-golang-classroom/golang-sample-with-nrpgx5
2+
3+
go 1.22.4
4+
5+
require (
6+
github.com/magefile/mage v1.15.0
7+
github.com/spf13/viper v1.19.0
8+
)
9+
10+
require (
11+
github.com/jackc/pgpassfile v1.0.0 // indirect
12+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
13+
golang.org/x/crypto v0.31.0 // indirect
14+
golang.org/x/net v0.25.0 // indirect
15+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
16+
google.golang.org/grpc v1.65.0 // indirect
17+
google.golang.org/protobuf v1.34.2 // indirect
18+
)
19+
20+
require (
21+
github.com/fsnotify/fsnotify v1.7.0 // indirect
22+
github.com/hashicorp/hcl v1.0.0 // indirect
23+
github.com/jackc/pgx/v5 v5.7.2
24+
github.com/magiconair/properties v1.8.7 // indirect
25+
github.com/mitchellh/mapstructure v1.5.0 // indirect
26+
github.com/newrelic/go-agent/v3 v3.35.1
27+
github.com/newrelic/go-agent/v3/integrations/nrpgx5 v1.3.0
28+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
29+
github.com/sagikazarmark/locafero v0.4.0 // indirect
30+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
31+
github.com/sourcegraph/conc v0.3.0 // indirect
32+
github.com/spf13/afero v1.11.0 // indirect
33+
github.com/spf13/cast v1.6.0 // indirect
34+
github.com/spf13/pflag v1.0.5 // indirect
35+
github.com/subosito/gotenv v1.6.0 // indirect
36+
go.uber.org/atomic v1.9.0 // indirect
37+
go.uber.org/multierr v1.9.0 // indirect
38+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
39+
golang.org/x/sys v0.28.0 // indirect
40+
golang.org/x/text v0.21.0 // indirect
41+
gopkg.in/ini.v1 v1.67.0 // indirect
42+
gopkg.in/yaml.v3 v3.0.1 // indirect
43+
)

0 commit comments

Comments
 (0)