44
55use std:: { borrow:: Cow , sync:: Arc } ;
66
7- use http:: {
8- header:: { ACCESS_CONTROL_ALLOW_HEADERS , ACCESS_CONTROL_ALLOW_ORIGIN , CONTENT_TYPE } ,
9- HeaderValue , Method , StatusCode ,
10- } ;
11-
127use crate :: {
138 manager:: AppManager ,
149 window:: { InvokeRequest , UriSchemeProtocolHandler } ,
1510 Runtime ,
1611} ;
12+ use http:: {
13+ header:: { ACCESS_CONTROL_ALLOW_HEADERS , ACCESS_CONTROL_ALLOW_ORIGIN , CONTENT_TYPE } ,
14+ HeaderValue , Method , StatusCode ,
15+ } ;
1716
1817use super :: { CallbackFn , InvokeBody , InvokeResponse } ;
1918
@@ -29,6 +28,14 @@ pub fn message_handler<R: Runtime>(
2928
3029pub fn get < R : Runtime > ( manager : Arc < AppManager < R > > , label : String ) -> UriSchemeProtocolHandler {
3130 Box :: new ( move |request, responder| {
31+ #[ cfg( feature = "tracing" ) ]
32+ let span = tracing:: trace_span!(
33+ "ipc::request" ,
34+ kind = "custom-protocol" ,
35+ request = tracing:: field:: Empty
36+ )
37+ . entered ( ) ;
38+
3239 let manager = manager. clone ( ) ;
3340 let label = label. clone ( ) ;
3441
@@ -44,9 +51,35 @@ pub fn get<R: Runtime>(manager: Arc<AppManager<R>>, label: String) -> UriSchemeP
4451 if let Some ( window) = manager. get_window ( & label) {
4552 match parse_invoke_request ( & manager, request) {
4653 Ok ( request) => {
54+ #[ cfg( feature = "tracing" ) ]
55+ span. record (
56+ "request" ,
57+ match & request. body {
58+ InvokeBody :: Json ( j) => serde_json:: to_string ( j) . unwrap ( ) ,
59+ InvokeBody :: Raw ( b) => serde_json:: to_string ( b) . unwrap ( ) ,
60+ } ,
61+ ) ;
62+ #[ cfg( feature = "tracing" ) ]
63+ let request_span = tracing:: trace_span!( "ipc::request::handle" , cmd = request. cmd) ;
64+
4765 window. on_message (
4866 request,
4967 Box :: new ( move |_window, _cmd, response, _callback, _error| {
68+ #[ cfg( feature = "tracing" ) ]
69+ let _respond_span = tracing:: trace_span!(
70+ parent: & request_span,
71+ "ipc::request::respond"
72+ )
73+ . entered ( ) ;
74+
75+ #[ cfg( feature = "tracing" ) ]
76+ let response_span = tracing:: trace_span!(
77+ "ipc::request::response" ,
78+ response = serde_json:: to_string( & response) . unwrap( ) ,
79+ mime_type = tracing:: field:: Empty
80+ )
81+ . entered ( ) ;
82+
5083 let ( mut response, mime_type) = match response {
5184 InvokeResponse :: Ok ( InvokeBody :: Json ( v) ) => (
5285 http:: Response :: new ( serde_json:: to_vec ( & v) . unwrap ( ) . into ( ) ) ,
@@ -64,6 +97,9 @@ pub fn get<R: Runtime>(manager: Arc<AppManager<R>>, label: String) -> UriSchemeP
6497 }
6598 } ;
6699
100+ #[ cfg( feature = "tracing" ) ]
101+ response_span. record ( "mime_type" , mime_type. essence_str ( ) ) ;
102+
67103 response. headers_mut ( ) . insert (
68104 CONTENT_TYPE ,
69105 HeaderValue :: from_str ( mime_type. essence_str ( ) ) . unwrap ( ) ,
@@ -129,6 +165,10 @@ pub fn get<R: Runtime>(manager: Arc<AppManager<R>>, label: String) -> UriSchemeP
129165#[ cfg( any( target_os = "macos" , target_os = "ios" , not( ipc_custom_protocol) ) ) ]
130166fn handle_ipc_message < R : Runtime > ( message : String , manager : & AppManager < R > , label : & str ) {
131167 if let Some ( window) = manager. get_window ( label) {
168+ #[ cfg( feature = "tracing" ) ]
169+ let _span =
170+ tracing:: trace_span!( "ipc::request" , kind = "post-message" , request = message) . entered ( ) ;
171+
132172 use serde:: { Deserialize , Deserializer } ;
133173
134174 pub ( crate ) struct HeaderMap ( http:: HeaderMap ) ;
@@ -185,6 +225,9 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
185225 }
186226
187227 if let crate :: Pattern :: Isolation { crypto_keys, .. } = & * manager. pattern {
228+ #[ cfg( feature = "tracing" ) ]
229+ let _span = tracing:: trace_span!( "ipc::request::decrypt_isolation_payload" ) . entered ( ) ;
230+
188231 invoke_message. replace (
189232 serde_json:: from_str :: < IsolationMessage < ' _ > > ( & message)
190233 . map_err ( Into :: into)
@@ -201,18 +244,25 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
201244 }
202245 }
203246
204- match invoke_message
205- . unwrap_or_else ( || serde_json:: from_str :: < Message > ( & message) . map_err ( Into :: into) )
206- {
247+ match invoke_message. unwrap_or_else ( || {
248+ #[ cfg( feature = "tracing" ) ]
249+ let _span = tracing:: trace_span!( "ipc::request::deserialize" ) . entered ( ) ;
250+ serde_json:: from_str :: < Message > ( & message) . map_err ( Into :: into)
251+ } ) {
207252 Ok ( message) => {
253+ let request = InvokeRequest {
254+ cmd : message. cmd ,
255+ callback : message. callback ,
256+ error : message. error ,
257+ body : message. payload . into ( ) ,
258+ headers : message. options . map ( |o| o. headers . 0 ) . unwrap_or_default ( ) ,
259+ } ;
260+
261+ #[ cfg( feature = "tracing" ) ]
262+ let request_span = tracing:: trace_span!( "ipc::request::handle" , cmd = request. cmd) ;
263+
208264 window. on_message (
209- InvokeRequest {
210- cmd : message. cmd ,
211- callback : message. callback ,
212- error : message. error ,
213- body : message. payload . into ( ) ,
214- headers : message. options . map ( |o| o. headers . 0 ) . unwrap_or_default ( ) ,
215- } ,
265+ request,
216266 Box :: new ( move |window, cmd, response, callback, error| {
217267 use crate :: ipc:: {
218268 format_callback:: {
@@ -222,6 +272,13 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
222272 } ;
223273 use serde_json:: Value as JsonValue ;
224274
275+ #[ cfg( feature = "tracing" ) ]
276+ let _respond_span = tracing:: trace_span!(
277+ parent: & request_span,
278+ "ipc::request::respond"
279+ )
280+ . entered ( ) ;
281+
225282 // the channel data command is the only command that uses a custom protocol on Linux
226283 if window. manager . window . invoke_responder . is_none ( )
227284 && cmd != crate :: ipc:: channel:: FETCH_CHANNEL_DATA_COMMAND
@@ -240,6 +297,19 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
240297 let _ = window. eval ( & eval_js) ;
241298 }
242299
300+ #[ cfg( feature = "tracing" ) ]
301+ let _response_span = tracing:: trace_span!(
302+ "ipc::request::response" ,
303+ response = serde_json:: to_string( & response) . unwrap( ) ,
304+ mime_type = match & response {
305+ InvokeResponse :: Ok ( InvokeBody :: Json ( _) ) => mime:: APPLICATION_JSON ,
306+ InvokeResponse :: Ok ( InvokeBody :: Raw ( _) ) => mime:: APPLICATION_OCTET_STREAM ,
307+ InvokeResponse :: Err ( _) => mime:: TEXT_PLAIN ,
308+ }
309+ . essence_str( )
310+ )
311+ . entered ( ) ;
312+
243313 match & response {
244314 InvokeResponse :: Ok ( InvokeBody :: Json ( v) ) => {
245315 if !( cfg ! ( target_os = "macos" ) || cfg ! ( target_os = "ios" ) )
@@ -277,6 +347,9 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
277347 ) ;
278348 }
279349 Err ( e) => {
350+ #[ cfg( feature = "tracing" ) ]
351+ tracing:: trace!( "ipc.request.error {}" , e) ;
352+
280353 let _ = window. eval ( & format ! (
281354 r#"console.error({})"# ,
282355 serde_json:: Value :: String ( e. to_string( ) )
@@ -301,6 +374,9 @@ fn parse_invoke_request<R: Runtime>(
301374 // the body is not set if ipc_custom_protocol is not enabled so we'll just ignore it
302375 #[ cfg( all( feature = "isolation" , ipc_custom_protocol) ) ]
303376 if let crate :: Pattern :: Isolation { crypto_keys, .. } = & * manager. pattern {
377+ #[ cfg( feature = "tracing" ) ]
378+ let _span = tracing:: trace_span!( "ipc::request::decrypt_isolation_payload" ) . entered ( ) ;
379+
304380 body = crate :: utils:: pattern:: isolation:: RawIsolationPayload :: try_from ( & body)
305381 . and_then ( |raw| crypto_keys. decrypt ( raw) )
306382 . map_err ( |e| e. to_string ( ) ) ?;
@@ -334,6 +410,10 @@ fn parse_invoke_request<R: Runtime>(
334410 . map ( |mime| mime. parse ( ) )
335411 . unwrap_or ( Ok ( mime:: APPLICATION_OCTET_STREAM ) )
336412 . map_err ( |_| "unknown content type" ) ?;
413+
414+ #[ cfg( feature = "tracing" ) ]
415+ let span = tracing:: trace_span!( "ipc::request::deserialize" ) . entered ( ) ;
416+
337417 let body = if content_type == mime:: APPLICATION_OCTET_STREAM {
338418 body. into ( )
339419 } else if content_type == mime:: APPLICATION_JSON {
@@ -349,6 +429,9 @@ fn parse_invoke_request<R: Runtime>(
349429 return Err ( format ! ( "content type {content_type} is not implemented" ) ) ;
350430 } ;
351431
432+ #[ cfg( feature = "tracing" ) ]
433+ drop ( span) ;
434+
352435 let payload = InvokeRequest {
353436 cmd,
354437 callback,
0 commit comments