Skip to content

feat: add PostgreSQL tracing support for sql and gorm#240

Open
wl2027 wants to merge 5 commits intoapache:mainfrom
wl2027:main-postgre
Open

feat: add PostgreSQL tracing support for sql and gorm#240
wl2027 wants to merge 5 commits intoapache:mainfrom
wl2027:main-postgre

Conversation

@wl2027
Copy link
Copy Markdown

@wl2027 wl2027 commented Apr 6, 2026

Summary

This PR adds PostgreSQL tracing support to the SkyWalking Go agent for:

  • database/sql with pgx/v5 stdlib
  • gorm with gorm.io/driver/postgres

Included

  • Add PostgreSQL SQL tracing support
  • Add GORM PostgreSQL tracing support
  • Add PostgreSQL SQL/GORM scenarios and tests

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(...), or RegisterConnConfig()+sql.Open(...).

Verification

  • go test ./plugins/sql/pgxstdlib ./plugins/gorm/entry ./plugins/gorm/postgres ./plugins/core/tracing
  • bash test/plugins/run.sh postgres
  • bash test/plugins/run.sh gorm-postgres

@wu-sheng wu-sheng requested review from Copilot and mrproliu April 6, 2026 15:00
@wu-sheng wu-sheng added this to the 0.7.0 milestone Apr 6, 2026
@wu-sheng wu-sheng added the enhancement New feature or request label Apr 6, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/pgxstdlib plugin to extract PostgreSQL peer info from pgx DSNs and enable SQL span tagging.
  • Add a new gorm/postgres plugin 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.

Copy link
Copy Markdown
Contributor

@mrproliu mrproliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, please fix the CI failure and above comments.

* `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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have changed the max version of this framework, then please also add the version check in this plugin test.

Copy link
Copy Markdown
Author

@wl2027 wl2027 Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@wl2027
Copy link
Copy Markdown
Author

wl2027 commented Apr 7, 2026

Generally LGTM, please fix the CI failure and above comments.

The last CI failure is in the rocketmq scenario. From the logs, it looks more like an existing stability issue in that RocketMQ scenario than something introduced by this pr . Would you like me to follow up separately and work on that scenario?

@wl2027 wl2027 requested a review from mrproliu April 7, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants