Skip to content

feat(jdbc): [SQL Sanitizing 2] Sanitize SQL span descriptions in the JDBC integration - #6798

Draft
lbloder wants to merge 2 commits into
feat/sql-sanitizing-vendor-otelfrom
feat/sql-sanitizing-jdbc
Draft

lbloder wants to merge 2 commits into
feat/sql-sanitizing-vendor-otelfrom
feat/sql-sanitizing-jdbc

Conversation

@lbloder

@lbloder lbloder commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

PR Stack (SQL Sanitizing)


📜 Description

Wires the SQL query analyzer from #6795 into sentry-jdbc.

  • SentryJdbcEventListener analyzes each statement in onBeforeAnyExecute, in the dialect of its connection's database system. The span description is the sanitized text (at most 32 KiB). The span also gets db.query.text, db.query.summary and db.stored_procedure.name when present, and db.system.name and db.namespace next to the existing db.system and db.name. If the analysis fails, the span keeps no query text at all, never the raw statement.
  • Database details are resolved per connection URL (cached, up to 64 URLs). p6spy shares one listener across all datasources of the JVM, so until now the first datasource's details labelled every span; with sanitization that would also pick the wrong dialect.
  • When auto.db.jdbc is an ignored span origin (the default in the OpenTelemetry agent and agentless-spring modes), the listener no longer reads the statement or the URL. The span would have been a no-op.
  • DatabaseUtils: file-based database names (SQLite, H2, HSQLDB, Derby) are cut at ;, ? and #, so connection properties such as ?password=… or ?key=… no longer end up in db.name. URLs without a database path (jdbc:postgresql://host:5432/) give no db.name instead of an empty one.
  • sentry-test-support: hasSpanWithOpAndDescription takes an optional map of span data that some span with that op and description must carry.

💡 Motivation and Context

JDBC spans carry the raw SQL statement today, including literal values from statements that don't use bind parameters.

💚 How did you test it?

  • ./gradlew :sentry-jdbc:check: 58 tests, 0 failures. The listener tests check wiring only: span fields equal SqlQueryAnalyzer's output for the URL's database system, so a grammar sync never needs re-baselining. The per-URL test uses a statement whose output differs between MySQL and PostgreSQL. Each new test was checked against a mutation of the listener (raw SQL as fallback description, missing null-URL guard, empty details past the cache bound, a wrapped raw statement in span data).
  • Spring Boot integration suites, one after another: sentry-spring-boot, sentry-spring-boot-jakarta and sentry-spring-boot-4 integrationTest, sentry-spring-boot-4 integrationTestOtlp now assert db.query.summary and db.system.name on the db.query span (expected values from the analyzer); they fail against the previous listener. The three integrationTestOtelNoAgent suites pass as well (ignored-origin path). The OpenTelemetry agent suites and the Spring Boot version matrix are left to CI.
  • JVM timing with and without sanitization: see the comment below.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

  • Android SQLite integration ([SQL Sanitizing 3]), with on-device timing.
  • The changelog entry for the stack goes into the collection PR (feat(sql): SQL Sanitizing #6794).
  • Follow-up PR: unwrap wrapper JDBC URLs (jdbc:tc:, jdbc:aws-wrapper:, jdbc:otel:, jdbc:log4jdbc:) to report the inner database system. Not needed for sanitization: an unknown system gets the stricter dialect.

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into 9.x.x.

#skip-changelog

@github-actions

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 7e091d1

@lbloder

lbloder commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

JVM timing with and without sanitization

Per query, in microseconds (µs). Measured on 7e091d17ba.

Statement Plain DB (µs) Span, no sanitization (µs) Current (µs) Sanitization adds (µs) Analyzer alone (µs)
select by id (92 chars, prepared) 2.7–2.8 3.2–3.3 4.6–4.8 +1.4–1.5 1.2–1.3
select with literals (62 chars) 29–30 30–31 31–32 +1.3–1.5 0.9
join, indexed (140 chars, prepared) 4.3 4.8–4.9 6.9–7.0 +2.0–2.2 1.8
insert (68 chars, prepared) 1.9–2.0 2.3–2.4 3.4–3.5 +1.1 1.0
update (38 chars, prepared) 2.0–2.1 2.3–2.5 3.1–3.4 +0.75–0.8 0.7
IN with 100 parameters (349 chars, prepared) 38–41 40–44 46–49 +5.5–5.9 4.9–5.0
IN with 1,500 literals (7,942 chars) 451–473 469–492 542–564 +73 70–71

Machine: Apple M4 Pro (14 cores, 48 GB), macOS 27.2.1, Temurin OpenJDK 17.0.13. Not idle: load average 4.7–5.5 during the runs (Gradle daemon and desktop apps).

How often: 2 runs of 3 rounds each. Per round and statement: 6,000 measured calls per variant after 3,000 warm-up calls (100-parameter IN: 4,000 after 2,000; 1,500-literal IN: 1,500 after 500). Each call is timed on its own with System.nanoTime(). Each value is the median of the 3 round medians of one run; a range spans the two runs, which agree within 5%. Analyzer alone: 10,000 calls after 2,000 warm-up per round and statement.

Setup:

  • One JVM with an in-memory HSQLDB 2.7.4. There is no network, so this is the worst case for the relative cost.
  • Real Sentry.init with a no-op transport and a transaction bound to the scope.
  • p6spy 3.9.1 with only the Sentry listener registered.
  • Three variants per statement, interleaved per call with a rotating order:
    • Plain DB: HSQLDB without p6spy.
    • Span, no sanitization: this PR's listener with sanitization switched off by a scratch flag (raw SQL as description, no analysis). This is the listener before this PR. The flag is not part of the PR.
    • Current: this PR's listener.

Reading:

  • Sanitization adds about 1–2 µs to a short statement. The analyzer is most of that; the remaining 0.2–0.5 µs is the per-URL details lookup and setting the db.query.* keys.
  • The span itself already costs 0.4–1.0 µs.
  • The ratios are large only because in-memory HSQLDB answers in 2–4 µs. Against a database over the network (0.1 ms or more per round trip), I expect about 2% or less for short statements. That is an assumption; it was not measured.

@github-actions

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 7e091d1.

Main concerns: data sent to Sentry, host performance, reach.

Rules that block low also fired:

  • R6: build or release configuration changed: sentry-jdbc/build.gradle.kts

This label is a hint for reviewers, not an approval.

@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 27, 2026
@sentry

sentry Bot commented Nov 27, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.65.1 (1) release

⚙️ sentry-android Build Distribution Settings

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant