Skip to content
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 @@ -115,6 +115,9 @@ public JSONObject queryRange(
logger.debug("Received Prometheus response for query_range: code={}", response);

JSONObject jsonObject = readResponse(response);
if (!jsonObject.has("data")) {
return new JSONObject();
}
return jsonObject.getJSONObject("data");
}

Expand Down Expand Up @@ -149,6 +152,9 @@ public JSONObject query(String query, Long time, Integer limit, Integer timeout)

logger.info("Received Prometheus response for instant query: code={}", response);
JSONObject jsonObject = readResponse(response);
if (!jsonObject.has("data")) {
return new JSONObject();
}
return jsonObject.getJSONObject("data");
}

Expand All @@ -167,6 +173,9 @@ public List<String> getLabels(Map<String, String> queryParams) throws IOExceptio
Request request = new Request.Builder().url(queryUrl).build();
Response response = AccessController.doPrivilegedChecked(() -> this.prometheusHttpClient.newCall(request).execute());
JSONObject jsonObject = readResponse(response);
if (!jsonObject.has("data")) {
return new ArrayList<>();
}
return toListOfLabels(jsonObject.getJSONArray("data"));
}

Expand All @@ -182,6 +191,9 @@ public List<String> getLabel(String labelName, Map<String, String> queryParams)
Request request = new Request.Builder().url(queryUrl).build();
Response response = AccessController.doPrivilegedChecked(() -> this.prometheusHttpClient.newCall(request).execute());
JSONObject jsonObject = readResponse(response);
if (!jsonObject.has("data")) {
return new ArrayList<>();
}
return toListOfLabels(jsonObject.getJSONArray("data"));
}

Expand All @@ -196,6 +208,9 @@ public Map<String, List<MetricMetadata>> getAllMetrics(Map<String, String> query
Request request = new Request.Builder().url(queryUrl).build();
Response response = AccessController.doPrivilegedChecked(() -> this.prometheusHttpClient.newCall(request).execute());
JSONObject jsonObject = readResponse(response);
if (!jsonObject.has("data")) {
return new HashMap<>();
}
TypeReference<HashMap<String, List<MetricMetadata>>> typeRef = new TypeReference<>() {};
return new ObjectMapper().readValue(jsonObject.getJSONObject("data").toString(), typeRef);
}
Expand All @@ -215,6 +230,9 @@ public List<Map<String, String>> getSeries(Map<String, String> queryParams) thro
Request request = new Request.Builder().url(queryUrl).build();
Response response = AccessController.doPrivilegedChecked(() -> this.prometheusHttpClient.newCall(request).execute());
JSONObject jsonObject = readResponse(response);
if (!jsonObject.has("data")) {
return new ArrayList<>();
}
JSONArray dataArray = jsonObject.getJSONArray("data");
return toListOfSeries(dataArray);
}
Expand All @@ -232,6 +250,9 @@ public JSONArray queryExemplars(String query, Long start, Long end) throws IOExc
Request request = new Request.Builder().url(queryUrl).build();
Response response = AccessController.doPrivilegedChecked(() -> this.prometheusHttpClient.newCall(request).execute());
JSONObject jsonObject = readResponse(response);
if (!jsonObject.has("data")) {
return new JSONArray();
}
return jsonObject.getJSONArray("data");
}

Expand All @@ -243,6 +264,9 @@ public JSONObject getAlerts() throws IOException {
Request request = new Request.Builder().url(queryUrl).build();
Response response = AccessController.doPrivilegedChecked(() -> this.prometheusHttpClient.newCall(request).execute());
JSONObject jsonObject = readResponse(response);
if (!jsonObject.has("data")) {
return new JSONObject();
}
return jsonObject.getJSONObject("data");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,4 +1119,109 @@ public void testGetAlertmanagerStatusHttpErrorWithNullBody() throws IOException
PrometheusClientException.class, () -> nullBodyClient.getAlertmanagerStatus());
assertTrue(exception.getMessage().contains("No response body"));
}

// Prometheus-compatible backends (e.g. Cortex) may return {"status":"success"} with no
// "data" key when there is nothing to report. Each of the following tests verifies the
// corresponding client method returns an empty result instead of throwing JSONException.

@Test
public void testGetAllMetricsReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

var result = client.getAllMetrics();

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
public void testGetAllMetricsWithParamsReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

HashMap<String, String> params = new HashMap<>();
params.put("limit", "10");
var result = client.getAllMetrics(params);

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
public void testQueryRangeReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

JSONObject result = client.queryRange("up", 1435781430L, 1435781460L, "15s");

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
public void testQueryReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

JSONObject result = client.query("up", 1435781460L, 100, 30);

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
public void testGetLabelsReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

List<String> result = client.getLabels(new HashMap<>());

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
public void testGetLabelReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

List<String> result = client.getLabel("job", new HashMap<>());

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
public void testGetSeriesReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

var result = client.getSeries(new HashMap<>());

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
public void testQueryExemplarsReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

JSONArray result = client.queryExemplars("http_request_duration_seconds_bucket", 1L, 2L);

assertNotNull(result);
assertEquals(0, result.length());
}

@Test
public void testGetAlertsReturnsEmptyWhenDataFieldMissing() throws IOException {
String emptyResponse = "{\"status\":\"success\"}";
mockWebServer.enqueue(new MockResponse().setBody(emptyResponse));

JSONObject result = client.getAlerts();

assertNotNull(result);
assertTrue(result.isEmpty());
}
}