feat: add PostgreSQL tracing support for sql and gorm#240
feat: add PostgreSQL tracing support for sql and gorm#240wl2027 wants to merge 5 commits intoapache:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds PostgreSQL tracing support to the SkyWalking Go agent for database/sql (via pgx/v5/stdlib) and for GORM (via gorm.io/driver/postgres), and wires up new integration test scenarios to validate emitted spans.
Changes:
- Add a new
sql/pgxstdlibplugin to extract PostgreSQL peer info from pgx DSNs and enable SQL span tagging. - Add a new
gorm/postgresplugin to extract PostgreSQL peer info from the GORM Postgres dialector/config and propagate it into the GORM entry plugin. - Add Postgres + GORM-Postgres scenario apps and expected trace snapshots; include them in the workspace and CI plugin-test matrix.
Reviewed changes
Copilot reviewed 22 out of 28 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/go-agent/instrument/plugins/register.go | Registers the new PostgreSQL SQL + GORM instruments with the agent tool. |
| plugins/sql/pgxstdlib/instrument.go | Declares the pgx stdlib instrumentation point (Driver.OpenConnector). |
| plugins/sql/pgxstdlib/interceptor.go | Extracts PostgreSQL peer info from pgx DSN/config and stores it in runtime context for database/sql entry plugin. |
| plugins/sql/pgxstdlib/interceptor_test.go | Unit tests for DSN parsing and peer address formatting edge-cases. |
| plugins/sql/go.mod | Adds dependencies needed for pgx stdlib plugin + tests. |
| plugins/sql/go.sum | Locks new/updated dependencies for the SQL plugin module. |
| plugins/gorm/postgres/instrument.go | Declares GORM postgres driver instrumentation points (Open, New, struct enhance). |
| plugins/gorm/postgres/interceptor.go | Extracts peer info from GORM postgres dialector DSN / Conn and stores it on the dialector dynamic field. |
| plugins/gorm/postgres/interceptor_test.go | Unit tests for DSN parsing, multi-host, and SQL-Conn adaptation. |
| plugins/gorm/go.mod | Adds postgres driver + pgx dependencies for the GORM plugin module. |
| plugins/gorm/go.sum | Locks new/updated dependencies for the GORM plugin module. |
| plugins/gorm/entry/interceptor.go | Adds a nil guard when setting up DB info from the opened *gorm.DB. |
| plugins/gorm/entry/interceptor_test.go | Adds test coverage for PrepareStmt mode to ensure spans are still reported. |
| test/plugins/scenarios/postgres/plugin.yml | Defines the new postgres SQL scenario metadata and docker dependency. |
| test/plugins/scenarios/postgres/main.go | Scenario app exercising database/sql operations against Postgres via pgx stdlib. |
| test/plugins/scenarios/postgres/go.mod | Module definition for the postgres SQL scenario app. |
| test/plugins/scenarios/postgres/go.sum | Dependency lockfile for the postgres SQL scenario app. |
| test/plugins/scenarios/postgres/config/excepted.yml | Expected trace snapshot for the postgres SQL scenario. |
| test/plugins/scenarios/postgres/bin/startup.sh | Builds and runs the postgres SQL scenario app with SQL parameter collection enabled. |
| test/plugins/scenarios/gorm-postgres/plugin.yml | Defines the new gorm-postgres scenario metadata and docker dependency. |
| test/plugins/scenarios/gorm-postgres/main.go | Scenario app exercising GORM CRUD + prepared statements against Postgres. |
| test/plugins/scenarios/gorm-postgres/go.mod | Module definition for the gorm-postgres scenario app. |
| test/plugins/scenarios/gorm-postgres/go.sum | Dependency lockfile for the gorm-postgres scenario app. |
| test/plugins/scenarios/gorm-postgres/config/excepted.yml | Expected trace snapshot for the gorm-postgres scenario. |
| test/plugins/scenarios/gorm-postgres/bin/startup.sh | Builds and runs the gorm-postgres scenario app with GORM parameter collection enabled. |
| go.work | Adds the new scenario modules to the workspace. |
| docs/en/agent/support-plugins.md | Documents support/testing versions for GORM Postgres driver and pgx stdlib. |
| .github/workflows/plugin-tests.yaml | Adds postgres + gorm-postgres scenarios to CI plugin-test matrix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mrproliu
left a comment
There was a problem hiding this comment.
Generally LGTM, please fix the CI failure and above comments.
docs/en/agent/support-plugins.md
Outdated
| * `grpc` : [gRPC](https://github.com/grpc/grpc-go) tested v1.55.0 to v1.64.0. | ||
| * Database Client | ||
| * `gorm`: [GORM](https://github.com/go-gorm/gorm) tested v1.22.0 to v1.25.1. | ||
| * `gorm`: [GORM](https://github.com/go-gorm/gorm) tested v1.22.0 to v1.25.10. |
There was a problem hiding this comment.
You have changed the max version of this framework, then please also add the version check in this plugin test.
There was a problem hiding this comment.
I reverted gorm back to v1.25.1 for now.
When I tried to extend the GORM test coverage to v1.25.10, I found that upstream GORM changed how LIMIT/OFFSET is generated. For example, LIMIT 1 becomes a parameterized LIMIT ? with 1 moved into the bound parameters. go-gorm/gorm#6806
The current plugin scenario excepted.yml does not seem to support covering these different but valid outputs in a single file. Should we enhance the excepted.yml matcher in a follow-up to handle this kind of case?
The last CI failure is in the |
Summary
This PR adds PostgreSQL tracing support to the SkyWalking Go agent for:
database/sqlwithpgx/v5 stdlibgormwithgorm.io/driver/postgresIncluded
Scope
This PR focuses on these PostgreSQL paths:
sql.Open("pgx", dsn)gorm.Open(postgres.Open(dsn), ...)It does not yet cover
stdlib.OpenDB(...),stdlib.OpenDBFromPool(...), orRegisterConnConfig()+sql.Open(...).Verification
go test ./plugins/sql/pgxstdlib ./plugins/gorm/entry ./plugins/gorm/postgres ./plugins/core/tracingbash test/plugins/run.sh postgresbash test/plugins/run.sh gorm-postgres