@@ -25,14 +25,15 @@ use datafusion::{
2525 logical_expr:: { logical_plan:: dml:: InsertOp , LogicalPlan , Values } ,
2626 physical_plan:: execute_stream,
2727 prelude:: { cast, lit, SessionContext } ,
28+ scalar:: ScalarValue ,
2829 sql:: TableReference ,
2930} ;
3031use log:: error;
3132use tokio_stream:: StreamExt ;
3233
3334use crate :: config:: ObservabilityConfig ;
3435
35- const REQUESTS_TABLE_NAME : & ' static str = "requests" ;
36+ const REQUESTS_TABLE_NAME : & str = "requests" ;
3637
3738#[ derive( Clone , Debug ) ]
3839pub struct ObservabilityContext {
@@ -87,13 +88,15 @@ impl ObservabilityContext {
8788 let values = Values {
8889 schema,
8990 values : vec ! [ vec![
90- lit( req. sql) ,
91+ lit( ScalarValue :: Utf8 ( req. request_id) ) ,
92+ lit( req. path) ,
93+ lit( ScalarValue :: Utf8 ( req. sql) ) ,
9194 cast(
9295 lit( req. start_ms) ,
9396 DataType :: Timestamp ( TimeUnit :: Millisecond , Some ( "UTC" . into( ) ) ) ,
9497 ) ,
9598 lit( req. duration_ms) ,
96- lit( req. rows) ,
99+ lit( ScalarValue :: UInt64 ( req. rows) ) ,
97100 lit( req. status) ,
98101 ] ] ,
99102 } ;
@@ -108,7 +111,7 @@ impl ObservabilityContext {
108111 // Requires executing this stream to actually insert the request. The plan
109112 // returns the count of records inserted
110113 let mut stream = execute_stream ( res, ctx. task_ctx ( ) ) ?;
111- while let Some ( _ ) = stream. next ( ) . await { }
114+ while ( stream. next ( ) . await ) . is_some ( ) { }
112115 }
113116 Err ( e) => {
114117 error ! ( "Error recording request: {}" , e. to_string( ) )
@@ -123,31 +126,34 @@ impl ObservabilityContext {
123126
124127/// Details that will be recorded in the configured observability request table
125128pub struct ObservabilityRequestDetails {
126- pub sql : String ,
129+ pub request_id : Option < String > ,
130+ pub path : String ,
131+ pub sql : Option < String > ,
127132 pub start_ms : i64 ,
128133 pub duration_ms : i64 ,
129- pub rows : u64 ,
134+ pub rows : Option < u64 > ,
130135 pub status : u16 ,
131136}
132137
133138fn req_fields ( ) -> Vec < Field > {
134139 vec ! [
135- Field :: new( "sql" , DataType :: Utf8 , false ) ,
140+ Field :: new( "request_id" , DataType :: Utf8 , true ) ,
141+ Field :: new( "path" , DataType :: Utf8 , false ) ,
142+ Field :: new( "sql" , DataType :: Utf8 , true ) ,
136143 Field :: new(
137144 "timestamp" ,
138145 DataType :: Timestamp ( TimeUnit :: Millisecond , Some ( "UTC" . into( ) ) ) ,
139146 false ,
140147 ) ,
141148 Field :: new( "duration_ms" , DataType :: Int64 , false ) ,
142- Field :: new( "rows" , DataType :: UInt64 , false ) ,
149+ Field :: new( "rows" , DataType :: UInt64 , true ) ,
143150 Field :: new( "status" , DataType :: UInt16 , false ) ,
144151 ]
145152}
146153
147154fn create_req_schema ( ) -> Schema {
148155 let fields = req_fields ( ) ;
149- let schema = Schema :: new ( fields) ;
150- schema
156+ Schema :: new ( fields)
151157}
152158
153159#[ cfg( test) ]
@@ -183,10 +189,12 @@ mod test {
183189
184190 let ctx = execution. session_ctx ( ) ;
185191 let req = ObservabilityRequestDetails {
186- sql : "SELECT 1" . to_string ( ) ,
192+ request_id : None ,
193+ path : "/sql" . to_string ( ) ,
194+ sql : Some ( "SELECT 1" . to_string ( ) ) ,
187195 start_ms : 100 ,
188196 duration_ms : 200 ,
189- rows : 1 ,
197+ rows : Some ( 1 ) ,
190198 status : 200 ,
191199 } ;
192200
@@ -206,11 +214,11 @@ mod test {
206214 . unwrap ( ) ;
207215
208216 let expected = [
209- "+----------+--------------------------+-------------+------+--------+" ,
210- "| sql | timestamp | duration_ms | rows | status |" ,
211- "+----------+--------------------------+-------------+------+--------+" ,
212- "| SELECT 1 | 1970-01-01T00:00:00.100Z | 200 | 1 | 200 |" ,
213- "+----------+--------------------------+-------------+------+--------+" ,
217+ "+------------+------+---------- +--------------------------+-------------+------+--------+" ,
218+ "| request_id | path | sql | timestamp | duration_ms | rows | status |" ,
219+ "+------------+------+---------- +--------------------------+-------------+------+--------+" ,
220+ "| | /sql | SELECT 1 | 1970-01-01T00:00:00.100Z | 200 | 1 | 200 |" ,
221+ "+------------+------+---------- +--------------------------+-------------+------+--------+" ,
214222 ] ;
215223
216224 assert_batches_eq ! ( expected, & batches) ;
0 commit comments