Skip to content

Add remote address tracking for JDBC/Avatica SQL queries#19231

Open
dprmfl wants to merge 1 commit intoapache:masterfrom
dprmfl:19230-fix-jdbc-avatica-remote-address
Open

Add remote address tracking for JDBC/Avatica SQL queries#19231
dprmfl wants to merge 1 commit intoapache:masterfrom
dprmfl:19230-fix-jdbc-avatica-remote-address

Conversation

@dprmfl
Copy link
Copy Markdown

@dprmfl dprmfl commented Mar 30, 2026

Fixes #19230.

Description

remoteAddress is always null for JDBC/Avatica queries, even though it's a documented dimension for sqlQuery/time, sqlQuery/bytes, and sqlQuery/planningTimeMs. This PR fixes that.

Problem

See #19230. In short, remoteAddress was hardcoded as null in PreparedStatement.java and SqlStatementFactory.java for the Avatica path, while SqlResource.java (the HTTP path) correctly passes
httpRequest.getRemoteAddr().

Solution

  1. DruidAvaticaJsonHandler — extracts remoteAddr from the HTTP request and stores it in a ThreadLocal. Cleans up in finally to prevent memory leaks.
  2. DruidMeta — reads from ThreadLocal in openConnection() and injects remoteAddress into the connection context map.
  3. SqlStatementFactory & PreparedStatement — reads remoteAddress from context and passes it to DirectStatement.
  4. SqlExecutionReporter — emits remoteAddress as a dimension in metrics and includes it in request logs.

No changes to DruidConnection or any existing data structures —remoteAddress piggybacks on the context map that's already there.

Testing

Added three test methods to DruidAvaticaHandlerTest:

  • testRemoteAddressInLogs — verifies remote address is captured for normal queries
  • testRemoteAddressInFailedQuery — verifies remote address is captured even when queries fail
  • testRemoteAddressInPreparedStatement — verifies remote address is captured for prepared statements

Release note

sqlQuery/time, sqlQuery/bytes, and sqlQuery/planningTimeMs metrics now include remoteAddress for JDBC/Avatica queries, consistent with the HTTP SQL API.


Key changed/added classes in this PR
  • DruidAvaticaJsonHandler — ThreadLocal-based remote address capture
  • DruidMeta — connection context enrichment with remote address
  • SqlStatementFactory — remote address extraction and propagation
  • PreparedStatement — remote address handling for prepared statements
  • SqlExecutionReporter — remote address emission in metrics and logs
image

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.

@dprmfl dprmfl changed the title Add remote address tracking for JDBC Avatica SQL queries Add remote address tracking for JDBC/Avatica SQL queries Mar 30, 2026
@dprmfl dprmfl force-pushed the 19230-fix-jdbc-avatica-remote-address branch 2 times, most recently from 17dd93d to 5aac04a Compare March 31, 2026 06:14
test: verify remote address capture in Avatica SQL queries

test: Replace forbidden API calls in DruidAvaticaHandlerTest

test: Update test expectations to include remoteAddress in query context & Update Quidem test files for remote address tracking

test: Fix checkstyle violations
@dprmfl dprmfl force-pushed the 19230-fix-jdbc-avatica-remote-address branch from 1563f92 to fc1d96f Compare March 31, 2026 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlQuery metrics always emit null remoteAddress for JDBC/Avatica queries

1 participant