feat: handle PromQL HTTP API parameters (#985)

* feat: impl EvalStmt parser

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* fix compile errors

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* update test result

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* add integration test

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* fix clippy

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* resolve CR comments

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* impl From<PromqlQuery> for PromQuery

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* move format into with_context

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* update test result

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* shorthand compound error

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* use rfc3339 error to report float parsing error

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* remove CompoundError

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

---------

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2023-02-15 17:15:44 +08:00
committed by GitHub
parent 5d1f231004
commit dfe7bfb07f
17 changed files with 270 additions and 46 deletions

View File

@@ -21,7 +21,7 @@ use common_recordbatch::RecordBatches;
use common_telemetry::logging::info;
use common_telemetry::timer;
use datatypes::schema::Schema;
use query::parser::{QueryLanguageParser, QueryStatement};
use query::parser::{PromQuery, QueryLanguageParser, QueryStatement};
use servers::error as server_error;
use servers::promql::PromqlHandler;
use servers::query_handler::sql::SqlQueryHandler;
@@ -170,7 +170,11 @@ impl Instance {
self.execute_stmt(stmt, query_ctx).await
}
pub async fn execute_promql(&self, promql: &str, query_ctx: QueryContextRef) -> Result<Output> {
pub async fn execute_promql(
&self,
promql: &PromQuery,
query_ctx: QueryContextRef,
) -> Result<Output> {
let stmt = QueryLanguageParser::parse_promql(promql).context(ExecuteSqlSnafu)?;
self.execute_stmt(stmt, query_ctx).await
}
@@ -185,7 +189,13 @@ impl Instance {
lookback: Duration,
query_ctx: QueryContextRef,
) -> Result<Output> {
let mut stmt = QueryLanguageParser::parse_promql(promql).context(ExecuteSqlSnafu)?;
let query = PromQuery {
query: promql.to_string(),
start: "0".to_string(),
end: "0".to_string(),
step: "5m".to_string(),
};
let mut stmt = QueryLanguageParser::parse_promql(&query).context(ExecuteSqlSnafu)?;
match &mut stmt {
QueryStatement::Sql(_) => unreachable!(),
QueryStatement::Promql(eval_stmt) => {
@@ -243,7 +253,7 @@ impl SqlQueryHandler for Instance {
async fn do_promql_query(
&self,
query: &str,
query: &PromQuery,
query_ctx: QueryContextRef,
) -> Vec<Result<Output>> {
let _timer = timer!(metric::METRIC_HANDLE_PROMQL_ELAPSED);
@@ -282,12 +292,18 @@ impl SqlQueryHandler for Instance {
#[async_trait]
impl PromqlHandler for Instance {
async fn do_query(&self, query: &str) -> server_error::Result<Output> {
async fn do_query(&self, query: &PromQuery) -> server_error::Result<Output> {
let _timer = timer!(metric::METRIC_HANDLE_PROMQL_ELAPSED);
self.execute_promql(query, QueryContext::arc())
.await
.map_err(BoxedError::new)
.with_context(|_| server_error::ExecuteQuerySnafu { query })
.with_context(|_| {
let query_literal = format!("{query:?}");
server_error::ExecuteQuerySnafu {
query: query_literal,
}
})
}
}