Skip to content

Conversation

@keytouch
Copy link

@keytouch keytouch commented Dec 3, 2025

Description

In case of a prometheus server behind proxy or a prometheus compatible API (e.g. VictoriaMetrics), prometheus API URI should be appended. Similar to what has been done in: trinodb/trino@55c008b

Motivation and Context

With replacePath, query will return 404 on VictoriaMetrics (we are using its prometheus API and configured as: http://ip:port/select/1:1/prometheus) as current code base will build the URI as http://ip:port/api/v1/query

Impact

Test Plan

Release Notes

== NO RELEASE NOTE ==

In case of a prometheus server behind proxy or a prometheus compatible
API (e.g. VictoriaMetrics), prometheus API URI should be appended.
Similar to what has been done in: trinodb/trino@55c008b
@keytouch keytouch requested a review from a team as a code owner December 3, 2025 07:23
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 3, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Switches Prometheus connector HTTP URI construction to use HttpUriBuilder.appendPath so that API paths are appended to the configured base URI instead of replacing existing paths, improving compatibility with proxied/Prometheus-compatible endpoints.

Sequence diagram for Prometheus query URI construction with appendPath

sequenceDiagram
    participant PrometheusSplitManager
    participant HttpUriBuilder
    participant URIBuilder
    participant HttpClient

    PrometheusSplitManager->>HttpUriBuilder: uriBuilderFrom(baseURI)
    HttpUriBuilder-->>PrometheusSplitManager: URIBuilder

    PrometheusSplitManager->>URIBuilder: appendPath(api_v1_query)
    PrometheusSplitManager->>URIBuilder: addParameter(query, metricName_with_range)
    PrometheusSplitManager->>URIBuilder: addParameter(time, time)
    PrometheusSplitManager->>URIBuilder: build()
    URIBuilder-->>PrometheusSplitManager: queryURI

    PrometheusSplitManager->>HttpClient: execute(queryURI)
    HttpClient-->>PrometheusSplitManager: Prometheus_response
Loading

Flow diagram for Prometheus base URI composition with appendPath

graph TD
    A["Configured_prometheus_base_URI
(e.g. http://ip:port/select/1:1/prometheus)"] --> B["getPrometheusMetricsURI
PrometheusClient"]
    B --> C["HttpUriBuilder.uriBuilderFrom(prometheusUri)"]
    C --> D["appendPath(METRICS_ENDPOINT)
(e.g. /api/v1/label/__name__/values)"]
    D --> E["build()
metrics_URI
(e.g. http://ip:port/select/1:1/prometheus/api/v1/label/__name__/values)"]

    A --> F["buildQuery
PrometheusSplitManager"]
    F --> G["HttpUriBuilder.uriBuilderFrom(baseURI)"]
    G --> H["appendPath(api/v1/query)"]
    H --> I["addParameter(query, metric_with_range)"]
    I --> J["addParameter(time, time)"]
    J --> K["build()
query_URI
(e.g. http://ip:port/select/1:1/prometheus/api/v1/query)"]
Loading

File-Level Changes

Change Details Files
Use HttpUriBuilder.appendPath for metrics endpoint URI construction instead of manual URI creation.
  • Replace manual URI creation using URI constructor and string concatenation with HttpUriBuilder.uriBuilderFrom(prometheusUri).appendPath(METRICS_ENDPOINT).build()
  • Rely on HttpUriBuilder for safe path handling and encoding, eliminating the try/catch for URISyntaxException
presto-prometheus/src/main/java/com/facebook/presto/plugin/prometheus/PrometheusClient.java
Append Prometheus query API path instead of replacing the existing path on the base URI.
  • Update comment to correctly reference HttpUriBuilder as the URI encoder
  • Change URI construction for queries from replacePath("api/v1/query") to appendPath("api/v1/query") so that the configured base path is preserved
  • Maintain existing query and time parameters while changing only the path handling
presto-prometheus/src/main/java/com/facebook/presto/plugin/prometheus/PrometheusSplitManager.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • The buildQuery method no longer constructs URIs in a way that can throw URISyntaxException, so you can drop the throws URISyntaxException from its signature and simplify any call sites that were forced to handle it.
  • When switching from replacePath to appendPath, double-check that appendPath("api/v1/query") correctly handles cases where the base URI path already ends with or without a trailing slash, to avoid unintended // or missing separators in the final path.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `buildQuery` method no longer constructs URIs in a way that can throw `URISyntaxException`, so you can drop the `throws URISyntaxException` from its signature and simplify any call sites that were forced to handle it.
- When switching from `replacePath` to `appendPath`, double-check that `appendPath("api/v1/query")` correctly handles cases where the base URI path already ends with or without a trailing slash, to avoid unintended `//` or missing separators in the final path.

## Individual Comments

### Comment 1
<location> `presto-prometheus/src/main/java/com/facebook/presto/plugin/prometheus/PrometheusClient.java:96` </location>
<code_context>
     private static URI getPrometheusMetricsURI(URI prometheusUri)
     {
-        try {
-            // endpoint to retrieve metric names from Prometheus
-            return new URI(prometheusUri.getScheme(), prometheusUri.getAuthority(), prometheusUri.getPath() + METRICS_ENDPOINT, null, null);
-        }
-        catch (URISyntaxException e) {
-            throw new RuntimeException(e);
-        }
+        // endpoint to retrieve metric names from Prometheus
+        return HttpUriBuilder.uriBuilderFrom(prometheusUri).appendPath(METRICS_ENDPOINT).build();
     }

</code_context>

<issue_to_address>
**issue (bug_risk):** Preserving query/fragment from the base URI may change the effective metrics endpoint.

The old code built a URI from only scheme, authority, and `path + METRICS_ENDPOINT`, dropping any query/fragment from `prometheusUri`. The new use of `uriBuilderFrom(prometheusUri)` keeps query/fragment/user-info and then appends the metrics path, so any configured query params or fragment will now be sent to the metrics endpoint. If the intent is a clean metrics URL, consider clearing query/fragment when building it (e.g., via `replacePath` plus explicitly clearing query).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

throw new RuntimeException(e);
}
// endpoint to retrieve metric names from Prometheus
return HttpUriBuilder.uriBuilderFrom(prometheusUri).appendPath(METRICS_ENDPOINT).build();
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Preserving query/fragment from the base URI may change the effective metrics endpoint.

The old code built a URI from only scheme, authority, and path + METRICS_ENDPOINT, dropping any query/fragment from prometheusUri. The new use of uriBuilderFrom(prometheusUri) keeps query/fragment/user-info and then appends the metrics path, so any configured query params or fragment will now be sent to the metrics endpoint. If the intent is a clean metrics URL, consider clearing query/fragment when building it (e.g., via replacePath plus explicitly clearing query).

@steveburnett
Copy link
Contributor

Please update the Release Notes section of the Description so that this PR can pass the failing test. https://github.com/prestodb/presto/actions/runs/19885919869/job/56993039935?pr=26733

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.

2 participants