Skip to content

Apply SQL_BODY_MAX_LENGTH to ClickHouse JDBC plugin span tags#807

Open
daguimu wants to merge 1 commit intoapache:mainfrom
daguimu:fix/clickhouse-sql-body-max-length
Open

Apply SQL_BODY_MAX_LENGTH to ClickHouse JDBC plugin span tags#807
daguimu wants to merge 1 commit intoapache:mainfrom
daguimu:fix/clickhouse-sql-body-max-length

Conversation

@daguimu
Copy link
Copy Markdown
Contributor

@daguimu daguimu commented Apr 29, 2026

Problem

ClickHouseStatementTracingWrapper (in clickhouse-0.3.1-plugin) and ClickHousePrepareStatementTracing (in clickhouse-0.3.2.x-plugin) record the SQL string directly into the DB_STATEMENT span tag:

Tags.DB_STATEMENT.set(span, sql);

Every other JDBC plugin in apm-sdk-plugin/ routes the same tag through SqlBodyUtil#limitSqlBodySize, which truncates the value to JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH and appends .... As a result, an operator who lowers SQL_BODY_MAX_LENGTH to control trace payload size still sees the full unbounded SQL emitted by the ClickHouse plugins.

Root Cause

The two ClickHouse interceptors miss the SqlBodyUtil#limitSqlBodySize call that the rest of the JDBC plugin family uses. The configuration option exists and works for every other JDBC dialect; only ClickHouse does not honour it.

Fix

Wrap the SQL with SqlBodyUtil#limitSqlBodySize in both interceptors. SqlBodyUtil already handles the negative-limit sentinel that disables truncation, so behaviour is unchanged for users who left the default in place but rely on the disable-truncation override.

Add a CHANGES.md entry under "Bug Fixes".

Tests Added

Change point Test
ClickHouseStatementTracingWrapper.of truncates when SQL exceeds the configured limit ClickHouseStatementTracingWrapperTest#longSqlIsTruncatedToConfiguredLimit — sets the limit to 16, asserts the recorded tag is the first 16 characters plus ...
Same wrapper passes short SQL through unchanged ClickHouseStatementTracingWrapperTest#shortSqlIsRecordedAsIs — sets the limit to 2048, asserts the recorded tag equals the original SQL
Negative limit disables truncation ClickHouseStatementTracingWrapperTest#negativeLimitDisablesTruncation — sets the limit to -1 with a 3KB SQL, asserts the full SQL is recorded
Same three semantics for ClickHousePrepareStatementTracing.of ClickHousePrepareStatementTracingTest#shortSqlIsRecordedAsIs, #longSqlIsTruncatedToConfiguredLimit, #negativeLimitDisablesTruncation

mvn -pl apm-sniffer/apm-sdk-plugin/clickhouse-0.3.1-plugin,apm-sniffer/apm-sdk-plugin/clickhouse-0.3.2.x-plugin -am test — all 31 tests in clickhouse-0.3.2.x-plugin and 6 tests in clickhouse-0.3.1-plugin pass locally.

Each test restores JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH in @After to keep test ordering safe.

Impact

  • Users who configured plugin.jdbc.sql_body_max_length will now see the same truncation behaviour for ClickHouse spans that they already get for MySQL / PostgreSQL / H2 / SQL Server / ...
  • Default users (no explicit limit) keep the same payload size as before, since the default limit is large.
  • No public API changes.

ClickHouseStatementTracingWrapper (clickhouse-0.3.1-plugin) and
ClickHousePrepareStatementTracing (clickhouse-0.3.2.x-plugin) wrote
the raw SQL straight into the DB_STATEMENT span tag. Other JDBC
plugins (mysql-*, postgresql-*, h2, mssql, etc.) route the same tag
through SqlBodyUtil#limitSqlBodySize, which truncates the value to
JDBCPluginConfig.Plugin.JDBC.SQL_BODY_MAX_LENGTH and appends
"...". Without that helper the ClickHouse plugins ignore the user
configuration and emit unbounded SQL bodies.

Apply SqlBodyUtil#limitSqlBodySize in both interceptors so the tag
respects the configured maximum length (and the negative-value
sentinel that disables truncation).

Add ClickHouseStatementTracingWrapperTest and
ClickHousePrepareStatementTracingTest with three cases each (short
sql passthrough, truncation at the configured limit, negative limit
disables truncation), each restoring the configured limit in tearDown.

Update CHANGES.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant