Skip to content

Answer a malformed Prometheus request body with a 4xx - #120938

Merged
nikitamikhaylov merged 3 commits into
ClickHouse:masterfrom
valerypetrov:fix/prometheus-malformed-body-status
Sep 22, 2026
Merged

nikitamikhaylov merged 3 commits into
ClickHouse:masterfrom
valerypetrov:fix/prometheus-malformed-body-status

Conversation

@valerypetrov

@valerypetrov valerypetrov commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

A Prometheus remote-write or remote-read request whose body cannot be decompressed is now answered with 400 instead of 500, and the error names the size of the block and the uncompressed length it declares.

Documentation entry for user-facing changes

No documentation change: the status of a malformed request is not described in the Prometheus protocol docs.


A body the client encoded wrongly is the client's permanent fault, but the two decoder error codes are not on the 4xx list, so the server answered 500. Prometheus treats 5xx as retryable, so a sender resent a body it could never fix, for as long as it kept it.

Measured before, against a remote-write body whose snappy frame declares 70000 uncompressed bytes over a 65536-byte literal:

500  Code: 638. DB::Exception: snappy uncompress failed: . (SNAPPY_UNCOMPRESS_FAILED)

and after:

400  Code: 36. DB::Exception: Cannot decode the request body: Snappy decompression failed:
     a 69 byte block declares 70000 uncompressed bytes. (BAD_ARGUMENTS)

The error codes themselves are deliberately left where they are. SNAPPY_UNCOMPRESS_FAILED is also raised by SnappyFramedReadBuffer and HadoopSnappyReadBuffer, which read stored files — there a 500 is exactly right, because the failure is the server's. So only the two handlers that decode a request body retag their own failure as BAD_ARGUMENTS, which is already what the protobuf parse of that same body raises one line further down.

The message ended in a colon before a reason that never came: snappy::Uncompress returns a bare bool and offers no error string, so there was nothing to put after it. It now names what was attempted instead — the size of the block, and the uncompressed length the block's own header declares via snappy::GetUncompressedLength, which is the vocabulary SnappyFramedReadBuffer beside it already uses. On a body that is not snappy at all the numbers say so at a glance: a 63 byte block declares 10 uncompressed bytes.

Found while testing #117170 against real scraped Prometheus data; it reproduces identically on a plain TimeSeries table, so it is not that PR's.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/120938]

Version info

  • Merged into: 26.10.1.466-master (included in 26.10 and later)

…as wrong

A remote-write or remote-read body the server cannot decompress was answered
with a 500, because SNAPPY_UNCOMPRESS_FAILED and ZSTD_DECODER_FAILED are not on
the 4xx list. A sender treats a 5xx as retryable, so Prometheus resent a body it
had encoded wrongly and could never fix, for as long as it kept it.

The codes themselves stay where they are: the same snappy code is raised for
reading a stored file, where a server-side failure is exactly what a 500 means.
Only the two handlers that decode a request body retag their own failure as
BAD_ARGUMENTS, which is already what the protobuf parse of that same body
raises one line further down.

The message it carries said "snappy uncompress failed: " and stopped, with a
colon before a reason the library never supplies: snappy::Uncompress answers
with a bool and nothing else. It now names what was attempted instead, the size
of the block and the uncompressed length the block's own header declares, which
is the vocabulary the framed reader beside it already uses.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QtEKPyQDMCBZfJkiJcs789
@clickhouse-gh clickhouse-gh Bot added comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... external Author is not a member of the ClickHouse organization labels Sep 19, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Sep 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2c9a9c3]

Summary: ✅


AI Review

Summary

This PR changes Prometheus remote-read / remote-write body-decode failures from 500 to 400 and improves the snappy error text. The status-code remapping looks correct and the missing path coverage was added, but the new tests still do not pin the new snappy diagnostic that the PR now advertises, so that user-visible part can regress silently.

PR Metadata
  • Changelog category should be Bug Fix, not Improvement: this is user-visible misbehavior in an existing stable feature.
  • The current Changelog entry overclaims for zstd: only the snappy path reports the block size and declared uncompressed length. Exact replacement:
    A Prometheus remote-write or remote-read request whose body cannot be decompressed is now answered with 400 instead of 500. Snappy decode failures now also report the compressed block size and the uncompressed length it declares.
Tests
  • ⚠️ [tests/integration/test_prometheus_protocols/test_write_read.py:232,248] The new regression tests only assert the outer Cannot decode the request body wrapper. That proves the 400, but it does not prove the new snappy-specific diagnostic introduced in SnappyBasicReadBuffer; both snappy paths would still stay green if that message regressed later. Add one representative inner-fragment assertion such as declares / uncompressed bytes on the snappy write case and the snappy read case.
Final Verdict
  • Status: ⚠️ Request changes
  • Add a snappy-specific assertion to the malformed-body tests so the new user-visible diagnostic is actually covered. The Changelog category / entry should also be adjusted as noted above.

LLVM Coverage Report

Measured on commit 2c9a9c3.

Metric Baseline Current Δ
Lines 88.00% 88.00% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 23/29 (79.31%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 19, 2026
Comment thread src/Server/PrometheusRequestHandler.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2c9a9c35a with master c22d746de (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

8 translation units recompiled, 12 s compile time in total, 8 of them have a recent master baseline.

Job report

The status of an undecodable request body is a user-visible contract and had no
test: the suite covered the happy paths and the header refusals only, so the
`500` this commit's parent replaced could come back the next time the handler or
the HTTP exception mapping moved.

One test per affected path, each sending a body that is not what its
`Content-Encoding` claims: remote write under `snappy` and under `zstd`, and
remote read under `snappy`. Each asserts the `400` and the part of the message
that says the body could not be decoded. The body is posted verbatim rather than
through the helpers, which would compress it on the way out.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QtEKPyQDMCBZfJkiJcs789
@valerypetrov

Copy link
Copy Markdown
Contributor Author

Should be ready for review.

with a 5xx, which Prometheus retries for as long as it keeps the batch."""
response = post_undecodable_body("/write", content_encoding)
assert response.status_code == requests.codes.bad_request, response.text
assert "Cannot decode the request body" in response.text

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.

This locks in the 400, but not the new snappy diagnostic the PR is also introducing. Both snappy paths would still stay green if SnappyBasicReadBuffer regressed back to a generic decompression message, because the assertion only checks the outer Cannot decode the request body wrapper. Please add one snappy-specific fragment here and in test_remote_read_undecodable_body (for example declares / uncompressed bytes) so the user-visible message this change advertises is actually covered.

@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

PromQL Compliance Report

Baseline: S3 master 8842ee48d

Metric Baseline Current Δ
Score 96.47% 96.47% +0.00%
Passed 520 520 +0
Failed 3 3 +0
Unsupported 16 16 +0

Note: score is unchanged vs baseline.

Uploaded compliance JSON for this PR commit

@nikitamikhaylov nikitamikhaylov self-assigned this Sep 22, 2026
@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Sep 22, 2026
Merged via the queue into ClickHouse:master with commit 365470f Sep 22, 2026
176 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... external Author is not a member of the ClickHouse organization pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants