Skip to content

feat(promql): Support prefixed /prometheus http_handlers URLs on the main HTTP port. - #104975

Merged
vitlibar merged 12 commits into
ClickHouse:masterfrom
JTCunning:prometheus-http-handlers-prefixed-urls
Jun 22, 2026
Merged

vitlibar merged 12 commits into
ClickHouse:masterfrom
JTCunning:prometheus-http-handlers-prefixed-urls

Conversation

@JTCunning

@JTCunning JTCunning commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

This allows us to handle prom endpoints on the main http port, but adds the ability to use a prefix (like /prometheus) so that we aren't establishing a pattern of someone issuing queries to clickhouse:8123/api/v1/* and establishing more of an industry standard approach to issue queries like clickhouse:8123/prometheus/api/v1/*.

Changelog category (leave one):

  • Experimental Feature

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

Allow for attaching prometheus handlers on the main http port with an optional prefix.

Version info

  • Merged into: 26.6.1.1137

@clickhouse-gh

clickhouse-gh Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [19e1823]

Summary: ✅


AI Review

Summary

This PR adds http_handlers support for Prometheus /api/v1 endpoints on the main HTTP port through a prometheus_api_v1 handler and url_prefix mounting. The implementation works for the happy-path tests that were added, but there are still unresolved correctness and user-facing contract issues around suffix-only endpoint dispatch, backward compatibility for existing query_api exact URLs, and missing official http_handlers documentation for the new handler types.

Findings

⚠️ Majors

  • [src/Server/PrometheusRequestHandler.cpp:627] [dismissed by author -- https://github.com/feat(promql): Support prefixed /prometheus http_handlers URLs on the main HTTP port. #104975#discussion_r3408576172] prometheus_api_v1 promises to dispatch the canonical Prometheus /api/v1 API, but APIv1Impl still selects WriteImpl or ReadImpl for any prefixed path that merely ends in /write or /read. With <url_prefix>/prometheus/api/v1</url_prefix>, a request such as /prometheus/api/v1/extra/api/v1/write still reaches the remote-write handler instead of the unknown-endpoint 404 path, because the current code checks only path.ends_with("/write") and path.ends_with("/read"). Suggested fix: dispatch on the path relative to the configured /api/v1 mount and require exactly /write or /read for those protocols.
  • [src/Server/PrometheusRequestHandler.cpp:467] [dismissed by author -- https://github.com/feat(promql): Support prefixed /prometheus http_handlers URLs on the main HTTP port. #104975#discussion_r3453542763] Existing query_api handlers configured on exact URLs that start with /api/v1/query but do not end exactly in /query are now rejected. For example, the old tested shape /api/v1/query_dynamic_table still passes the outer exact url filter, but QueryImpl now requires uri_path.ends_with("/query"), so it returns not_found before getTimeSeriesTableID can use the dynamic database and table parameters. Suggested fix: preserve the old exact-URL query_api behavior, or make the incompatibility explicit in docs and changelog with a migration path.
  • [docs/en/operations/server-configuration-parameters/_server_settings_outside_source.md:497] [dismissed by author -- https://github.com/feat(promql): Support prefixed /prometheus http_handlers URLs on the main HTTP port. #104975#discussion_r3408576171] The official http_handlers documentation still lists supported handler type values as only static, dynamic_query_handler, predefined_query_handler, and redirect. Users cannot discover the new prometheus_api_v1 configuration from the shipped docs, even though this PR's feature depends on that type plus url_prefix. Suggested fix: update the supported type list and include a minimal prometheus_api_v1 example.
Tests
  • ⚠️ Add negative prefixed-path tests for unknown nested endpoints such as /prometheus/api/v1/extra/api/v1/write and /prometheus/api/v1/extra/api/v1/read, proving they return the Prometheus not_found response instead of executing remote write/read.
  • ⚠️ If legacy exact query_api URLs are intended to keep working, restore a regression test for a path like /api/v1/query_dynamic_table; if not, add explicit migration coverage and documentation for the new required shape.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions: narrow prometheus_api_v1 dispatch to canonical relative /api/v1 endpoints, either preserve or explicitly document the query_api exact-URL compatibility break, and update the official http_handlers docs for the new Prometheus handler types.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label May 14, 2026
@JTCunning JTCunning added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label May 14, 2026
Comment thread src/Server/PrometheusRequestHandler.cpp Resolved
@JTCunning
JTCunning marked this pull request as ready for review May 15, 2026 04:01
@JTCunning
JTCunning requested a review from vitlibar May 15, 2026 04:01
Comment thread src/Server/HTTPHandlerFactory.cpp Outdated
|| handler_type == "expose_metrics"
|| handler_type == "remote_write"
|| handler_type == "remote_read"
|| handler_type == "query_api")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitlibar: Referring to an offline conversation, this is where I got confused. We register the prometheus handler type but none of the others.
I'm unsure if we should register every handler type as it exists today, or if we should just have a generic prometheus type that case-switches. That would allow us to end up with a single registered prefix, ie:

<handler>
  <type>prometheus</type>
  <prefix>/prometheus</table> //could be an implicit default
  <table>default.prometheus</table>
</handler>

I feel like this is what we should ultimately end up with since it cuts down on a lot of configuring in XML. I'm definitely willing to do it in a series of PRs instead of one big jump if that's how we should go about it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the right solution here is to add another handler type api_v1 here which handles all prometheus /api/v1 protocols (/api/v1/write, /api/v1/read, /api/v1/query, /api/v1/query_range, etc.):

<http_port>8123</http_port>
<http_handlers>
    <my_rule_1>
        <url>/prometheus/api/v1</url>
        <handler>
            <type>prometheus.api_v1</type>
            <table>db.time_series_table_name</table>
        </handler>
    </my_rule_1>
</http_handlers>

I'll finish this PR

@JTCunning JTCunning added the can be tested Allows running workflows for external contributors label Jun 10, 2026
Comment thread src/Server/PrometheusRequestHandler.cpp Resolved
@vitlibar
vitlibar force-pushed the prometheus-http-handlers-prefixed-urls branch 2 times, most recently from 03d2e26 to d9f51a5 Compare June 13, 2026 20:29
Comment thread src/Server/PrometheusRequestHandler.cpp Resolved
@vitlibar

Copy link
Copy Markdown
Member

Waiting for #107492

JTCunning and others added 8 commits June 22, 2026 15:36
Dispatch handler.type=prometheus rules from literal <url> path suffixes so
bare and prefixed remote write, read, and query API paths can coexist on
8123. Reject regex: <url> values at config load instead of silently
serving exposition, and route Query API sub-endpoints by path suffix.
Add integration coverage in test_prometheus_protocols.

Co-authored-by: Cursor <[email protected]>
Register remote_write, remote_read, and query_api in http_handlers and
build rules through parseHandlerConfig instead of inferring protocols
from <url> suffixes. Update integration and http_handlers configs to
use explicit types, treat prometheus as an expose_metrics alias, and
drop the regex startup test that depended on URL-based dispatch.

Co-authored-by: Cursor <[email protected]>
Restore the factory header metrics example, keep PromQL Params named
params, parse label values from the trailing /values suffix, and add
prefixed query_range integration coverage.

Co-authored-by: Cursor <[email protected]>
Target a dedicated prometheus_http TimeSeries table on 8123, add prefixed
query_range and label-values routing checks, and keep the http_handlers
prometheus handler fixture on type=prometheus.

Co-authored-by: Cursor <[email protected]>
…> prometheus_write, prometheus_query_api -> prometheus_query
@vitlibar
vitlibar force-pushed the prometheus-http-handlers-prefixed-urls branch from d9f51a5 to 64e39ba Compare June 22, 2026 14:07
@vitlibar
vitlibar force-pushed the prometheus-http-handlers-prefixed-urls branch from 64e39ba to d95c929 Compare June 22, 2026 14:26
@vitlibar
vitlibar requested a review from Copilot June 22, 2026 14:29

Copilot AI left a comment

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.

Pull request overview

Adds support for serving Prometheus HTTP API endpoints (including /api/v1/*) on the main HTTP port via http_handlers, with the ability to mount them behind a URL prefix (e.g. /prometheus/api/v1/*). This is implemented by extending the Prometheus handler configuration/types and adding an api_v1 dispatcher handler.

Changes:

  • Extend Prometheus handler type parsing to support new canonical names (metrics/write/read/query) plus deprecated aliases, and introduce an api_v1 handler type.
  • Add request dispatch logic so /api/v1/* endpoints can work both “bare” and behind a configured prefix.
  • Update integration test configs and add a new integration test validating prefixed operation on the main HTTP port.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/integration/test_prometheus_protocols/test_query_api.py Refactors imports; adjusts query API tests (but currently leaves a missing assertion).
tests/integration/test_prometheus_protocols/test_http_port.py New integration test covering prefixed /prometheus/api/v1/* on port 8123 and coexistence with bare endpoints.
tests/integration/test_prometheus_protocols/configs/prometheus.xml Updates handler type names to the new canonical identifiers (write/read/query).
tests/integration/test_prometheus_protocols/configs/http_port.xml New http_handlers config mounting Prometheus API v1 under a prefix on the main HTTP port.
src/Storages/TimeSeries/PrometheusHTTPProtocolAPI.h Updates class comment to reflect broader Prometheus HTTP API scope.
src/Storages/TimeSeries/PrometheusHTTPProtocolAPI.cpp Updates internal comment referencing the renamed query handler impl.
src/Server/PrometheusRequestHandlerFactory.h Updates documentation/examples for new handler types (contains a malformed XML example).
src/Server/PrometheusRequestHandlerFactory.cpp Adds handler type parsing/aliases and api_v1 config parsing (currently includes a compile-breaking call).
src/Server/PrometheusRequestHandlerConfig.h Adds APIv1/renames handler types; updates documentation (needs small doc correction).
src/Server/PrometheusRequestHandler.h Renames impl classes and adds APIv1Impl (needs doc comment update).
src/Server/PrometheusRequestHandler.cpp Implements suffix-based path dispatch for prefixed URLs and adds APIv1Impl dispatcher.
src/Server/HTTPHandlerFactory.cpp Routes prometheus* handler types to the Prometheus handler factory.
Comments suppressed due to low confidence (1)

tests/integration/test_prometheus_protocols/test_query_api.py:219

  • The test assigns error but never asserts anything about it, so the last check becomes a no-op and will pass even if the handler returns an unexpected error.
    error = execute_query_via_http_api(
        node.ip_address, 9093, "/api/v1/query", query, timestamp=timestamp,
        params={"database": "other"}, expect_error=True,
    )

Comment thread src/Server/PrometheusRequestHandler.h Resolved
Comment thread src/Server/PrometheusRequestHandler.cpp Resolved
@vitlibar
vitlibar force-pushed the prometheus-http-handlers-prefixed-urls branch from 9ee1c72 to 19e1823 Compare June 22, 2026 15:55
@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

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

Full report · Diff report

@vitlibar
vitlibar added this pull request to the merge queue Jun 22, 2026
Merged via the queue into ClickHouse:master with commit 2415dce Jun 22, 2026
167 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 22, 2026
@vitlibar

vitlibar commented Jun 23, 2026 •

Copy link
Copy Markdown
Member

Example:

config:

<http_port>8123</http_port>
<http_handlers>
    <my_rule_1>
        <url_prefix>/prometheus/api/v1</url_prefix>
        <handler>
            <type>prometheus_api_v1</type>
        </handler>
    </my_rule_1>
</http_handlers>

request:

https://clickhouse_host/prometheus/api/v1/query?query=up&time=1782212142&database=mydb&table=mytimeseriestable

Notes:

  • <url_prefix> is important here, because just <url>path</url> means serving only the specified path
  • type prometheus_api_v1 means all /api/v1 protocols will be served. Don't confuse it with just prometheus which is the same as prometheus_metrics and means that ClickHouse just exposes /metrics
  • database and table name can be either specified in the URL or defined in the config as before

JTCunning added a commit to JTCunning/ClickHouse that referenced this pull request Jun 23, 2026
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 added a commit to JTCunning/tsbs that referenced this pull request Jul 21, 2026
Replace the dedicated <prometheus> protocols port in the documented
server setup with a single prometheus_api_v1 http_handlers rule mounted
under a url_prefix on the main HTTP port, following
ClickHouse/ClickHouse#104975. One rule serves all /api/v1 protocols
(remote write, query, query_range), replacing the three per-endpoint
handler rules. The runner's default --urls becomes
http://localhost:8123/prometheus to match.
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 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