@@ -244,6 +244,32 @@ impl<T: UserEvent> Context<T> {
244244 }
245245}
246246
247+ #[ cfg( feature = "tracing" ) ]
248+ #[ derive( Debug , Clone , Default ) ]
249+ pub struct ActiveTraceSpanStore ( Rc < RefCell < Vec < ActiveTracingSpan > > > ) ;
250+
251+ #[ cfg( feature = "tracing" ) ]
252+ impl ActiveTraceSpanStore {
253+ pub fn remove_window_draw ( & self , window_id : WindowId ) {
254+ let mut store = self . 0 . borrow_mut ( ) ;
255+ if let Some ( index) = store
256+ . iter ( )
257+ . position ( |t| matches ! ( t, ActiveTracingSpan :: WindowDraw { id, span: _ } if id == & window_id) )
258+ {
259+ store. remove ( index) ;
260+ }
261+ }
262+ }
263+
264+ #[ cfg( feature = "tracing" ) ]
265+ #[ derive( Debug ) ]
266+ pub enum ActiveTracingSpan {
267+ WindowDraw {
268+ id : WindowId ,
269+ span : tracing:: span:: EnteredSpan ,
270+ } ,
271+ }
272+
247273#[ derive( Debug , Clone ) ]
248274pub struct DispatcherMainThreadContext < T : UserEvent > {
249275 pub window_target : EventLoopWindowTarget < Message < T > > ,
@@ -255,6 +281,8 @@ pub struct DispatcherMainThreadContext<T: UserEvent> {
255281 pub windows : Rc < RefCell < HashMap < WebviewId , WindowWrapper > > > ,
256282 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
257283 system_tray_manager : SystemTrayManager ,
284+ #[ cfg( feature = "tracing" ) ]
285+ pub active_tracing_spans : ActiveTraceSpanStore ,
258286}
259287
260288// SAFETY: we ensure this type is only used on the main thread.
@@ -1135,7 +1163,10 @@ pub enum WindowMessage {
11351163
11361164#[ derive( Debug , Clone ) ]
11371165pub enum WebviewMessage {
1166+ #[ cfg( not( feature = "tracing" ) ) ]
11381167 EvaluateScript ( String ) ,
1168+ #[ cfg( feature = "tracing" ) ]
1169+ EvaluateScript ( String , Sender < ( ) > , tracing:: Span ) ,
11391170 #[ allow( dead_code) ]
11401171 WebviewEvent ( WebviewEvent ) ,
11411172 Print ,
@@ -1651,6 +1682,21 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
16511682 )
16521683 }
16531684
1685+ #[ cfg( feature = "tracing" ) ]
1686+ fn eval_script < S : Into < String > > ( & self , script : S ) -> Result < ( ) > {
1687+ // use a channel so the EvaluateScript task uses the current span as parent
1688+ let ( tx, rx) = channel ( ) ;
1689+ getter ! (
1690+ self ,
1691+ rx,
1692+ Message :: Webview (
1693+ self . window_id,
1694+ WebviewMessage :: EvaluateScript ( script. into( ) , tx, tracing:: Span :: current( ) ) ,
1695+ )
1696+ )
1697+ }
1698+
1699+ #[ cfg( not( feature = "tracing" ) ) ]
16541700 fn eval_script < S : Into < String > > ( & self , script : S ) -> Result < ( ) > {
16551701 send_user_message (
16561702 & self . context ,
@@ -1962,6 +2008,8 @@ impl<T: UserEvent> Wry<T> {
19622008 windows,
19632009 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
19642010 system_tray_manager,
2011+ #[ cfg( feature = "tracing" ) ]
2012+ active_tracing_spans : Default :: default ( ) ,
19652013 } ,
19662014 } ;
19672015
@@ -2165,6 +2213,9 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
21652213 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
21662214 let system_tray_manager = self . context . main_thread . system_tray_manager . clone ( ) ;
21672215
2216+ #[ cfg( feature = "tracing" ) ]
2217+ let active_tracing_spans = self . context . main_thread . active_tracing_spans . clone ( ) ;
2218+
21682219 #[ cfg( all( desktop, feature = "global-shortcut" ) ) ]
21692220 let global_shortcut_manager = self . context . main_thread . global_shortcut_manager . clone ( ) ;
21702221 #[ cfg( all( desktop, feature = "global-shortcut" ) ) ]
@@ -2202,6 +2253,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
22022253 clipboard_manager : clipboard_manager. clone ( ) ,
22032254 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
22042255 system_tray_manager : system_tray_manager. clone ( ) ,
2256+ #[ cfg( feature = "tracing" ) ]
2257+ active_tracing_spans : active_tracing_spans. clone ( ) ,
22052258 } ,
22062259 web_context,
22072260 ) ;
@@ -2226,6 +2279,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
22262279 clipboard_manager : clipboard_manager. clone ( ) ,
22272280 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
22282281 system_tray_manager : system_tray_manager. clone ( ) ,
2282+ #[ cfg( feature = "tracing" ) ]
2283+ active_tracing_spans : active_tracing_spans. clone ( ) ,
22292284 } ,
22302285 web_context,
22312286 ) ;
@@ -2240,6 +2295,9 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
22402295 let web_context = self . context . main_thread . web_context ;
22412296 let mut plugins = self . plugins ;
22422297
2298+ #[ cfg( feature = "tracing" ) ]
2299+ let active_tracing_spans = self . context . main_thread . active_tracing_spans . clone ( ) ;
2300+
22432301 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
22442302 let system_tray_manager = self . context . main_thread . system_tray_manager ;
22452303
@@ -2272,6 +2330,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
22722330 clipboard_manager : clipboard_manager. clone ( ) ,
22732331 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
22742332 system_tray_manager : system_tray_manager. clone ( ) ,
2333+ #[ cfg( feature = "tracing" ) ]
2334+ active_tracing_spans : active_tracing_spans. clone ( ) ,
22752335 } ,
22762336 & web_context,
22772337 ) ;
@@ -2295,6 +2355,8 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
22952355 clipboard_manager : clipboard_manager. clone ( ) ,
22962356 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
22972357 system_tray_manager : system_tray_manager. clone ( ) ,
2358+ #[ cfg( feature = "tracing" ) ]
2359+ active_tracing_spans : active_tracing_spans. clone ( ) ,
22982360 } ,
22992361 & web_context,
23002362 ) ;
@@ -2314,6 +2376,8 @@ pub struct EventLoopIterationContext<'a, T: UserEvent> {
23142376 pub clipboard_manager : Arc < Mutex < Clipboard > > ,
23152377 #[ cfg( all( desktop, feature = "system-tray" ) ) ]
23162378 pub system_tray_manager : SystemTrayManager ,
2379+ #[ cfg( feature = "tracing" ) ]
2380+ pub active_tracing_spans : ActiveTraceSpanStore ,
23172381}
23182382
23192383struct UserMessageContext {
@@ -2590,6 +2654,19 @@ fn handle_user_message<T: UserEvent>(
25902654 }
25912655 }
25922656 Message :: Webview ( id, webview_message) => match webview_message {
2657+ #[ cfg( feature = "tracing" ) ]
2658+ WebviewMessage :: EvaluateScript ( script, tx, span) => {
2659+ let _span = span. entered ( ) ;
2660+ if let Some ( WindowHandle :: Webview { inner : webview, .. } ) =
2661+ windows. borrow ( ) . get ( & id) . and_then ( |w| w. inner . as_ref ( ) )
2662+ {
2663+ if let Err ( e) = webview. evaluate_script ( & script) {
2664+ debug_eprintln ! ( "{}" , e) ;
2665+ }
2666+ }
2667+ tx. send ( ( ) ) . unwrap ( ) ;
2668+ }
2669+ #[ cfg( not( feature = "tracing" ) ) ]
25932670 WebviewMessage :: EvaluateScript ( script) => {
25942671 if let Some ( WindowHandle :: Webview { inner : webview, .. } ) =
25952672 windows. borrow ( ) . get ( & id) . and_then ( |w| w. inner . as_ref ( ) )
@@ -2758,6 +2835,8 @@ fn handle_event_loop<T: UserEvent>(
27582835 clipboard_manager,
27592836 #[ cfg ( all ( desktop, feature = "system-tray" ) ) ]
27602837 system_tray_manager,
2838+ #[ cfg ( feature = "tracing" ) ]
2839+ active_tracing_spans,
27612840 } = context;
27622841 if * control_flow != ControlFlow :: Exit {
27632842 * control_flow = ControlFlow :: Wait ;
@@ -2780,6 +2859,11 @@ fn handle_event_loop<T: UserEvent>(
27802859 callback ( RunEvent :: Exit ) ;
27812860 }
27822861
2862+ #[ cfg( feature = "tracing" ) ]
2863+ Event :: RedrawRequested ( id) => {
2864+ active_tracing_spans. remove_window_draw ( id) ;
2865+ }
2866+
27832867 #[ cfg( all( desktop, feature = "global-shortcut" ) ) ]
27842868 Event :: GlobalShortcutEvent ( accelerator_id) => {
27852869 for ( id, handler) in & * global_shortcut_manager_handle. listeners . lock ( ) . unwrap ( ) {
@@ -3123,6 +3207,14 @@ fn create_webview<T: UserEvent>(
31233207 #[ cfg( windows) ]
31243208 let proxy = context. proxy . clone ( ) ;
31253209
3210+ #[ cfg( feature = "tracing" ) ]
3211+ let _webview_create_span = tracing:: debug_span!( "wry::webview::create" ) . entered ( ) ;
3212+ #[ cfg( feature = "tracing" ) ]
3213+ let window_draw_span = tracing:: debug_span!( "wry::window::draw" ) . entered ( ) ;
3214+ #[ cfg( feature = "tracing" ) ]
3215+ let window_create_span =
3216+ tracing:: debug_span!( parent: & window_draw_span, "wry::window::create" ) . entered ( ) ;
3217+
31263218 let window_event_listeners = WindowEventListeners :: default ( ) ;
31273219
31283220 #[ cfg( windows) ]
@@ -3157,6 +3249,21 @@ fn create_webview<T: UserEvent>(
31573249 let focused = window_builder. inner . window . focused ;
31583250 let window = window_builder. inner . build ( event_loop) . unwrap ( ) ;
31593251
3252+ #[ cfg( feature = "tracing" ) ]
3253+ {
3254+ drop ( window_create_span) ;
3255+
3256+ context
3257+ . main_thread
3258+ . active_tracing_spans
3259+ . 0
3260+ . borrow_mut ( )
3261+ . push ( ActiveTracingSpan :: WindowDraw {
3262+ id : window. id ( ) ,
3263+ span : window_draw_span,
3264+ } ) ;
3265+ }
3266+
31603267 webview_id_map. insert ( window. id ( ) , window_id) ;
31613268
31623269 if window_builder. center {
0 commit comments