Skip to content

feat(promql): Allow Prometheus handlers to target TimeSeries tables via HTTP headers. - #104902

Closed
JTCunning wants to merge 8 commits into
ClickHouse:masterfrom
JTCunning:feature/prometheus-timeseries-http-headers
Closed

JTCunning wants to merge 8 commits into
ClickHouse:masterfrom
JTCunning:feature/prometheus-timeseries-http-headers

Conversation

@JTCunning

@JTCunning JTCunning commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Prometheus http_handlers rules that omit a <table> now resolve the target TimeSeries table per request, adding the X-ClickHouse-Table HTTP header as a resolution source.

This builds on #104975, which added ?database=&table= URL query-parameter targeting for the Prometheus HTTP handlers on the main HTTP port. The only addition here is the header transport.

Table-name precedence:

  1. XML <table> config (pinned; a conflicting query parameter still errors)
  2. ?table= URL query parameter
  3. X-ClickHouse-Table HTTP header (lowest priority)

The database is taken from the <database> config or the ?database= query parameter. The X-ClickHouse-Table header is consulted only for a table still unset by both the configuration and the query parameter. Its value is sanitized (ASCII control characters removed) to prevent CRLF header injection, and it never errors on a conflict with a pinned value (this header is often set globally by clients/proxies). The table name may be a qualified database.table from any source; if no database is resolved otherwise it is taken from the qualified name, falling back to default. When nothing resolves the table the request fails with a clear error — there is no implicit default table.

The prometheus_api_v1 handler and this resolution order are documented under the http_handlers server-configuration section, and the behavior is covered by integration tests in test_prometheus_protocols.

Changelog category (leave one):

  • Experimental Feature

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

Prometheus protocol HTTP handlers can now resolve the target TimeSeries table from the X-ClickHouse-Table request header (which may be a qualified database.table) when no table is pinned in the handler configuration.

@JTCunning JTCunning changed the title Allow Prometheus handlers to target TimeSeries tables via HTTP headers. feat(promql): Allow Prometheus handlers to target TimeSeries tables via HTTP headers. May 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9817b9a]

Summary: ❌

job_name test_name status info comment
Stress test (amd_debug) FAIL
Logical error: 'Inconsistent AST formatting: the query: (STID: 1941-26fa) FAIL cidb
Stress test (arm_msan) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue
Stress test (arm_ubsan) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue
AST fuzzer (amd_debug) FAIL
Logical error: Block structure mismatch in A stream: different columns: (STID: 0993-2cc2) FAIL cidb, issue

AI Review

Summary

This PR extends Prometheus HTTP handler TimeSeries target resolution so handlers without a pinned <table> can use the lower-priority X-ClickHouse-Table header, updates the http_handlers documentation, and adds focused integration coverage for write, read, query API, missing-target, qualified-header, and pinned-table behavior. I found no unresolved correctness, safety, or compatibility issues in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label May 14, 2026
Comment thread src/Server/PrometheusRequestHandler.cpp Resolved
Comment thread src/Server/PrometheusRequestHandler.cpp Resolved
pull Bot pushed a commit to infdahai/ClickHouse that referenced this pull request May 17, 2026
…formed_json

Both tests in test_storage_kafka/test_batch_fast.py wait for the
Kafka 'Committed offset 128' log line and then assert the destination
row counts immediately:

    instance.wait_for_log_line(f'{kafka_table}.*Committed offset 128')
    assert TSV(instance.query(f'SELECT count() FROM test.{kafka_table}_data'))
        == TSV('...')
    assert TSV(instance.query(f'SELECT count() FROM test.{kafka_table}_errors'))
        == TSV('64')

The offset commit and the materialized-view INSERT into the destination
MergeTree are not strictly ordered: the consumer side can log
'Committed offset 128' before the last MV block has finished flushing,
so the immediate SELECT undercounts (typically by exactly the last
block, i.e. assert 54 == 64).

Across both parametrized variants of each test the family has hit 23+
failures across 5 distinct unrelated PRs in the last 30 days (e.g.
ClickHouse#96886, ClickHouse#104902, ClickHouse#104905), with 0 master failures. Surfaced again by
@alexey-milovidov on
ClickHouse#104905 (comment).

The helper file in the same suite already acknowledges this anti-pattern
(tests/integration/helpers/kafka/common.py:463):
"Since everything is async and shaky when receiving messages from
Kafka, we may want to try and check results multiple times in a loop."

Most other tests in test_batch_fast.py already use query_with_retry
with a check_callback for exactly this reason (see e.g. line 1053,
1516). These two tests were the outliers.

Replace the bare assert with query_with_retry + check_callback. The
asserted values are unchanged, so no test intent is weakened: if the
count never reaches the expected value, query_with_retry returns the
last observed result and the surrounding assert ... == EXPECTED fails
loudly.
Prometheus handlers configured without a pinned <table> now resolve the
target TimeSeries table from the X-ClickHouse-Database / X-ClickHouse-Table
HTTP headers, in addition to the existing ?database=&table= URL query
parameters (added in ClickHouse#104975).

Per-field precedence (database and table resolved independently):
  1. XML <table> config (pinned; a conflicting query param still errors)
  2. ?database= / ?table= URL query parameters
  3. X-ClickHouse-Database / X-ClickHouse-Table headers (lowest priority)

Header values are consulted only when a name is left unset by both the
configuration and the query parameters, are sanitized (ASCII control
characters removed) to prevent CRLF header injection, and never error on
a conflict with a pinned value. When nothing resolves the table the
request fails with a clear error (no implicit default table).
@JTCunning
JTCunning force-pushed the feature/prometheus-timeseries-http-headers branch from 6d4c8eb to 99decd8 Compare June 23, 2026 19:21
The factory doc comment referenced the XML table element with bare angle
brackets, which clang parses as an unclosed HTML tag and rejects under
-Werror,-Wdocumentation-html. Reword to plain text.
@JTCunning JTCunning added the can be tested Allows running workflows for external contributors label Jun 23, 2026
…ence

Address clickhouse-gh AI Review: prove the changed QueryImpl path resolves
its TimeSeries table from the X-ClickHouse-Database / X-ClickHouse-Table
headers, and that a handler with a pinned table in config ignores
conflicting targeting headers.

- Thread extra_headers through the Prometheus HTTP query-API test helpers.
- Add a deferred prometheus_api_v1 handler (no table element) to the test
  config so the query path resolves its target per request.
- Add test_deferred_handler_query_api_targets_table_from_headers and
  test_pinned_handler_ignores_targeting_headers.
Comment thread src/Server/PrometheusRequestHandler.cpp Resolved
…se header

A qualified `?table=db.t` query parameter supplied its own database, but
getTimeSeriesTableID() filled the database from the lower-priority
X-ClickHouse-Database header before splitting the qualified table name.
The qualified split was then skipped (database already set), so the
target resolved to `<header_db>."db.t"` instead of `db.t` -- a lookup of
a non-existent table that fails the request.

Split a table supplied by the configuration or the query parameter into
database and table before consulting the header, so a qualified table
name provides its own database. Header resolution remains the
lowest-priority source for names still unset.

Adds an integration regression test
(test_deferred_handler_qualified_table_param_ignores_database_header)
that writes via a deferred handler with `?table=default.hdr_target` and a
conflicting `X-ClickHouse-Database: otherdb` header, asserting the sample
lands in default.hdr_target and otherdb.hdr_target is untouched.
…resolution

Document the `prometheus_metrics`, `prometheus_write`, `prometheus_read`,
`prometheus_query` and `prometheus_api_v1` handler types and the per-request
`TimeSeries` table resolution order (handler configuration, then the
`database`/`table` URL query parameters, then the `X-ClickHouse-Database`/
`X-ClickHouse-Table` HTTP headers) in the `http_handlers` server-configuration
section.
…ckHouse-Table

The Prometheus handler now resolves the TimeSeries table from the config,
the ?database=/?table= URL query parameters, or the X-ClickHouse-Table HTTP
header only. A qualified database.table in the table header (or any source)
supplies its own database, so the separate X-ClickHouse-Database header was
redundant for targeting; dropping it also removes the split-before-header
edge case and avoids repurposing the standard, often-globally-set
X-ClickHouse-Database header. Docs, the factory comment, and the
test_prometheus_protocols integration tests are updated to match.
@clickhouse-gh

clickhouse-gh Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.30% -0.10%
Functions 92.60% 92.60% +0.00%
Branches 77.60% 77.60% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 28/28 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Dear @JTCunning, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@abashkeev abashkeev added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label Sep 4, 2026
@JTCunning JTCunning closed this Sep 13, 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... pr-experimental Experimental Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants