Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,6 +12,7 @@
import io.netty.handler.codec.compression.Snappy;

import org.apache.http.HttpHeaders;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.elasticsearch.client.Request;
Expand All @@ -30,6 +31,7 @@
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
Expand Down Expand Up @@ -135,6 +137,25 @@ public void testGetWithMatchSelectorFiltersValues() throws Exception {
assertThat(values, not(hasItem("other_job")));
}

public void testGetWithMultipleMatchSelectorsReturnsCombinedValues() throws Exception {
writeMetric("multi_selector_metric_a", Map.of("job", "multi_job_a"));
writeMetric("multi_selector_metric_b", Map.of("job", "multi_job_b"));
writeMetric("multi_selector_metric_c", Map.of("job", "multi_job_c")); // must not appear in results

// Use URIBuilder to send two match[] selectors in a single request, working around the
// test client's single-value-per-key restriction on Request.addParameter.
Request request = new Request(
"GET",
new URIBuilder("/_prometheus/api/v1/label/job/values").addParameter("match[]", "multi_selector_metric_a")
.addParameter("match[]", "multi_selector_metric_b")
.build()
.toString()
);
List<String> values = labelValuesData(client().performRequest(request));

assertThat(values, containsInAnyOrder("multi_job_a", "multi_job_b"));
}

public void testGetValuesAreSorted() throws Exception {
writeMetric("sorted_gauge", Map.of("job", "zebra"));
writeMetric("sorted_gauge", Map.of("job", "alpha"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.netty.handler.codec.compression.Snappy;

import org.apache.http.HttpHeaders;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.elasticsearch.client.Request;
Expand Down Expand Up @@ -135,6 +136,26 @@ public void testGetWithMatchSelectorFiltersToMatchingLabels() throws Exception {
assertThat(data, hasItem("unique_label"));
}

@SuppressWarnings("unchecked")
public void testGetWithMultipleMatchSelectorsReturnsCombinedLabels() throws Exception {
writeMetric("multi_labels_metric_a", Map.of("label_only_in_a", "value_a"));
writeMetric("multi_labels_metric_b", Map.of("label_only_in_b", "value_b"));
writeMetric("multi_labels_metric_c", Map.of("label_only_in_c", "value_c")); // must not appear in results

// Use URIBuilder to send two match[] selectors in a single request, working around the
// test client's single-value-per-key restriction on Request.addParameter.
Request request = new Request(
"GET",
new URIBuilder("/_prometheus/api/v1/labels").addParameter("match[]", "multi_labels_metric_a")
.addParameter("match[]", "multi_labels_metric_b")
.build()
.toString()
);
List<String> data = (List<String>) entityAsMap(client().performRequest(request)).get("data");

assertThat(data, containsInAnyOrder("__name__", "label_only_in_a", "label_only_in_b"));
}

/** Builds a labels request with optional {@code match[]} parameters. */
private static Request labelsRequest(String... matchers) {
Request request = new Request("GET", "/_prometheus/api/v1/labels");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.netty.handler.codec.compression.Snappy;

import org.apache.http.HttpHeaders;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
Expand All @@ -31,6 +32,7 @@
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
Expand Down Expand Up @@ -143,12 +145,28 @@ public void testSeriesWithIndexPattern() throws Exception {
assertThat(data.getFirst().get("__name__"), equalTo("test_gauge_idx"));
}

public void testGetWithMultipleMatchSelectorsReturnsCombinedSeries() throws Exception {
writeMetric("multi_series_selector_a", Map.of("job", "job_a"));
writeMetric("multi_series_selector_b", Map.of("job", "job_b"));
writeMetric("multi_series_selector_c", Map.of("job", "job_c")); // must not appear in results

// Use URIBuilder to send two match[] selectors in a single request, working around the
// test client's single-value-per-key restriction on Request.addParameter.
Request request = new Request(
"GET",
new URIBuilder("/_prometheus/api/v1/series").addParameter("match[]", "multi_series_selector_a")
.addParameter("match[]", "multi_series_selector_b")
.build()
.toString()
);
List<Map<String, Object>> data = seriesData(client().performRequest(request));

List<String> names = data.stream().map(s -> (String) s.get("__name__")).toList();
assertThat(names, containsInAnyOrder("multi_series_selector_a", "multi_series_selector_b"));
}

// Helpers

/**
* Builds a series request with a single {@code match[]} parameter.
* TODO: support multiple {@code match[]} values once multi-value query param support lands.
*/
private static Request seriesRequest(String matcher) {
return seriesRequest(null, matcher);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RequestParams;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.Scope;
import org.elasticsearch.rest.ServerlessScope;
Expand Down Expand Up @@ -73,9 +74,10 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
String labelName = PrometheusLabelNameUtils.decodeLabelName(rawName);
String index = request.param(INDEX_PARAM, "*");

// TODO: support multiple match[] selectors once multi-value param support is added
String matchSelector = request.param(MATCH_PARAM);
List<String> matchSelectors = matchSelector != null ? List.of(matchSelector) : List.of();
// Consume the parameter; re-parse from the raw URI to handle repeated match[] params,
// since request processing currently keeps only the last value for repeated parameters.
request.repeatedParamAsList(MATCH_PARAM);
List<String> matchSelectors = RequestParams.fromUri(request.uri()).getAll(MATCH_PARAM);

// Time range
String endParam = request.param(END_PARAM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RequestParams;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.Scope;
import org.elasticsearch.rest.ServerlessScope;
Expand Down Expand Up @@ -59,8 +60,10 @@ public List<Route> routes() {

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
String matchParam = request.param(MATCH_PARAM);
List<String> matchSelectors = matchParam != null ? List.of(matchParam) : List.of();
// Consume the parameter; re-parse from the raw URI to handle repeated match[] params,
// since request processing currently keeps only the last value for repeated parameters.
request.repeatedParamAsList(MATCH_PARAM);
List<String> matchSelectors = RequestParams.fromUri(request.uri()).getAll(MATCH_PARAM);

// Time range
String endParam = request.param(END_PARAM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RequestParams;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.Scope;
import org.elasticsearch.rest.ServerlessScope;
Expand Down Expand Up @@ -57,12 +58,13 @@ public List<Route> routes() {

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
// TODO: support multiple match[] values once multi-value query param support lands
String matchSelector = request.param(MATCH_PARAM);
if (matchSelector == null) {
// Consume the parameter; re-parse from the raw URI to handle repeated match[] params,
// since request processing currently keeps only the last value for repeated parameters.
request.repeatedParamAsList(MATCH_PARAM);
List<String> matchSelectors = RequestParams.fromUri(request.uri()).getAll(MATCH_PARAM);
if (matchSelectors.isEmpty()) {
throw new IllegalArgumentException("At least one [match[]] selector is required");
Comment thread
felixbarny marked this conversation as resolved.
}
List<String> matchSelectors = List.of(matchSelector);

// Time range
String endParam = request.param(END_PARAM);
Expand Down
Loading