@@ -43,7 +43,7 @@ use std::pin::Pin;
4343use std:: sync:: Arc ;
4444use std:: time:: Instant ;
4545use tokio:: task:: JoinSet ;
46- use tracing:: { error, warn} ;
46+ use tracing:: { Instrument , error, warn} ;
4747
4848use crate :: event:: { DEFAULT_TIMESTAMP_KEY , commit_schema} ;
4949use crate :: metrics:: { QUERY_EXECUTE_TIME , increment_query_calls_by_date} ;
@@ -79,6 +79,8 @@ pub struct Query {
7979/// A function to execute the query and fetch QueryResponse
8080/// This won't look in the cache
8181/// TODO: Improve this function and make this a part of the query API
82+ #[ tracing:: instrument( name = "get_records_and_fields" , skip( query_request, creds, tenant_id) ) ]
83+ #[ allow( clippy:: type_complexity) ]
8284pub async fn get_records_and_fields (
8385 query_request : & Query ,
8486 creds : & SessionKey ,
@@ -115,6 +117,7 @@ pub async fn get_records_and_fields(
115117 Ok ( ( Some ( records) , Some ( fields) ) )
116118}
117119
120+ #[ tracing:: instrument( name = "query" , skip( req, query_request) , fields( otel. kind = "server" , query. sql = %query_request. query, query. streaming = query_request. streaming) ) ]
118121pub async fn query ( req : HttpRequest , query_request : Query ) -> Result < HttpResponse , QueryError > {
119122 let mut session_state = QUERY_SESSION . get_ctx ( ) . state ( ) ;
120123 let time_range =
@@ -179,6 +182,7 @@ pub async fn query(req: HttpRequest, query_request: Query) -> Result<HttpRespons
179182///
180183/// # Returns
181184/// - `HttpResponse` with the count result as JSON, including fields if requested.
185+ #[ tracing:: instrument( name = "handle_count_query" , skip( query_request, time) , fields( table = %table_name) ) ]
182186async fn handle_count_query (
183187 query_request : & Query ,
184188 table_name : & str ,
@@ -230,6 +234,10 @@ async fn handle_count_query(
230234///
231235/// # Returns
232236/// - `HttpResponse` with the full query result as a JSON object.
237+ #[ tracing:: instrument(
238+ name = "handle_non_streaming_query" ,
239+ skip( query, query_request, time, table_name, tenant_id)
240+ ) ]
233241async fn handle_non_streaming_query (
234242 query : LogicalQuery ,
235243 table_name : Vec < String > ,
@@ -283,6 +291,10 @@ async fn handle_non_streaming_query(
283291///
284292/// # Returns
285293/// - `HttpResponse` streaming the query results as NDJSON, optionally prefixed with the fields array.
294+ #[ tracing:: instrument(
295+ name = "handle_streaming_query" ,
296+ skip( query, query_request, time, table_name, tenant_id)
297+ ) ]
286298async fn handle_streaming_query (
287299 query : LogicalQuery ,
288300 table_name : Vec < String > ,
@@ -367,6 +379,7 @@ fn create_batch_processor(
367379 }
368380}
369381
382+ #[ tracing:: instrument( name = "get_counts" , skip( req, counts_request) , fields( otel. kind = "server" ) ) ]
370383pub async fn get_counts (
371384 req : HttpRequest ,
372385 counts_request : Json < CountsRequest > ,
@@ -453,6 +466,7 @@ pub async fn update_schema_when_distributed(
453466/// Create streams for querier if they do not exist
454467/// get list of streams from memory and storage
455468/// create streams for memory from storage if they do not exist
469+ #[ tracing:: instrument( name = "create_streams_for_distributed" , skip_all, fields( stream_count = streams. len( ) ) ) ]
456470pub async fn create_streams_for_distributed (
457471 streams : Vec < String > ,
458472 tenant_id : & Option < String > ,
@@ -461,19 +475,25 @@ pub async fn create_streams_for_distributed(
461475 return Ok ( ( ) ) ;
462476 }
463477 let mut join_set = JoinSet :: new ( ) ;
478+ let parent_span = tracing:: Span :: current ( ) ;
464479 for stream_name in streams {
465480 let id = tenant_id. to_owned ( ) ;
466- join_set. spawn ( async move {
467- let result = PARSEABLE
468- . create_stream_and_schema_from_storage ( & stream_name, & id)
469- . await ;
470-
471- if let Err ( e) = & result {
472- warn ! ( "Failed to create stream '{}': {}" , stream_name, e) ;
481+ let task_span =
482+ tracing:: info_span!( parent: & parent_span, "create_stream_task" , stream = %stream_name) ;
483+ join_set. spawn (
484+ async move {
485+ let result = PARSEABLE
486+ . create_stream_and_schema_from_storage ( & stream_name, & id)
487+ . await ;
488+
489+ if let Err ( e) = & result {
490+ warn ! ( "Failed to create stream '{}': {}" , stream_name, e) ;
491+ }
492+
493+ ( stream_name, result)
473494 }
474-
475- ( stream_name, result)
476- } ) ;
495+ . instrument ( task_span) ,
496+ ) ;
477497 }
478498
479499 while let Some ( result) = join_set. join_next ( ) . await {
@@ -516,6 +536,7 @@ impl FromRequest for Query {
516536 }
517537}
518538
539+ #[ tracing:: instrument( name = "into_query" , skip( query, session_state, time_range) ) ]
519540pub async fn into_query (
520541 query : & Query ,
521542 session_state : & SessionState ,
0 commit comments