reflow_tracing_protocol 0.2.1

Shared tracing-protocol types for the Reflow runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
//! # Reflow Tracing Client
//!
//! A WebSocket client for communicating with the reflow_tracing server.
//! Provides automatic actor tracing, batching, and cross-platform support.

use super::*;
use serde::{Deserialize, Serialize};
use serde_json;
use std::collections::{HashMap as StdHashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tracing::{debug, error, info, warn};

#[cfg(not(target_arch = "wasm32"))]
use futures_util::{SinkExt, StreamExt};
#[cfg(not(target_arch = "wasm32"))]
use tokio::net::TcpStream;
#[cfg(not(target_arch = "wasm32"))]
use tokio_tungstenite::{
    connect_async, tungstenite::Message as WsMessage, MaybeTlsStream, WebSocketStream,
};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsCast;
#[cfg(target_arch = "wasm32")]
use web_sys::{CloseEvent, ErrorEvent, MessageEvent, WebSocket};

/// Configuration for the tracing client
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TracingConfig {
    /// WebSocket server URL (e.g., "ws://localhost:8080")
    pub server_url: String,
    /// Maximum number of events to buffer before sending
    pub batch_size: usize,
    /// Maximum time to wait before sending a batch
    pub batch_timeout: Duration,
    /// Whether to enable compression for large payloads
    pub enable_compression: bool,
    /// Connection retry configuration
    pub retry_config: RetryConfig,
    /// Whether tracing is enabled
    pub enabled: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryConfig {
    pub max_retries: usize,
    pub initial_delay: Duration,
    pub max_delay: Duration,
    pub backoff_multiplier: f64,
}

/// Errors that can occur during tracing operations
#[derive(Debug, thiserror::Error)]
pub enum TracingError {
    #[error("Connection failed: {0}")]
    ConnectionFailed(String),
    #[error("Serialization failed: {0}")]
    SerializationFailed(#[from] serde_json::Error),
    #[error("WebSocket error: {0}")]
    WebSocketError(String),
    #[error("Client is disconnected")]
    Disconnected,
    #[error("Timeout occurred")]
    Timeout,
}

type Result<T> = std::result::Result<T, TracingError>;

/// Internal state for the tracing client
#[derive(Debug)]
struct ClientState {
    connection_status: ConnectionStatus,
    event_buffer: VecDeque<TraceEvent>,
    #[cfg(not(target_arch = "wasm32"))]
    pending_requests: StdHashMap<String, tokio::sync::oneshot::Sender<TracingResponse>>,
    last_batch_time: Instant,
    reconnect_attempts: usize,
    current_trace_id: Option<TraceId>,
    #[cfg(not(target_arch = "wasm32"))]
    message_sender: Option<tokio::sync::mpsc::UnboundedSender<String>>,
    #[cfg(target_arch = "wasm32")]
    websocket: Option<WebSocket>,
}

// `web_sys::WebSocket` (and other wasm-bindgen types) are `!Send`
// because they're handles into the JS heap. The browser is
// single-threaded, so concurrent access through the auto-trait check
// is moot — we only need this marker to satisfy generic bounds in
// downstream async code (e.g. `Box::pin(future) where Future: Send`).
#[cfg(target_arch = "wasm32")]
unsafe impl Send for ClientState {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for ClientState {}

#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
enum ConnectionStatus {
    Disconnected,
    Connecting,
    Connected,
    Reconnecting,
}

/// Main tracing client for communicating with reflow_tracing server
pub struct TracingClient {
    config: TracingConfig,
    state: Arc<Mutex<ClientState>>,
    #[cfg(not(target_arch = "wasm32"))]
    #[allow(dead_code)]
    runtime: Option<tokio::runtime::Handle>,
}

impl Default for TracingConfig {
    fn default() -> Self {
        Self {
            server_url: "ws://localhost:8080".to_string(),
            batch_size: 1, // Send events immediately by default
            batch_timeout: Duration::from_millis(100), // Shorter timeout
            enable_compression: true,
            retry_config: RetryConfig {
                max_retries: 5,
                initial_delay: Duration::from_millis(1000),
                max_delay: Duration::from_secs(30),
                backoff_multiplier: 2.0,
            },
            enabled: true,
        }
    }
}

impl TracingClient {
    /// Create a new tracing client with the given configuration
    pub fn new(config: TracingConfig) -> Self {
        let state = Arc::new(Mutex::new(ClientState {
            connection_status: ConnectionStatus::Disconnected,
            event_buffer: VecDeque::new(),
            #[cfg(not(target_arch = "wasm32"))]
            pending_requests: StdHashMap::new(),
            last_batch_time: Instant::now(),
            reconnect_attempts: 0,
            current_trace_id: None,
            #[cfg(not(target_arch = "wasm32"))]
            message_sender: None,
            #[cfg(target_arch = "wasm32")]
            websocket: None,
        }));

        Self {
            config: config.clone(),
            state,
            #[cfg(not(target_arch = "wasm32"))]
            runtime: if config.enabled {
                tokio::runtime::Handle::try_current().ok()
            } else {
                None
            },
        }
    }

    /// Create a new tracing client with default configuration
    pub fn with_default_config() -> Self {
        Self::new(TracingConfig::default())
    }

    /// Create a new tracing client with custom server URL
    pub fn with_server_url(url: impl Into<String>) -> Self {
        let config = TracingConfig {
            server_url: url.into(),
            ..TracingConfig::default()
        };
        Self::new(config)
    }

    /// Connect to the tracing server
    #[cfg(not(target_arch = "wasm32"))]
    pub async fn connect(&self) -> Result<()> {
        if !self.config.enabled {
            debug!("Tracing is disabled, skipping connection");
            return Ok(());
        }

        info!("Connecting to tracing server at {}", self.config.server_url);

        {
            let mut state = self.state.lock().unwrap();
            state.connection_status = ConnectionStatus::Connecting;
        }

        let (ws_stream, _) = connect_async(&self.config.server_url)
            .await
            .map_err(|e| TracingError::ConnectionFailed(e.to_string()))?;

        {
            let mut state = self.state.lock().unwrap();
            state.connection_status = ConnectionStatus::Connected;
            state.reconnect_attempts = 0;
        }

        info!("Successfully connected to tracing server");

        // Start the connection handler in a separate task
        let state_clone = Arc::clone(&self.state);
        let config_clone = self.config.clone();
        tokio::spawn(async move {
            Self::handle_connection_static(state_clone, config_clone, ws_stream).await;
        });

        // Wait a moment for connection to be fully established
        tokio::time::sleep(Duration::from_millis(100)).await;

        Ok(())
    }

    /// Handle the WebSocket connection (static method for spawning)
    #[cfg(not(target_arch = "wasm32"))]
    async fn handle_connection_static(
        state: Arc<Mutex<ClientState>>,
        config: TracingConfig,
        ws_stream: WebSocketStream<MaybeTlsStream<TcpStream>>,
    ) {
        let (ws_sender, mut ws_receiver) = ws_stream.split();

        // Create a channel for sending messages
        let (message_tx, mut message_rx) = tokio::sync::mpsc::unbounded_channel();

        // Store the sender in state for use by send_message
        {
            let mut state_guard = state.lock().unwrap();
            state_guard.message_sender = Some(message_tx);
        }

        // Spawn a task to handle outgoing messages
        let mut ws_sender = ws_sender;
        let state_for_sender = Arc::clone(&state);
        tokio::spawn(async move {
            while let Some(message) = message_rx.recv().await {
                debug!("Sending WebSocket message: {}", message);
                if let Err(e) = ws_sender.send(WsMessage::Text(message.into())).await {
                    error!("Failed to send message: {}", e);
                    // Mark connection as disconnected
                    let mut state_guard = state_for_sender.lock().unwrap();
                    state_guard.connection_status = ConnectionStatus::Disconnected;
                    state_guard.message_sender = None;
                    break;
                }
            }
        });

        // Spawn a task to handle incoming messages
        let state_for_receiver = Arc::clone(&state);
        tokio::spawn(async move {
            while let Some(msg) = ws_receiver.next().await {
                match msg {
                    Ok(WsMessage::Text(text)) => {
                        debug!("Received WebSocket message: {}", text);
                        // Handle different response types
                        if let Ok(response) = serde_json::from_str::<TraceResponse>(&text) {
                            let mut state_guard = state_for_receiver.lock().unwrap();

                            // Convert TraceResponse to TracingResponse for compatibility
                            let converted_response = match response {
                                TraceResponse::TraceStored { trace_id } => {
                                    TracingResponse::TraceStarted { trace_id }
                                }
                                TraceResponse::TracesFound { traces } => {
                                    TracingResponse::QueryResults {
                                        traces,
                                        total_count: 0,
                                    }
                                }
                                TraceResponse::TraceData { trace } => {
                                    TracingResponse::TraceData { trace: Some(trace) }
                                }
                                TraceResponse::Error { message } => TracingResponse::Error {
                                    message,
                                    code: ErrorCode::InternalError,
                                },
                                TraceResponse::Metrics { data: _ } => TracingResponse::Pong,
                            };

                            // Notify any pending requests
                            #[cfg(not(target_arch = "wasm32"))]
                            if let Some((_, sender)) = state_guard.pending_requests.drain().next() {
                                let _ = sender.send(converted_response);
                            };
                        }
                    }
                    Ok(WsMessage::Close(_)) => {
                        warn!("WebSocket connection closed by server");
                        let mut state_guard = state_for_receiver.lock().unwrap();
                        state_guard.connection_status = ConnectionStatus::Disconnected;
                        state_guard.message_sender = None;
                        break;
                    }
                    Err(e) => {
                        error!("WebSocket error: {}", e);
                        let mut state_guard = state_for_receiver.lock().unwrap();
                        state_guard.connection_status = ConnectionStatus::Disconnected;
                        state_guard.message_sender = None;
                        break;
                    }
                    _ => {}
                }
            }
        });

        // Handle event batching and sending
        let state_for_batching = Arc::clone(&state);
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(config.batch_timeout);

            loop {
                interval.tick().await;

                let (events_to_send, sender_ref) = {
                    let mut state_guard = state_for_batching.lock().unwrap();
                    if state_guard.connection_status != ConnectionStatus::Connected {
                        continue;
                    }

                    let should_send = !state_guard.event_buffer.is_empty()
                        && (state_guard.event_buffer.len() >= config.batch_size
                            || state_guard.last_batch_time.elapsed() >= config.batch_timeout);

                    if should_send {
                        let events = state_guard.event_buffer.drain(..).collect::<Vec<_>>();
                        state_guard.last_batch_time = Instant::now();
                        (events, state_guard.message_sender.clone())
                    } else {
                        (Vec::new(), None)
                    }
                };

                if !events_to_send.is_empty() {
                    info!("Sending batch of {} trace events", events_to_send.len());

                    if let Some(sender) = sender_ref {
                        for event in events_to_send {
                            // Send individual events as TracingRequest::RecordEvent
                            let request = TracingRequest::RecordEvent {
                                trace_id: TraceId::new(),
                                event,
                            };

                            if let Ok(message) = serde_json::to_string(&request) {
                                if let Err(e) = sender.send(message) {
                                    error!("Failed to send trace event: {}", e);
                                    let mut state_guard = state_for_batching.lock().unwrap();
                                    state_guard.connection_status = ConnectionStatus::Disconnected;
                                    state_guard.message_sender = None;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        });
    }

    /// Connect to the tracing server (WASM version)
    #[cfg(target_arch = "wasm32")]
    pub async fn connect(&self) -> Result<()> {
        if !self.config.enabled {
            debug!("Tracing is disabled, skipping connection");
            return Ok(());
        }

        info!("Connecting to tracing server at {}", self.config.server_url);

        let ws = WebSocket::new(&self.config.server_url).map_err(|_| {
            TracingError::ConnectionFailed("Failed to create WebSocket".to_string())
        })?;

        // Set up event handlers
        let state_clone = Arc::clone(&self.state);
        let onopen = Closure::wrap(Box::new(move || {
            let mut state = state_clone.lock().unwrap();
            state.connection_status = ConnectionStatus::Connected;
            state.reconnect_attempts = 0;
            info!("WebSocket connection opened");
        }) as Box<dyn FnMut()>);
        ws.set_onopen(Some(onopen.as_ref().unchecked_ref()));
        onopen.forget();

        let state_clone = Arc::clone(&self.state);
        let onclose = Closure::wrap(Box::new(move |_: CloseEvent| {
            let mut state = state_clone.lock().unwrap();
            state.connection_status = ConnectionStatus::Disconnected;
            warn!("WebSocket connection closed");
        }) as Box<dyn FnMut(CloseEvent)>);
        ws.set_onclose(Some(onclose.as_ref().unchecked_ref()));
        onclose.forget();

        let onerror = Closure::wrap(Box::new(move |_: ErrorEvent| {
            error!("WebSocket error occurred");
        }) as Box<dyn FnMut(ErrorEvent)>);
        ws.set_onerror(Some(onerror.as_ref().unchecked_ref()));
        onerror.forget();

        // Store websocket reference
        {
            let mut state = self.state.lock().unwrap();
            state.websocket = Some(ws);
            state.connection_status = ConnectionStatus::Connected;
        }

        Ok(())
    }

    /// Start a new trace for a flow
    pub async fn start_trace(&self, flow_id: FlowId, version: FlowVersion) -> Result<TraceId> {
        if !self.config.enabled {
            return Ok(TraceId::new());
        }

        let trace_id = TraceId::new();

        // Send StartTrace request
        let request = TracingRequest::StartTrace { flow_id, version };
        let message = serde_json::to_string(&request)?;

        // Send the message immediately
        self.send_message(message).await?;

        // Store current trace ID
        {
            let mut state = self.state.lock().unwrap();
            state.current_trace_id = Some(trace_id.clone());
        }

        Ok(trace_id)
    }

    /// Record a trace event
    pub async fn record_event(&self, _trace_id: TraceId, event: TraceEvent) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        debug!("Recording trace event: {:?}", event.event_type);

        // Check if we should send immediately
        let should_send_immediately = {
            let mut state = self.state.lock().unwrap();
            state.event_buffer.push_back(event.clone());

            // Check if we should send immediately
            if self.config.batch_size == 1 {
                // Pop the event for immediate sending
                state.event_buffer.pop_front().is_some()
            } else if state.event_buffer.len() >= self.config.batch_size {
                debug!("Event buffer full, will be sent by batching task");
                false
            } else {
                false
            }
        }; // Lock is dropped here

        // Send immediately if needed (lock is already dropped)
        if should_send_immediately {
            let request = TracingRequest::RecordEvent {
                trace_id: TraceId::new(),
                event,
            };

            if let Ok(message) = serde_json::to_string(&request) {
                self.send_message(message).await?;
            }
        }

        Ok(())
    }

    /// Send a ping to the server
    pub async fn ping(&self) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        let request = TracingRequest::Ping;
        let message = serde_json::to_string(&request)?;
        self.send_message(message).await?;
        Ok(())
    }

    /// Query traces from the server
    pub async fn query_traces(&self, query: TraceQuery) -> Result<Vec<FlowTrace>> {
        if !self.config.enabled {
            return Ok(Vec::new());
        }

        let request = TracingRequest::QueryTraces { query };
        let message = serde_json::to_string(&request)?;
        self.send_message(message).await?;

        // For now, return empty - would need proper response handling
        Ok(Vec::new())
    }

    /// Get a specific trace by ID
    pub async fn get_trace(&self, trace_id: TraceId) -> Result<Option<FlowTrace>> {
        if !self.config.enabled {
            return Ok(None);
        }

        let request = TracingRequest::GetTrace { trace_id };
        let message = serde_json::to_string(&request)?;
        self.send_message(message).await?;

        // For now, return None - would need proper response handling
        Ok(None)
    }

    /// Subscribe to real-time trace events
    pub async fn subscribe(&self, filters: SubscriptionFilters) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        let request = TracingRequest::Subscribe { filters };
        let message = serde_json::to_string(&request)?;
        self.send_message(message).await?;
        Ok(())
    }

    /// Check if the client is connected
    pub fn is_connected(&self) -> bool {
        let state = self.state.lock().unwrap();
        state.connection_status == ConnectionStatus::Connected
    }

    /// Get the current connection status
    pub fn connection_status(&self) -> String {
        let state = self.state.lock().unwrap();
        format!("{:?}", state.connection_status)
    }

    /// Shutdown the tracing client and flush pending events
    pub async fn shutdown(&self) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        info!("Shutting down tracing client...");

        // Flush any remaining events in the buffer
        let events_to_send = {
            let mut state = self.state.lock().unwrap();
            state.connection_status = ConnectionStatus::Disconnected;
            state.event_buffer.drain(..).collect::<Vec<_>>()
        };

        if !events_to_send.is_empty() {
            info!("Flushing {} pending trace events", events_to_send.len());

            for event in events_to_send {
                let request = TracingRequest::RecordEvent {
                    trace_id: TraceId::new(),
                    event,
                };

                if let Ok(message) = serde_json::to_string(&request) {
                    let _ = self.send_message(message).await;
                }
            }
        }

        info!("Tracing client shutdown complete");
        Ok(())
    }

    /// Force flush any pending events
    pub async fn flush(&self) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        let events_to_send = {
            let mut state = self.state.lock().unwrap();
            if state.event_buffer.is_empty() {
                return Ok(());
            }

            let events = state.event_buffer.drain(..).collect::<Vec<_>>();
            state.last_batch_time = Instant::now();
            events
        };

        info!("Flushing {} trace events", events_to_send.len());

        for event in events_to_send {
            let request = TracingRequest::RecordEvent {
                trace_id: TraceId::new(),
                event,
            };

            if let Ok(message) = serde_json::to_string(&request) {
                self.send_message(message).await?;
            }
        }

        Ok(())
    }

    /// Send a message through the WebSocket connection
    async fn send_message(&self, message: String) -> Result<()> {
        debug!("Attempting to send message: {}", message);

        #[cfg(not(target_arch = "wasm32"))]
        {
            let sender_ref = {
                let state = self.state.lock().unwrap();
                if state.connection_status != ConnectionStatus::Connected {
                    return Err(TracingError::Disconnected);
                }
                state.message_sender.clone()
            };

            if let Some(sender) = sender_ref {
                sender
                    .send(message.clone())
                    .map_err(|e| TracingError::WebSocketError(e.to_string()))?;
                debug!("Message queued for sending: {}", message);
            } else {
                return Err(TracingError::Disconnected);
            }
        }

        #[cfg(target_arch = "wasm32")]
        {
            let websocket = {
                let state = self.state.lock().unwrap();
                if state.connection_status != ConnectionStatus::Connected {
                    return Err(TracingError::Disconnected);
                }
                state.websocket.clone()
            };

            if let Some(ws) = websocket {
                ws.send_with_str(&message).map_err(|_| {
                    TracingError::WebSocketError("Failed to send message".to_string())
                })?;
                debug!("Message sent via WebSocket: {}", message);
            } else {
                return Err(TracingError::Disconnected);
            }
        }

        Ok(())
    }
}

/// High-level API for tracing integration with actor systems
#[derive(Clone)]
pub struct TracingIntegration {
    client: Arc<TracingClient>,
    current_trace_id: Arc<Mutex<Option<TraceId>>>,
}

unsafe impl Send for TracingIntegration {}
unsafe impl Sync for TracingIntegration {}

impl TracingIntegration {
    /// Create a new tracing integration
    pub fn new(client: TracingClient) -> Self {
        Self {
            client: Arc::new(client),
            current_trace_id: Arc::new(Mutex::new(None)),
        }
    }

    /// Start tracing for a flow
    pub async fn start_flow_trace(&self, flow_id: impl Into<String>) -> Result<TraceId> {
        let flow_id = FlowId::new(flow_id);
        let version = FlowVersion {
            major: 1,
            minor: 0,
            patch: 0,
            git_hash: None,
            timestamp: chrono::Utc::now(),
        };

        let trace_id = self.client.start_trace(flow_id, version).await?;
        {
            let mut current = self.current_trace_id.lock().unwrap();
            *current = Some(trace_id.clone());
        }
        Ok(trace_id)
    }

    /// Record an actor creation event
    pub async fn trace_actor_created(&self, actor_id: impl Into<String>) -> Result<()> {
        let event = TraceEvent::actor_created(actor_id.into());
        let trace_id = self.current_trace_id.lock().unwrap().clone();
        if let Some(trace_id) = trace_id {
            self.client.record_event(trace_id, event).await
        } else {
            // Use a default trace ID if none is set
            self.client.record_event(TraceId::new(), event).await
        }
    }

    /// Record an actor process completion event
    pub async fn trace_actor_completed(&self, actor_id: impl Into<String>) -> Result<()> {
        let event = TraceEvent::actor_completed(actor_id.into());
        let trace_id = self.current_trace_id.lock().unwrap().clone();
        if let Some(trace_id) = trace_id {
            self.client.record_event(trace_id, event).await
        } else {
            // Use a default trace ID if none is set
            self.client.record_event(TraceId::new(), event).await
        }
    }

    /// Record a message sent event
    pub async fn trace_message_sent(
        &self,
        actor_id: impl Into<String>,
        port: impl Into<String>,
        message_type: impl Into<String>,
        size_bytes: usize,
    ) -> Result<()> {
        let event = TraceEvent::message_sent(
            actor_id.into(),
            port.into(),
            message_type.into(),
            size_bytes,
        );
        let trace_id = self.current_trace_id.lock().unwrap().clone();
        if let Some(trace_id) = trace_id {
            self.client.record_event(trace_id, event).await
        } else {
            // Use a default trace ID if none is set
            self.client.record_event(TraceId::new(), event).await
        }
    }

    /// Record an actor failure event
    pub async fn trace_actor_failed(
        &self,
        actor_id: impl Into<String>,
        error: impl Into<String>,
    ) -> Result<()> {
        let event = TraceEvent::actor_failed(actor_id.into(), error.into());
        let trace_id = self.current_trace_id.lock().unwrap().clone();
        if let Some(trace_id) = trace_id {
            self.client.record_event(trace_id, event).await
        } else {
            // Use a default trace ID if none is set
            self.client.record_event(TraceId::new(), event).await
        }
    }

    /// Record a data flow event between two actors
    pub async fn trace_data_flow(
        &self,
        from_actor: impl Into<String>,
        from_port: impl Into<String>,
        to_actor: impl Into<String>,
        to_port: impl Into<String>,
        message_type: impl Into<String>,
        size_bytes: usize,
    ) -> Result<()> {
        let event = TraceEvent::data_flow(
            from_actor.into(),
            from_port.into(),
            to_actor.into(),
            to_port.into(),
            message_type.into(),
            size_bytes,
        );

        let trace_id = self.current_trace_id.lock().unwrap().clone();
        if let Some(trace_id) = trace_id {
            self.client.record_event(trace_id, event).await
        } else {
            // Use a default trace ID if none is set
            self.client.record_event(TraceId::new(), event).await
        }
    }

    /// Get access to the underlying client
    pub fn client(&self) -> Arc<TracingClient> {
        Arc::clone(&self.client)
    }
}

/// Global tracing client instance
static GLOBAL_CLIENT: std::sync::OnceLock<TracingIntegration> = std::sync::OnceLock::new();

/// Initialize the global tracing client
pub fn init_global_tracing(config: TracingConfig) -> Result<()> {
    let client = TracingClient::new(config);
    let integration = TracingIntegration::new(client);

    GLOBAL_CLIENT.set(integration).map_err(|_| {
        TracingError::WebSocketError("Global tracing client already initialized".to_string())
    })?;

    Ok(())
}

/// Get the global tracing client
pub fn global_tracing() -> Option<&'static TracingIntegration> {
    GLOBAL_CLIENT.get()
}

/// Convenience macro for tracing actor events
#[macro_export]
macro_rules! trace_actor_event {
    (created, $actor_id:expr) => {
        if let Some(tracing) = $crate::tracing::global_tracing() {
            let _ = tracing.trace_actor_created($actor_id).await;
        }
    };
    (message_sent, $actor_id:expr, $port:expr, $msg_type:expr, $size:expr) => {
        if let Some(tracing) = $crate::tracing::global_tracing() {
            let _ = tracing
                .trace_message_sent($actor_id, $port, $msg_type, $size)
                .await;
        }
    };
    (failed, $actor_id:expr, $error:expr) => {
        if let Some(tracing) = $crate::tracing::global_tracing() {
            let _ = tracing.trace_actor_failed($actor_id, $error).await;
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_default() {
        let config = TracingConfig::default();
        assert_eq!(config.server_url, "ws://localhost:8080");
        assert_eq!(config.batch_size, 1); // Updated to match new default
        assert!(config.enabled);
    }

    #[test]
    fn test_client_creation() {
        let client = TracingClient::with_default_config();
        assert!(!client.is_connected());
    }

    #[tokio::test]
    async fn test_tracing_integration() {
        let config = TracingConfig {
            enabled: false,
            ..Default::default()
        }; // Disable tracing for test

        let client = TracingClient::new(config);
        let integration = TracingIntegration::new(client);

        // Test starting a trace (should succeed even when disabled)
        let result = integration.start_flow_trace("test_flow").await;
        assert!(result.is_ok());

        // Test recording events (should also succeed when disabled)
        let actor_result = integration.trace_actor_created("test_actor").await;
        assert!(actor_result.is_ok());

        let message_result = integration
            .trace_message_sent("test_actor", "output", "TestMessage", 128)
            .await;
        assert!(message_result.is_ok());
    }
}