Skip to content

Commit

Permalink
Merge branch 'main' into project-scoped-oxql
Browse files Browse the repository at this point in the history
# Conflicts:
#	nexus/tests/integration_tests/metrics.rs
  • Loading branch information
david-crespo committed Dec 3, 2024
2 parents 683df3e + 9285a7c commit 20a7147
Showing 1 changed file with 66 additions and 12 deletions.
78 changes: 66 additions & 12 deletions nexus/tests/integration_tests/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,28 +305,67 @@ pub async fn timeseries_query(
cptestctx: &ControlPlaneTestContext<omicron_nexus::Server>,
query: impl ToString,
) -> Vec<oxql_types::Table> {
execute_timeseries_query(cptestctx, "/v1/system/timeseries/query", query)
.await
timeseries_query_until_success(
cptestctx,
"/v1/system/timeseries/query",
query,
)
.await
}

pub async fn project_timeseries_query(
cptestctx: &ControlPlaneTestContext<omicron_nexus::Server>,
project: &str,
query: impl ToString,
) -> Vec<oxql_types::Table> {
execute_timeseries_query(
timeseries_query_until_success(
cptestctx,
&format!("/v1/timeseries/query?project={}", project),
query,
)
.await
}

async fn execute_timeseries_query(
/// Run an OxQL query until it succeeds or panics.
async fn timeseries_query_until_success(
cptestctx: &ControlPlaneTestContext<omicron_nexus::Server>,
endpoint: &str,
query: impl ToString,
) -> Vec<oxql_types::Table> {
const POLL_INTERVAL: Duration = Duration::from_secs(1);
const POLL_MAX: Duration = Duration::from_secs(30);
let query_ = query.to_string();
wait_for_condition(
|| async {
match execute_timeseries_query(cptestctx, endpoint, &query_).await {
Some(r) => Ok(r),
None => Err(CondCheckError::<()>::NotYet),
}
},
&POLL_INTERVAL,
&POLL_MAX,
)
.await
.unwrap_or_else(|_| {
panic!(
"Timeseries named in query are not available \
after {:?}, query: '{}'",
POLL_MAX,
query.to_string(),
)
})
}

/// Run an OxQL query.
///
/// This returns `None` if the query resulted in client error and the body
/// indicates that a timeseries named in the query could not be found. In all
/// other cases, it either succeeds or panics.
pub async fn execute_timeseries_query(
cptestctx: &ControlPlaneTestContext<omicron_nexus::Server>,
endpoint: &str,
query: impl ToString,
) -> Option<Vec<oxql_types::Table>> {
// first, make sure the latest timeseries have been collected.
cptestctx
.oximeter
Expand All @@ -353,14 +392,29 @@ async fn execute_timeseries_query(
.unwrap_or_else(|e| {
panic!("timeseries query failed: {e:?}\nquery: {query}")
});
rsp.parsed_body::<OxqlQueryResult>()
.unwrap_or_else(|e| {
panic!(
"could not parse timeseries query response: {e:?}\n\
query: {query}\nresponse: {rsp:#?}"
);
})
.tables

// Check for a timeseries-not-found error specifically.
if rsp.status.is_client_error() {
let text = std::str::from_utf8(&rsp.body)
.expect("Timeseries query response body should be UTF-8");
if text.contains("Schema for timeseries") && text.contains("not found")
{
return None;
}
}

// Try to parse the query as usual, which will fail on other kinds of
// errors.
Some(
rsp.parsed_body::<OxqlQueryResult>()
.unwrap_or_else(|e| {
panic!(
"could not parse timeseries query response: {e:?}\n\
query: {query}\nresponse: {rsp:#?}"
);
})
.tables,
)
}

#[nexus_test]
Expand Down

0 comments on commit 20a7147

Please sign in to comment.