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

feat(promql): parameterize lookback #3630

Merged
5 changes: 4 additions & 1 deletion src/servers/src/http/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ pub struct PromqlQuery {
pub start: String,
pub end: String,
pub step: String,
pub lookback: Option<String>,
pub db: Option<String>,
}

Expand All @@ -215,7 +216,9 @@ impl From<PromqlQuery> for PromQuery {
start: query.start,
end: query.end,
step: query.step,
lookback: DEFAULT_LOOKBACK_STRING.to_string(),
lookback: query
.lookback
.unwrap_or(DEFAULT_LOOKBACK_STRING.to_string()),
}
}
}
Expand Down
33 changes: 28 additions & 5 deletions src/servers/src/http/prometheus.rs
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub async fn build_info_query() -> PrometheusJsonResponse {
#[derive(Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct InstantQuery {
query: Option<String>,
lookback: Option<String>,
time: Option<String>,
timeout: Option<String>,
db: Option<String>,
Expand All @@ -178,7 +179,10 @@ pub async fn instant_query(
start: time.clone(),
end: time,
step: "1s".to_string(),
lookback: "5m".to_string(),
lookback: params
.lookback
.or(form_params.lookback)
.unwrap_or(DEFAULT_LOOKBACK_STRING.to_string()),
};

let result = handler.do_query(&prom_query, query_ctx).await;
Expand All @@ -197,6 +201,7 @@ pub struct RangeQuery {
start: Option<String>,
end: Option<String>,
step: Option<String>,
lookback: Option<String>,
timeout: Option<String>,
db: Option<String>,
}
Expand All @@ -217,7 +222,10 @@ pub async fn range_query(
start: params.start.or(form_params.start).unwrap_or_default(),
end: params.end.or(form_params.end).unwrap_or_default(),
step: params.step.or(form_params.step).unwrap_or_default(),
lookback: DEFAULT_LOOKBACK_STRING.to_string(),
lookback: params
.lookback
.or(form_params.lookback)
.unwrap_or(DEFAULT_LOOKBACK_STRING.to_string()),
};

let result = handler.do_query(&prom_query, query_ctx).await;
Expand All @@ -237,6 +245,7 @@ struct Matches(Vec<String>);
pub struct LabelsQuery {
start: Option<String>,
end: Option<String>,
lookback: Option<String>,
#[serde(flatten)]
matches: Matches,
db: Option<String>,
Expand Down Expand Up @@ -312,6 +321,11 @@ pub async fn labels_query(
.or(form_params.end)
.unwrap_or_else(current_time_rfc3339);

let lookback = params
.lookback
.or(form_params.lookback)
.unwrap_or(DEFAULT_LOOKBACK_STRING.to_string());

let mut labels = HashSet::new();
let _ = labels.insert(METRIC_NAME.to_string());

Expand All @@ -322,7 +336,7 @@ pub async fn labels_query(
start: start.clone(),
end: end.clone(),
step: DEFAULT_LOOKBACK_STRING.to_string(),
lookback: DEFAULT_LOOKBACK_STRING.to_string(),
lookback: lookback.clone(),
};

let result = handler.do_query(&prom_query, query_ctx.clone()).await;
Expand Down Expand Up @@ -549,6 +563,7 @@ fn promql_expr_to_metric_name(expr: &PromqlExpr) -> Option<String> {
pub struct LabelValueQuery {
start: Option<String>,
end: Option<String>,
lookback: Option<String>,
#[serde(flatten)]
matches: Matches,
db: Option<String>,
Expand Down Expand Up @@ -590,6 +605,9 @@ pub async fn label_values_query(

let start = params.start.unwrap_or_else(yesterday_rfc3339);
let end = params.end.unwrap_or_else(current_time_rfc3339);
let lookback = params
.lookback
.unwrap_or(DEFAULT_LOOKBACK_STRING.to_string());

let mut label_values = HashSet::new();

Expand All @@ -600,7 +618,7 @@ pub async fn label_values_query(
start: start.clone(),
end: end.clone(),
step: DEFAULT_LOOKBACK_STRING.to_string(),
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
lookback: DEFAULT_LOOKBACK_STRING.to_string(),
lookback: lookback.clone(),
};
let result = handler.do_query(&prom_query, query_ctx.clone()).await;
if let Err(err) =
Expand Down Expand Up @@ -699,6 +717,7 @@ async fn retrieve_label_values_from_record_batch(
pub struct SeriesQuery {
start: Option<String>,
end: Option<String>,
lookback: Option<String>,
#[serde(flatten)]
matches: Matches,
db: Option<String>,
Expand Down Expand Up @@ -730,6 +749,10 @@ pub async fn series_query(
.end
.or(form_params.end)
.unwrap_or_else(current_time_rfc3339);
let lookback = params
.lookback
.or(form_params.lookback)
.unwrap_or(DEFAULT_LOOKBACK_STRING.to_string());

let mut series = Vec::new();
let mut merge_map = HashMap::new();
Expand All @@ -741,7 +764,7 @@ pub async fn series_query(
end: end.clone(),
// TODO: find a better value for step
step: DEFAULT_LOOKBACK_STRING.to_string(),
lookback: DEFAULT_LOOKBACK_STRING.to_string(),
lookback: lookback.clone(),
};
let result = handler.do_query(&prom_query, query_ctx.clone()).await;

Expand Down