4
4
5
5
use std:: { borrow:: Cow , sync:: Arc } ;
6
6
7
- use http:: {
8
- header:: { ACCESS_CONTROL_ALLOW_HEADERS , ACCESS_CONTROL_ALLOW_ORIGIN , CONTENT_TYPE } ,
9
- HeaderValue , Method , StatusCode ,
10
- } ;
11
-
12
7
use crate :: {
13
8
manager:: AppManager ,
14
9
window:: { InvokeRequest , UriSchemeProtocolHandler } ,
15
10
Runtime ,
16
11
} ;
12
+ use http:: {
13
+ header:: { ACCESS_CONTROL_ALLOW_HEADERS , ACCESS_CONTROL_ALLOW_ORIGIN , CONTENT_TYPE } ,
14
+ HeaderValue , Method , StatusCode ,
15
+ } ;
17
16
18
17
use super :: { CallbackFn , InvokeBody , InvokeResponse } ;
19
18
@@ -29,6 +28,14 @@ pub fn message_handler<R: Runtime>(
29
28
30
29
pub fn get < R : Runtime > ( manager : Arc < AppManager < R > > , label : String ) -> UriSchemeProtocolHandler {
31
30
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
+
32
39
let manager = manager. clone ( ) ;
33
40
let label = label. clone ( ) ;
34
41
@@ -44,9 +51,35 @@ pub fn get<R: Runtime>(manager: Arc<AppManager<R>>, label: String) -> UriSchemeP
44
51
if let Some ( window) = manager. get_window ( & label) {
45
52
match parse_invoke_request ( & manager, request) {
46
53
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
+
47
65
window. on_message (
48
66
request,
49
67
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
+
50
83
let ( mut response, mime_type) = match response {
51
84
InvokeResponse :: Ok ( InvokeBody :: Json ( v) ) => (
52
85
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
64
97
}
65
98
} ;
66
99
100
+ #[ cfg( feature = "tracing" ) ]
101
+ response_span. record ( "mime_type" , mime_type. essence_str ( ) ) ;
102
+
67
103
response. headers_mut ( ) . insert (
68
104
CONTENT_TYPE ,
69
105
HeaderValue :: from_str ( mime_type. essence_str ( ) ) . unwrap ( ) ,
@@ -129,6 +165,10 @@ pub fn get<R: Runtime>(manager: Arc<AppManager<R>>, label: String) -> UriSchemeP
129
165
#[ cfg( any( target_os = "macos" , target_os = "ios" , not( ipc_custom_protocol) ) ) ]
130
166
fn handle_ipc_message < R : Runtime > ( message : String , manager : & AppManager < R > , label : & str ) {
131
167
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
+
132
172
use serde:: { Deserialize , Deserializer } ;
133
173
134
174
pub ( crate ) struct HeaderMap ( http:: HeaderMap ) ;
@@ -185,6 +225,9 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
185
225
}
186
226
187
227
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
+
188
231
invoke_message. replace (
189
232
serde_json:: from_str :: < IsolationMessage < ' _ > > ( & message)
190
233
. map_err ( Into :: into)
@@ -201,18 +244,25 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
201
244
}
202
245
}
203
246
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
+ } ) {
207
252
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
+
208
264
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,
216
266
Box :: new ( move |window, cmd, response, callback, error| {
217
267
use crate :: ipc:: {
218
268
format_callback:: {
@@ -222,6 +272,13 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
222
272
} ;
223
273
use serde_json:: Value as JsonValue ;
224
274
275
+ #[ cfg( feature = "tracing" ) ]
276
+ let _respond_span = tracing:: trace_span!(
277
+ parent: & request_span,
278
+ "ipc::request::respond"
279
+ )
280
+ . entered ( ) ;
281
+
225
282
// the channel data command is the only command that uses a custom protocol on Linux
226
283
if window. manager . window . invoke_responder . is_none ( )
227
284
&& cmd != crate :: ipc:: channel:: FETCH_CHANNEL_DATA_COMMAND
@@ -240,6 +297,19 @@ fn handle_ipc_message<R: Runtime>(message: String, manager: &AppManager<R>, labe
240
297
let _ = window. eval ( & eval_js) ;
241
298
}
242
299
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
+
243
313
match & response {
244
314
InvokeResponse :: Ok ( InvokeBody :: Json ( v) ) => {
245
315
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
277
347
) ;
278
348
}
279
349
Err ( e) => {
350
+ #[ cfg( feature = "tracing" ) ]
351
+ tracing:: trace!( "ipc.request.error {}" , e) ;
352
+
280
353
let _ = window. eval ( & format ! (
281
354
r#"console.error({})"# ,
282
355
serde_json:: Value :: String ( e. to_string( ) )
@@ -301,6 +374,9 @@ fn parse_invoke_request<R: Runtime>(
301
374
// the body is not set if ipc_custom_protocol is not enabled so we'll just ignore it
302
375
#[ cfg( all( feature = "isolation" , ipc_custom_protocol) ) ]
303
376
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
+
304
380
body = crate :: utils:: pattern:: isolation:: RawIsolationPayload :: try_from ( & body)
305
381
. and_then ( |raw| crypto_keys. decrypt ( raw) )
306
382
. map_err ( |e| e. to_string ( ) ) ?;
@@ -334,6 +410,10 @@ fn parse_invoke_request<R: Runtime>(
334
410
. map ( |mime| mime. parse ( ) )
335
411
. unwrap_or ( Ok ( mime:: APPLICATION_OCTET_STREAM ) )
336
412
. map_err ( |_| "unknown content type" ) ?;
413
+
414
+ #[ cfg( feature = "tracing" ) ]
415
+ let span = tracing:: trace_span!( "ipc::request::deserialize" ) . entered ( ) ;
416
+
337
417
let body = if content_type == mime:: APPLICATION_OCTET_STREAM {
338
418
body. into ( )
339
419
} else if content_type == mime:: APPLICATION_JSON {
@@ -349,6 +429,9 @@ fn parse_invoke_request<R: Runtime>(
349
429
return Err ( format ! ( "content type {content_type} is not implemented" ) ) ;
350
430
} ;
351
431
432
+ #[ cfg( feature = "tracing" ) ]
433
+ drop ( span) ;
434
+
352
435
let payload = InvokeRequest {
353
436
cmd,
354
437
callback,
0 commit comments