fix(query_range): skip data field on errors (#5520)

* fix: skip serializing PrometheusResponse when None

* fix: fix unit test

* chore: clippy
This commit is contained in:
Weny Xu
2025-02-13 13:32:24 +09:00
committed by GitHub
parent 954310f917
commit 63d5a69a31
3 changed files with 9 additions and 7 deletions

View File

@@ -95,7 +95,7 @@ pub struct PromData {
pub result: PromQueryResult,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum PrometheusResponse {
PromData(PromData),
@@ -106,6 +106,8 @@ pub enum PrometheusResponse {
BuildInfo(OwnedBuildInfo),
#[serde(skip_deserializing)]
ParseResult(promql_parser::parser::Expr),
#[default]
None,
}
impl PrometheusResponse {
@@ -144,11 +146,9 @@ impl PrometheusResponse {
}
}
}
}
impl Default for PrometheusResponse {
fn default() -> Self {
PrometheusResponse::PromData(Default::default())
pub fn is_none(&self) -> bool {
matches!(self, PrometheusResponse::None)
}
}

View File

@@ -42,6 +42,8 @@ use crate::http::prometheus::{
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct PrometheusJsonResponse {
pub status: String,
#[serde(skip_serializing_if = "PrometheusResponse::is_none")]
#[serde(default)]
pub data: PrometheusResponse,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
@@ -90,7 +92,7 @@ impl PrometheusJsonResponse {
{
PrometheusJsonResponse {
status: "error".to_string(),
data: PrometheusResponse::default(),
data: PrometheusResponse::None,
error: Some(reason.into()),
error_type: Some(error_type.to_string()),
warnings: None,

View File

@@ -766,7 +766,7 @@ pub async fn test_prom_http_api(store_type: StorageType) {
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
let data = res.text().await;
let expected = "{\"status\":\"error\",\"data\":{\"resultType\":\"\",\"result\":[]},\"error\":\"invalid promql query\",\"errorType\":\"InvalidArguments\"}";
let expected = "{\"status\":\"error\",\"error\":\"invalid promql query\",\"errorType\":\"InvalidArguments\"}";
assert_eq!(expected, data);
// range_query with __name__ not-equal matcher