Add more JSONAPI spans (#1297)

<!-- The PR description should answer 2 important questions: -->

### What

Lets be a bit more granular about our spans for JSONAPI endpoint.

<img width="1508" alt="Screenshot 2024-10-29 at 16 46 32"
src="https://github.com/user-attachments/assets/97f67ae3-ac27-4dcd-84a4-083dc6ef5e67">
V3_GIT_ORIGIN_REV_ID: d5c0342c0c9dae2f50e78c7f529a54eeedb541a9
This commit is contained in:
Daniel Harvey 2024-10-30 14:19:40 +00:00 committed by hasura-bot
parent e15574de67
commit ae25f249a2
6 changed files with 113 additions and 15 deletions

View File

@ -89,8 +89,8 @@ async fn handle_rest_request(
let tracer = tracing_util::global_tracer();
let response = tracer
.in_span_async(
"handle_request",
"Handle request",
"handle_rest_request",
"Handle rest request",
SpanVisibility::User,
|| {
Box::pin(jsonapi::handler_internal(

View File

@ -6,6 +6,7 @@ use super::types::{Catalog, Model, QueryResult, RequestError, State};
use axum::http::{HeaderMap, Method, Uri};
use hasura_authn_core::Session;
use metadata_resolve::Metadata;
use tracing_util::SpanVisibility;
#[allow(clippy::unused_async)]
pub async fn handler_internal<'metadata>(
@ -18,6 +19,8 @@ pub async fn handler_internal<'metadata>(
uri: Uri,
query_string: jsonapi_library::query::Query,
) -> Result<jsonapi_library::api::DocumentData, RequestError> {
let tracer = tracing_util::global_tracer();
let state = catalog
.state_per_role
.get(&session.role)
@ -28,19 +31,38 @@ pub async fn handler_internal<'metadata>(
None => Err(RequestError::NotFound),
Some(model) => {
// create the query IR
let query_ir = parse::create_query_ir(model, &http_method, &uri, &query_string)?;
let query_ir = tracer.in_span(
"create_query_ir",
"Create query IR",
SpanVisibility::User,
|| parse::create_query_ir(model, &http_method, &uri, &query_string),
)?;
// execute the query with the query-engine
let result = query_engine_execute(
&query_ir,
metadata,
&session,
&http_context,
&request_headers,
)
.await?;
let result = tracer
.in_span_async(
"query_engine_execute",
"Execute query",
SpanVisibility::User,
|| {
Box::pin(query_engine_execute(
&query_ir,
metadata,
&session,
&http_context,
&request_headers,
))
},
)
.await?;
// process result to JSON:API compliant response
Ok(process_response::process_result(result))
tracer.in_span(
"process_response",
"Process response",
SpanVisibility::User,
|| Ok(process_response::process_result(result)),
)
}
}
}

View File

@ -66,12 +66,14 @@ pub fn create_query_ir(
let limit = query_string
.page
.as_ref()
.map(|page| usize::try_from(page.limit).unwrap());
.and_then(|page| usize::try_from(page.limit).ok())
.filter(|page| *page > 0);
let offset = query_string
.page
.as_ref()
.map(|page| usize::try_from(page.offset).unwrap());
.and_then(|page| usize::try_from(page.offset).ok())
.filter(|page| *page > 0);
// form the model selection
let model_selection = open_dds::query::ModelSelection {
@ -86,6 +88,7 @@ pub fn create_query_ir(
subgraph,
},
};
let queries = IndexMap::from_iter([(
open_dds::query::Alias::new(identifier!("jsonapi_model_query")),
open_dds::query::Query::Model(model_selection),

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,72 @@
---
source: crates/jsonapi/tests/jsonapi_golden_tests.rs
expression: result
input_file: crates/jsonapi/tests/passing/no_limits/MediaType.txt
---
DocumentData {
data: Some(
Multiple(
[
Resource {
_type: "default_MediaType",
id: "1",
attributes: {
"MediaTypeId": Number(1),
"Name": String("MPEG audio file"),
},
relationships: None,
links: None,
meta: None,
},
Resource {
_type: "default_MediaType",
id: "2",
attributes: {
"MediaTypeId": Number(2),
"Name": String("Protected AAC audio file"),
},
relationships: None,
links: None,
meta: None,
},
Resource {
_type: "default_MediaType",
id: "3",
attributes: {
"MediaTypeId": Number(3),
"Name": String("Protected MPEG-4 video file"),
},
relationships: None,
links: None,
meta: None,
},
Resource {
_type: "default_MediaType",
id: "4",
attributes: {
"MediaTypeId": Number(4),
"Name": String("Purchased AAC audio file"),
},
relationships: None,
links: None,
meta: None,
},
Resource {
_type: "default_MediaType",
id: "5",
attributes: {
"MediaTypeId": Number(5),
"Name": String("AAC audio file"),
},
relationships: None,
links: None,
meta: None,
},
],
),
),
included: None,
links: None,
meta: None,
jsonapi: None,
}

View File

@ -79,7 +79,7 @@ test *ARGS: start-docker-test-deps
watch: start-docker-test-deps start-docker-run-deps
RUST_LOG=DEBUG \
cargo watch -i "**/*.snap.new" \
-x test \
-x 'nextest run' \
-x 'clippy --no-deps' \
-x 'run --bin engine -- \
--otlp-endpoint http://localhost:4317 \