Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VictoriaMetrics has an URL part in the endpoint definition while Prometheus only has scheme://host:port. Allow both #2120

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down Expand Up @@ -41,12 +41,12 @@ class PrometheusAdapter {
private static final String STEP = "step";

private final CloseableHttpClient _httpClient;
protected final HttpHost _prometheusEndpoint;
protected final URIBuilder _prometheusEndpoint;
protected final int _samplingIntervalMs;

PrometheusAdapter(CloseableHttpClient httpClient,
HttpHost prometheusEndpoint,
int samplingIntervalMs) {
URIBuilder prometheusEndpoint,
int samplingIntervalMs) {
_httpClient = validateNotNull(httpClient, "httpClient cannot be null.");
_prometheusEndpoint = validateNotNull(prometheusEndpoint, "prometheusEndpoint cannot be null.");
_samplingIntervalMs = samplingIntervalMs;
Expand All @@ -59,7 +59,7 @@ public int samplingIntervalMs() {
public List<PrometheusQueryResult> queryMetric(String queryString,
long startTimeMs,
long endTimeMs) throws IOException {
URI queryUri = URI.create(_prometheusEndpoint.toURI() + QUERY_RANGE_API_PATH);
URI queryUri = URI.create(_prometheusEndpoint.toString() + QUERY_RANGE_API_PATH);
HttpPost httpPost = new HttpPost(queryUri);

List<NameValuePair> data = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
package com.linkedin.kafka.cruisecontrol.monitor.sampling.prometheus;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpHost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.kafka.common.Cluster;
Expand Down Expand Up @@ -106,16 +107,14 @@ private void configurePrometheusAdapter(Map<String, ?> configs) {
}

try {
HttpHost host = HttpHost.create(endpoint);
if (host.getPort() < 0) {
throw new IllegalArgumentException();
}
URIBuilder uriBuilder = new URIBuilder(endpoint);

_httpClient = HttpClients.createDefault();
_prometheusAdapter = new PrometheusAdapter(_httpClient, host, _samplingIntervalMs);
} catch (IllegalArgumentException ex) {
_prometheusAdapter = new PrometheusAdapter(_httpClient, uriBuilder, _samplingIntervalMs);
} catch (URISyntaxException ex) {
throw new ConfigException(
String.format("Prometheus endpoint URI is malformed, "
+ "expected schema://host:port, provided %s", endpoint), ex);
+ "expected schema://host:port[/path], provided %s", endpoint), ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.localserver.LocalServerTestBase;
import org.apache.http.protocol.HttpContext;
Expand Down Expand Up @@ -44,7 +44,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
}
});

HttpHost httpHost = this.start();
URIBuilder httpHost = new URIBuilder(this.start().toURI());
PrometheusAdapter prometheusAdapter
= new PrometheusAdapter(this.httpclient, httpHost, SAMPLING_INTERVAL_MS);
final List<PrometheusQueryResult> prometheusQueryResults = prometheusAdapter.queryMetric(
Expand Down Expand Up @@ -136,7 +136,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
}
});

HttpHost httpHost = this.start();
URIBuilder httpHost = new URIBuilder(this.start().toURI());
PrometheusAdapter prometheusAdapter
= new PrometheusAdapter(this.httpclient, httpHost, SAMPLING_INTERVAL_MS);

Expand All @@ -156,7 +156,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
}
});

HttpHost httpHost = this.start();
URIBuilder httpHost = new URIBuilder(this.start().toURI());
PrometheusAdapter prometheusAdapter
= new PrometheusAdapter(this.httpclient, httpHost, SAMPLING_INTERVAL_MS);

Expand All @@ -176,7 +176,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
}
});

HttpHost httpHost = this.start();
URIBuilder httpHost = new URIBuilder(this.start().toURI());
PrometheusAdapter prometheusAdapter
= new PrometheusAdapter(this.httpclient, httpHost, SAMPLING_INTERVAL_MS);

Expand All @@ -196,7 +196,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
}
});

HttpHost httpHost = this.start();
URIBuilder httpHost = new URIBuilder(this.start().toURI());
PrometheusAdapter prometheusAdapter
= new PrometheusAdapter(this.httpclient, httpHost, SAMPLING_INTERVAL_MS);

Expand All @@ -216,7 +216,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
}
});

HttpHost httpHost = this.start();
URIBuilder httpHost = new URIBuilder(this.start().toURI());
PrometheusAdapter prometheusAdapter
= new PrometheusAdapter(this.httpclient, httpHost, SAMPLING_INTERVAL_MS);

Expand All @@ -236,7 +236,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
}
});

HttpHost httpHost = this.start();
URIBuilder httpHost = new URIBuilder(this.start().toURI());
PrometheusAdapter prometheusAdapter
= new PrometheusAdapter(this.httpclient, httpHost, SAMPLING_INTERVAL_MS);

Expand Down