joerl 0.7.1

An Erlang-inspired actor model library for Rust
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
//! Telemetry and observability for joerl.
//!
//! This module provides comprehensive telemetry support including:
//! - Structured tracing with OpenTelemetry spans
//! - Metrics (counters, gauges, histograms) for actor operations
//! - Integration with standard observability backends
//!
//! ## Feature Flag
//!
//! Telemetry support is optional and enabled via the `telemetry` feature flag.
//! When disabled, all telemetry operations become zero-cost no-ops.
//!
//! ## Metrics
//!
//! The following metrics are tracked:
//!
//! ### Actor Lifecycle
//! - `joerl_actors_spawned_total`: Total number of actors spawned
//! - `joerl_actors_stopped_total`: Total number of actors stopped (by reason)
//! - `joerl_actors_active`: Current number of active actors
//! - `joerl_actors_panicked_total`: Total number of actors that panicked
//! - `joerl_actor_lifetime_seconds`: Actor lifetime duration histogram
//! - `joerl_short_lived_actors_total`: Actors that lived < 1 second
//!
//! ### Messages
//! - `joerl_messages_sent_total`: Total messages sent
//! - `joerl_messages_sent_failed_total`: Failed message send attempts
//! - `joerl_messages_processed_total`: Total messages processed
//! - `joerl_message_processing_duration_seconds`: Message processing time histogram
//! - `joerl_message_queue_wait_seconds`: Time messages spend in queue before processing
//!
//! ### Mailboxes
//! - `joerl_mailbox_depth`: Current mailbox depth (gauge)
//! - `joerl_mailbox_full_total`: Times mailbox was full
//!
//! ### Links and Monitors
//! - `joerl_links_created_total`: Total links created
//! - `joerl_monitors_created_total`: Total monitors created
//!
//! ### Supervisors
//! - `joerl_supervisor_restarts_total`: Total child restarts
//! - `joerl_supervisor_restart_intensity_exceeded_total`: Times restart intensity was exceeded
//!
//! ### GenServers
//! - `joerl_gen_server_call_duration_seconds`: Call duration histogram
//! - `joerl_gen_server_casts_total`: Total casts sent
//! - `joerl_gen_server_call_timeouts_total`: Total call timeouts
//! - `joerl_gen_server_calls_in_flight`: Current number of calls in progress
//!
//! ### GenStatems
//! - `joerl_gen_statem_transitions_total`: Total state transitions
//! - `joerl_gen_statem_invalid_transitions_total`: Invalid transition attempts
//! - `joerl_gen_statem_state_duration_seconds`: Time spent in each state
//! - `joerl_gen_statem_current_state`: Current state of state machines
//!
//! ### Signals
//! - `joerl_signals_sent_total`: Total signals sent
//! - `joerl_signals_received_total`: Total signals received
//! - `joerl_signals_ignored_total`: Signals ignored (trapped exits)
//! - `joerl_exit_signals_by_reason_total`: Exit signals by reason
//!
//! ### Distributed System
//! - `joerl_remote_messages_sent_total`: Remote messages sent (by target node)
//! - `joerl_remote_messages_failed_total`: Remote message send failures (by node and reason)
//! - `joerl_node_connections_active`: Active node connections
//! - `joerl_node_connection_established_total`: Node connections established
//! - `joerl_node_connection_lost_total`: Node connections lost
//! - `joerl_network_latency_seconds`: Network operation latency histogram
//! - `joerl_serialization_errors_total`: Message serialization errors
//!
//! ## Example
//!
//! ```rust,no_run
//! use joerl::telemetry;
//! use joerl::{Actor, ActorContext, ActorSystem, Message};
//! use async_trait::async_trait;
//!
//! #[tokio::main]
//! async fn main() {
//!     // Initialize telemetry (when feature is enabled)
//!     telemetry::init();
//!
//!     let system = ActorSystem::new();
//!     // Actors are now automatically instrumented
//! }
//! ```

#[cfg(feature = "telemetry")]
use std::time::Instant;

#[cfg(feature = "telemetry")]
use metrics::{counter, gauge, histogram};

#[cfg(feature = "telemetry")]
use std::sync::atomic::{AtomicU32, Ordering};

#[cfg(feature = "telemetry")]
use std::sync::OnceLock;

#[cfg(feature = "telemetry")]
use tracing::{debug_span, info_span};

/// Trait for custom telemetry providers.
///
/// Implement this trait to integrate joerl with custom telemetry backends
/// or add application-specific telemetry logic.
///
/// # Examples
///
/// ```rust,no_run
/// use joerl::telemetry::TelemetryProvider;
///
/// struct MyTelemetryProvider;
///
/// impl TelemetryProvider for MyTelemetryProvider {
///     fn on_actor_spawned(&self, actor_type: &str, pid: &str) {
///         println!("Actor {} spawned with pid {}", actor_type, pid);
///     }
///     
///     fn on_actor_stopped(&self, actor_type: &str, pid: &str, reason: &str) {
///         println!("Actor {} stopped: {}", actor_type, reason);
///     }
///     
///     fn on_message_sent(&self, from_pid: &str, to_pid: &str) {
///         // Custom logic
///     }
///     
///     fn on_message_received(&self, pid: &str) {
///         // Custom logic
///     }
/// }
///
/// // Register the provider
/// joerl::telemetry::set_telemetry_provider(Box::new(MyTelemetryProvider));
/// ```
pub trait TelemetryProvider: Send + Sync {
    /// Called when an actor is spawned.
    fn on_actor_spawned(&self, actor_type: &str, pid: &str) {
        let _ = (actor_type, pid);
    }

    /// Called when an actor stops.
    fn on_actor_stopped(&self, actor_type: &str, pid: &str, reason: &str) {
        let _ = (actor_type, pid, reason);
    }

    /// Called when an actor panics.
    fn on_actor_panicked(&self, actor_type: &str, pid: &str, error: &str) {
        let _ = (actor_type, pid, error);
    }

    /// Called when a message is sent.
    fn on_message_sent(&self, from_pid: &str, to_pid: &str) {
        let _ = (from_pid, to_pid);
    }

    /// Called when a message is received.
    fn on_message_received(&self, pid: &str) {
        let _ = pid;
    }

    /// Called when a supervisor restarts a child.
    fn on_supervisor_restart(&self, child_type: &str, strategy: &str) {
        let _ = (child_type, strategy);
    }
}

/// Configuration for telemetry sampling.
///
/// Sampling reduces overhead in high-throughput systems by only recording
/// a percentage of metrics. This is especially useful for high-frequency
/// operations like message processing.
///
/// Sampling rates are expressed as percentages (0-100):
/// - 100 = record all events (default, no sampling)
/// - 50 = record 50% of events
/// - 10 = record 10% of events
/// - 1 = record 1% of events
///
/// # Examples
///
/// ```rust
/// use joerl::telemetry::TelemetryConfig;
///
/// let config = TelemetryConfig {
///     message_sampling_rate: 10,  // Sample 10% of messages
///     signal_sampling_rate: 100,  // Sample all signals
///     ..Default::default()
/// };
///
/// // Apply configuration
/// joerl::telemetry::set_config(config);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct TelemetryConfig {
    /// Sampling rate for message processing metrics (0-100%)
    pub message_sampling_rate: u32,
    /// Sampling rate for signal metrics (0-100%)
    pub signal_sampling_rate: u32,
    /// Sampling rate for queue wait time metrics (0-100%)
    pub queue_wait_sampling_rate: u32,
}

impl Default for TelemetryConfig {
    fn default() -> Self {
        Self {
            message_sampling_rate: 100,
            signal_sampling_rate: 100,
            queue_wait_sampling_rate: 100,
        }
    }
}

#[cfg(feature = "telemetry")]
static TELEMETRY_CONFIG: OnceLock<TelemetryConfig> = OnceLock::new();

#[cfg(feature = "telemetry")]
static SAMPLE_COUNTER: AtomicU32 = AtomicU32::new(0);

#[cfg(feature = "telemetry")]
static CUSTOM_PROVIDER: OnceLock<Box<dyn TelemetryProvider>> = OnceLock::new();

/// Telemetry context for tracking operation duration.
///
/// This struct automatically records the duration when dropped.
pub struct TelemetrySpan {
    #[cfg(feature = "telemetry")]
    start: Instant,
    #[cfg(feature = "telemetry")]
    metric_name: &'static str,
    #[cfg(feature = "telemetry")]
    should_record: bool,
}

impl TelemetrySpan {
    /// Creates a new telemetry span for tracking duration.
    #[inline]
    pub fn new(_metric_name: &'static str) -> Self {
        Self {
            #[cfg(feature = "telemetry")]
            start: Instant::now(),
            #[cfg(feature = "telemetry")]
            metric_name: _metric_name,
            #[cfg(feature = "telemetry")]
            should_record: true,
        }
    }

    /// Creates a new telemetry span with explicit sampling control.
    #[inline]
    pub fn new_sampled(_metric_name: &'static str, _should_record: bool) -> Self {
        Self {
            #[cfg(feature = "telemetry")]
            start: Instant::now(),
            #[cfg(feature = "telemetry")]
            metric_name: _metric_name,
            #[cfg(feature = "telemetry")]
            should_record: _should_record,
        }
    }

    /// Records the span duration and finishes it.
    #[cfg(feature = "telemetry")]
    pub fn finish(self) {
        if self.should_record {
            let duration = self.start.elapsed();
            histogram!(self.metric_name).record(duration.as_secs_f64());
        }
    }

    /// No-op when telemetry is disabled.
    #[cfg(not(feature = "telemetry"))]
    pub fn finish(self) {}
}

#[cfg(feature = "telemetry")]
impl Drop for TelemetrySpan {
    fn drop(&mut self) {
        if self.should_record {
            let duration = self.start.elapsed();
            histogram!(self.metric_name).record(duration.as_secs_f64());
        }
    }
}

/// Actor type name helper.
///
/// Extracts a human-readable type name from a type.
/// This is used for metric labels to identify actor types.
pub fn actor_type_name<T: ?Sized>() -> &'static str {
    let full_name = std::any::type_name::<T>();
    // Extract just the struct name, removing module path
    full_name.rsplit("::").next().unwrap_or(full_name)
}

/// Actor lifecycle metrics.
pub struct ActorMetrics;

impl ActorMetrics {
    /// Records an actor spawn with type information.
    #[inline]
    pub fn actor_spawned_typed(actor_type: &str) {
        #[cfg(feature = "telemetry")]
        {
            counter!("joerl_actors_spawned_total", "type" => actor_type.to_string()).increment(1);
            gauge!("joerl_actors_active", "type" => actor_type.to_string()).increment(1.0);
        }
    }

    /// Records actor lifetime statistics.
    #[inline]
    pub fn actor_lifetime(actor_type: &str, lifetime_secs: f64) {
        #[cfg(feature = "telemetry")]
        {
            // Record lifetime histogram
            histogram!("joerl_actor_lifetime_seconds", "type" => actor_type.to_string())
                .record(lifetime_secs);

            // Track short-lived actors (< 1 second)
            if lifetime_secs < 1.0 {
                counter!("joerl_short_lived_actors_total", "type" => actor_type.to_string())
                    .increment(1);
            }
        }
    }

    /// Records an actor spawn (backward compatible, no type label).
    #[inline]
    pub fn actor_spawned() {
        Self::actor_spawned_typed("unknown")
    }

    /// Records an actor stop with type information.
    #[inline]
    pub fn actor_stopped_typed(actor_type: &str, _reason: &str) {
        #[cfg(feature = "telemetry")]
        {
            counter!("joerl_actors_stopped_total",
                "type" => actor_type.to_string(),
                "reason" => _reason.to_string()
            )
            .increment(1);
            gauge!("joerl_actors_active", "type" => actor_type.to_string()).decrement(1.0);
        }
    }

    /// Records an actor stop (backward compatible, no type label).
    #[inline]
    pub fn actor_stopped(_reason: &str) {
        Self::actor_stopped_typed("unknown", _reason)
    }

    /// Records an actor panic with type information.
    #[inline]
    pub fn actor_panicked_typed(actor_type: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_actors_panicked_total", "type" => actor_type.to_string()).increment(1);
    }

    /// Records an actor panic (backward compatible, no type label).
    #[inline]
    pub fn actor_panicked() {
        Self::actor_panicked_typed("unknown")
    }

    /// Creates an OpenTelemetry span for actor spawning.
    #[cfg(feature = "telemetry")]
    #[inline]
    pub fn actor_spawn_span(actor_type: &str, pid: &str) -> tracing::Span {
        info_span!(
            "actor.spawn",
            actor.type = actor_type,
            actor.pid = pid,
            otel.kind = "internal"
        )
    }

    /// No-op when telemetry is disabled.
    #[cfg(not(feature = "telemetry"))]
    #[inline]
    pub fn actor_spawn_span(_actor_type: &str, _pid: &str) -> tracing::Span {
        tracing::Span::none()
    }

    /// Creates an OpenTelemetry span for actor lifecycle events.
    #[cfg(feature = "telemetry")]
    #[inline]
    pub fn actor_lifecycle_span(event: &str, actor_type: &str, pid: &str) -> tracing::Span {
        info_span!(
            "actor.lifecycle",
            actor.event = event,
            actor.type = actor_type,
            actor.pid = pid
        )
    }

    /// No-op when telemetry is disabled.
    #[cfg(not(feature = "telemetry"))]
    #[inline]
    pub fn actor_lifecycle_span(_event: &str, _actor_type: &str, _pid: &str) -> tracing::Span {
        tracing::Span::none()
    }
}

/// Message and mailbox metrics.
pub struct MessageMetrics;

impl MessageMetrics {
    /// Records a successful message send.
    #[inline]
    pub fn message_sent() {
        #[cfg(feature = "telemetry")]
        counter!("joerl_messages_sent_total").increment(1);
    }

    /// Records a failed message send.
    #[inline]
    pub fn message_send_failed(_reason: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_messages_sent_failed_total", "reason" => _reason.to_string()).increment(1);
    }

    /// Records a processed message.
    #[inline]
    pub fn message_processed() {
        #[cfg(feature = "telemetry")]
        {
            let config = get_config();
            if should_sample(config.message_sampling_rate) {
                counter!("joerl_messages_processed_total").increment(1);
            }
        }
    }

    /// Creates a span for tracking message processing duration.
    ///
    /// Note: Sampling is applied when the span is dropped/finished.
    #[inline]
    pub fn message_processing_span() -> TelemetrySpan {
        #[cfg(feature = "telemetry")]
        {
            let config = get_config();
            if should_sample(config.message_sampling_rate) {
                return TelemetrySpan::new_sampled(
                    "joerl_message_processing_duration_seconds",
                    true,
                );
            }
        }
        TelemetrySpan::new_sampled("joerl_message_processing_duration_seconds", false)
    }

    /// Records message queue wait time.
    #[inline]
    pub fn message_queue_wait(wait_time_secs: f64) {
        #[cfg(feature = "telemetry")]
        {
            let config = get_config();
            if should_sample(config.queue_wait_sampling_rate) {
                histogram!("joerl_message_queue_wait_seconds").record(wait_time_secs);
            }
        }
    }

    /// Updates mailbox depth gauge with actor type.
    #[inline]
    pub fn mailbox_depth_typed(actor_type: &str, _depth: usize, _capacity: usize) {
        #[cfg(feature = "telemetry")]
        {
            gauge!("joerl_mailbox_depth", "type" => actor_type.to_string()).set(_depth as f64);
            // Also track utilization percentage
            let utilization = if _capacity > 0 {
                (_depth as f64 / _capacity as f64) * 100.0
            } else {
                0.0
            };
            gauge!("joerl_mailbox_utilization_percent", "type" => actor_type.to_string())
                .set(utilization);
        }
    }

    /// Updates mailbox depth gauge (backward compatible, no type label).
    #[inline]
    pub fn mailbox_depth(_depth: usize) {
        #[cfg(feature = "telemetry")]
        gauge!("joerl_mailbox_depth").set(_depth as f64);
    }

    /// Records mailbox full event with actor type.
    #[inline]
    pub fn mailbox_full_typed(actor_type: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_mailbox_full_total", "type" => actor_type.to_string()).increment(1);
    }

    /// Records mailbox full event (backward compatible, no type label).
    #[inline]
    pub fn mailbox_full() {
        #[cfg(feature = "telemetry")]
        counter!("joerl_mailbox_full_total").increment(1);
    }

    /// Creates an OpenTelemetry span for message sending.
    #[cfg(feature = "telemetry")]
    #[inline]
    pub fn message_send_span(from_pid: &str, to_pid: &str) -> tracing::Span {
        debug_span!(
            "message.send",
            messaging.operation = "send",
            messaging.system = "joerl",
            actor.from_pid = from_pid,
            actor.to_pid = to_pid,
            otel.kind = "producer"
        )
    }

    /// No-op when telemetry is disabled.
    #[cfg(not(feature = "telemetry"))]
    #[inline]
    pub fn message_send_span(_from_pid: &str, _to_pid: &str) -> tracing::Span {
        tracing::Span::none()
    }

    /// Creates an OpenTelemetry span for message receiving.
    /// If parent_span_id is provided, it will be recorded as a span attribute for linking.
    #[cfg(feature = "telemetry")]
    #[inline]
    pub fn message_receive_span(to_pid: &str, parent_span_id: Option<&str>) -> tracing::Span {
        let span = debug_span!(
            "message.receive",
            messaging.operation = "receive",
            messaging.system = "joerl",
            actor.pid = to_pid,
            parent.span.id = parent_span_id.unwrap_or("none"),
            otel.kind = "consumer"
        );

        span
    }

    /// No-op when telemetry is disabled.
    #[cfg(not(feature = "telemetry"))]
    #[inline]
    pub fn message_receive_span(_to_pid: &str, _parent_span_id: Option<&str>) -> tracing::Span {
        tracing::Span::none()
    }
}

/// Link and monitor metrics.
pub struct LinkMetrics;

impl LinkMetrics {
    /// Records a link creation.
    #[inline]
    pub fn link_created() {
        #[cfg(feature = "telemetry")]
        counter!("joerl_links_created_total").increment(1);
    }

    /// Records a monitor creation.
    #[inline]
    pub fn monitor_created() {
        #[cfg(feature = "telemetry")]
        counter!("joerl_monitors_created_total").increment(1);
    }
}

/// Supervisor metrics.
pub struct SupervisorMetrics;

impl SupervisorMetrics {
    /// Records a child restart.
    #[inline]
    pub fn child_restarted(_strategy: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_supervisor_restarts_total", "strategy" => _strategy.to_string())
            .increment(1);
    }

    /// Records restart intensity exceeded.
    #[inline]
    pub fn restart_intensity_exceeded() {
        #[cfg(feature = "telemetry")]
        counter!("joerl_supervisor_restart_intensity_exceeded_total").increment(1);
    }

    /// Creates a span for tracking restart duration.
    #[inline]
    pub fn restart_span() -> TelemetrySpan {
        TelemetrySpan::new("joerl_supervisor_restart_duration_seconds")
    }
}

/// GenServer metrics.
pub struct GenServerMetrics;

impl GenServerMetrics {
    /// Records a GenServer call with duration tracking.
    #[inline]
    pub fn call_span(server_type: &str) -> GenServerCallSpan {
        GenServerCallSpan {
            #[cfg(feature = "telemetry")]
            start: Instant::now(),
            #[cfg(feature = "telemetry")]
            server_type: server_type.to_string(),
        }
    }

    /// Records a GenServer cast.
    #[inline]
    pub fn cast(server_type: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_gen_server_casts_total", "type" => server_type.to_string()).increment(1);
    }

    /// Records a GenServer call timeout.
    #[inline]
    pub fn call_timeout(server_type: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_gen_server_call_timeouts_total", "type" => server_type.to_string())
            .increment(1);
    }

    /// Updates the number of calls currently in flight.
    #[inline]
    pub fn calls_in_flight_inc(server_type: &str) {
        #[cfg(feature = "telemetry")]
        gauge!("joerl_gen_server_calls_in_flight", "type" => server_type.to_string())
            .increment(1.0);
    }

    /// Decrements the number of calls currently in flight.
    #[inline]
    pub fn calls_in_flight_dec(server_type: &str) {
        #[cfg(feature = "telemetry")]
        gauge!("joerl_gen_server_calls_in_flight", "type" => server_type.to_string())
            .decrement(1.0);
    }
}

/// Span for tracking GenServer call duration.
pub struct GenServerCallSpan {
    #[cfg(feature = "telemetry")]
    start: Instant,
    #[cfg(feature = "telemetry")]
    server_type: String,
}

impl GenServerCallSpan {
    /// Records the call duration and finishes the span.
    #[cfg(feature = "telemetry")]
    pub fn finish(self) {
        let duration = self.start.elapsed();
        histogram!("joerl_gen_server_call_duration_seconds", "type" => self.server_type.clone())
            .record(duration.as_secs_f64());
    }

    /// No-op when telemetry is disabled.
    #[cfg(not(feature = "telemetry"))]
    pub fn finish(self) {}
}

#[cfg(feature = "telemetry")]
impl Drop for GenServerCallSpan {
    fn drop(&mut self) {
        let duration = self.start.elapsed();
        histogram!("joerl_gen_server_call_duration_seconds", "type" => self.server_type.clone())
            .record(duration.as_secs_f64());
    }
}

/// Signal metrics.
pub struct SignalMetrics;

impl SignalMetrics {
    /// Records a signal sent.
    #[inline]
    pub fn signal_sent(signal_type: &str) {
        #[cfg(feature = "telemetry")]
        {
            let config = get_config();
            if should_sample(config.signal_sampling_rate) {
                counter!("joerl_signals_sent_total", "type" => signal_type.to_string())
                    .increment(1);
            }
        }
    }

    /// Records a signal received by an actor.
    #[inline]
    pub fn signal_received(signal_type: &str) {
        #[cfg(feature = "telemetry")]
        {
            let config = get_config();
            if should_sample(config.signal_sampling_rate) {
                counter!("joerl_signals_received_total", "type" => signal_type.to_string())
                    .increment(1);
            }
        }
    }

    /// Records a signal that was ignored (trapped).
    #[inline]
    pub fn signal_ignored(signal_type: &str) {
        #[cfg(feature = "telemetry")]
        {
            let config = get_config();
            if should_sample(config.signal_sampling_rate) {
                counter!("joerl_signals_ignored_total", "type" => signal_type.to_string())
                    .increment(1);
            }
        }
    }

    /// Records an exit signal with its reason.
    #[inline]
    pub fn exit_signal_by_reason(reason: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_exit_signals_by_reason_total", "reason" => reason.to_string()).increment(1);
    }
}

/// GenStatem metrics.
pub struct GenStatemMetrics;

impl GenStatemMetrics {
    /// Records a state transition.
    #[inline]
    pub fn state_transition(fsm_type: &str, from_state: &str, to_state: &str) {
        #[cfg(feature = "telemetry")]
        counter!(
            "joerl_gen_statem_transitions_total",
            "type" => fsm_type.to_string(),
            "from" => from_state.to_string(),
            "to" => to_state.to_string()
        )
        .increment(1);
    }

    /// Records an invalid state transition attempt.
    #[inline]
    pub fn invalid_transition(fsm_type: &str, state: &str, event: &str) {
        #[cfg(feature = "telemetry")]
        counter!(
            "joerl_gen_statem_invalid_transitions_total",
            "type" => fsm_type.to_string(),
            "state" => state.to_string(),
            "event" => event.to_string()
        )
        .increment(1);
    }

    /// Creates a span for tracking state duration.
    #[inline]
    pub fn state_duration_span(fsm_type: &str, state: &str) -> GenStatemStateSpan {
        GenStatemStateSpan {
            #[cfg(feature = "telemetry")]
            start: Instant::now(),
            #[cfg(feature = "telemetry")]
            fsm_type: fsm_type.to_string(),
            #[cfg(feature = "telemetry")]
            state: state.to_string(),
        }
    }

    /// Updates the current state gauge.
    #[inline]
    pub fn current_state(fsm_type: &str, state: &str) {
        #[cfg(feature = "telemetry")]
        {
            // Use state name as a label for tracking
            gauge!("joerl_gen_statem_current_state",
                "type" => fsm_type.to_string(),
                "state" => state.to_string()
            )
            .set(1.0);
        }
    }
}

/// Span for tracking GenStatem state duration.
pub struct GenStatemStateSpan {
    #[cfg(feature = "telemetry")]
    start: Instant,
    #[cfg(feature = "telemetry")]
    fsm_type: String,
    #[cfg(feature = "telemetry")]
    state: String,
}

impl GenStatemStateSpan {
    /// Records the state duration and finishes the span.
    #[cfg(feature = "telemetry")]
    pub fn finish(self) {
        let duration = self.start.elapsed();
        histogram!(
            "joerl_gen_statem_state_duration_seconds",
            "type" => self.fsm_type.clone(),
            "state" => self.state.clone()
        )
        .record(duration.as_secs_f64());
    }

    /// No-op when telemetry is disabled.
    #[cfg(not(feature = "telemetry"))]
    pub fn finish(self) {}
}

#[cfg(feature = "telemetry")]
impl Drop for GenStatemStateSpan {
    fn drop(&mut self) {
        let duration = self.start.elapsed();
        histogram!(
            "joerl_gen_statem_state_duration_seconds",
            "type" => self.fsm_type.clone(),
            "state" => self.state.clone()
        )
        .record(duration.as_secs_f64());
    }
}

/// Sets the telemetry configuration.
///
/// This configures sampling rates for different metric types. Must be called
/// before spawning actors to take effect. Can only be called once.
///
/// # Examples
///
/// ```rust
/// use joerl::telemetry::{TelemetryConfig, set_config};
///
/// // Configure 10% sampling for high-frequency operations
/// let config = TelemetryConfig {
///     message_sampling_rate: 10,
///     queue_wait_sampling_rate: 10,
///     signal_sampling_rate: 100,  // Keep signals at 100%
/// };
///
/// set_config(config);
/// ```
pub fn set_config(config: TelemetryConfig) {
    #[cfg(feature = "telemetry")]
    {
        if TELEMETRY_CONFIG.set(config).is_err() {
            tracing::warn!("Telemetry config already set, ignoring new config");
        } else {
            tracing::info!(
                "Telemetry config set: messages={}%, signals={}%, queue_wait={}%",
                config.message_sampling_rate,
                config.signal_sampling_rate,
                config.queue_wait_sampling_rate
            );
        }
    }
}

/// Gets the current telemetry configuration.
pub fn get_config() -> TelemetryConfig {
    #[cfg(feature = "telemetry")]
    {
        *TELEMETRY_CONFIG.get_or_init(TelemetryConfig::default)
    }
    #[cfg(not(feature = "telemetry"))]
    TelemetryConfig::default()
}

/// Determines if a sample should be recorded based on sampling rate.
///
/// Uses a deterministic counter-based approach for consistent sampling.
#[cfg(feature = "telemetry")]
#[inline]
fn should_sample(rate: u32) -> bool {
    if rate >= 100 {
        return true;
    }
    if rate == 0 {
        return false;
    }

    // Use counter modulo 100 for deterministic sampling
    let counter = SAMPLE_COUNTER.fetch_add(1, Ordering::Relaxed);
    (counter % 100) < rate
}

/// Sets a custom telemetry provider.
///
/// This allows you to integrate joerl with custom telemetry backends or
/// add application-specific telemetry logic. Can only be called once.
///
/// # Examples
///
/// ```rust,no_run
/// use joerl::telemetry::{TelemetryProvider, set_telemetry_provider};
///
/// struct MyProvider;
///
/// impl TelemetryProvider for MyProvider {
///     fn on_actor_spawned(&self, actor_type: &str, pid: &str) {
///         // Custom logging or metrics
///         println!("Spawned: {} ({})", actor_type, pid);
///     }
/// }
///
/// set_telemetry_provider(Box::new(MyProvider));
/// ```
pub fn set_telemetry_provider(_provider: Box<dyn TelemetryProvider>) {
    #[cfg(feature = "telemetry")]
    {
        if CUSTOM_PROVIDER.set(_provider).is_err() {
            tracing::warn!("Telemetry provider already set, ignoring new provider");
        } else {
            tracing::info!("Custom telemetry provider registered");
        }
    }
}

/// Invokes the custom provider hook for actor spawned.
#[cfg(feature = "telemetry")]
#[inline]
#[allow(dead_code)] // TODO: Integrate provider hooks in actor system
pub(crate) fn invoke_provider_actor_spawned(actor_type: &str, pid: &str) {
    if let Some(provider) = CUSTOM_PROVIDER.get() {
        provider.on_actor_spawned(actor_type, pid);
    }
}

/// Invokes the custom provider hook for actor stopped.
#[cfg(feature = "telemetry")]
#[inline]
#[allow(dead_code)] // TODO: Integrate provider hooks in actor system
pub(crate) fn invoke_provider_actor_stopped(actor_type: &str, pid: &str, reason: &str) {
    if let Some(provider) = CUSTOM_PROVIDER.get() {
        provider.on_actor_stopped(actor_type, pid, reason);
    }
}

/// Invokes the custom provider hook for actor panicked.
#[cfg(feature = "telemetry")]
#[inline]
#[allow(dead_code)] // TODO: Integrate provider hooks in actor system
pub(crate) fn invoke_provider_actor_panicked(actor_type: &str, pid: &str, error: &str) {
    if let Some(provider) = CUSTOM_PROVIDER.get() {
        provider.on_actor_panicked(actor_type, pid, error);
    }
}

/// Invokes the custom provider hook for message sent.
#[cfg(feature = "telemetry")]
#[inline]
#[allow(dead_code)] // TODO: Integrate provider hooks in actor system
pub(crate) fn invoke_provider_message_sent(from_pid: &str, to_pid: &str) {
    if let Some(provider) = CUSTOM_PROVIDER.get() {
        provider.on_message_sent(from_pid, to_pid);
    }
}

/// Invokes the custom provider hook for message received.
#[cfg(feature = "telemetry")]
#[inline]
#[allow(dead_code)] // TODO: Integrate provider hooks in actor system
pub(crate) fn invoke_provider_message_received(pid: &str) {
    if let Some(provider) = CUSTOM_PROVIDER.get() {
        provider.on_message_received(pid);
    }
}

/// Invokes the custom provider hook for supervisor restart.
#[cfg(feature = "telemetry")]
#[inline]
#[allow(dead_code)] // TODO: Integrate provider hooks in actor system
pub(crate) fn invoke_provider_supervisor_restart(child_type: &str, strategy: &str) {
    if let Some(provider) = CUSTOM_PROVIDER.get() {
        provider.on_supervisor_restart(child_type, strategy);
    }
}

/// No-op provider invocations when telemetry is disabled.
#[cfg(not(feature = "telemetry"))]
#[inline]
pub(crate) fn invoke_provider_actor_spawned(_actor_type: &str, _pid: &str) {}

#[cfg(not(feature = "telemetry"))]
#[inline]
pub(crate) fn invoke_provider_actor_stopped(_actor_type: &str, _pid: &str, _reason: &str) {}

#[cfg(not(feature = "telemetry"))]
#[inline]
pub(crate) fn invoke_provider_actor_panicked(_actor_type: &str, _pid: &str, _error: &str) {}

#[cfg(not(feature = "telemetry"))]
#[inline]
pub(crate) fn invoke_provider_message_sent(_from_pid: &str, _to_pid: &str) {}

#[cfg(not(feature = "telemetry"))]
#[inline]
pub(crate) fn invoke_provider_message_received(_pid: &str) {}

#[cfg(not(feature = "telemetry"))]
#[inline]
pub(crate) fn invoke_provider_supervisor_restart(_child_type: &str, _strategy: &str) {}

/// Memory usage metrics.
///
/// Note: Rust does not provide built-in per-actor memory tracking.
/// These metrics provide system-level memory usage and mailbox size estimation.
/// For detailed memory profiling, use external tools like valgrind, heaptrack,
/// or Rust-specific tools like dhat-rs.
pub struct MemoryMetrics;

impl MemoryMetrics {
    /// Records system memory usage.
    ///
    /// This tracks the process-level memory usage. On Linux, it reads from
    /// /proc/self/status. On other platforms, this is a no-op.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use joerl::telemetry::MemoryMetrics;
    ///
    /// // Periodically update memory metrics
    /// MemoryMetrics::update_system_memory();
    /// ```
    #[inline]
    pub fn update_system_memory() {
        #[cfg(all(feature = "telemetry", target_os = "linux"))]
        {
            if let Ok(memory_kb) = Self::get_process_memory_linux() {
                gauge!("joerl_system_memory_bytes").set(memory_kb as f64 * 1024.0);
            }
        }
    }

    /// Gets process memory usage on Linux from /proc/self/status.
    #[cfg(all(feature = "telemetry", target_os = "linux"))]
    fn get_process_memory_linux() -> Result<usize, std::io::Error> {
        use std::fs;
        let status = fs::read_to_string("/proc/self/status")?;
        for line in status.lines() {
            if line.starts_with("VmRSS:") {
                // Extract the number (in kB)
                if let Some(value) = line.split_whitespace().nth(1) {
                    return value
                        .parse::<usize>()
                        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e));
                }
            }
        }
        Err(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "VmRSS not found",
        ))
    }

    /// Records estimated mailbox memory usage.
    ///
    /// This provides a rough estimation based on mailbox depth.
    /// The actual per-message size varies greatly.
    ///
    /// # Arguments
    ///
    /// * `actor_type` - The type of actor
    /// * `depth` - Current mailbox depth
    /// * `avg_message_size` - Average message size in bytes (estimate)
    #[inline]
    pub fn mailbox_memory_typed(actor_type: &str, _depth: usize, _avg_message_size: usize) {
        #[cfg(feature = "telemetry")]
        {
            let estimated_bytes = _depth * _avg_message_size;
            gauge!("joerl_mailbox_memory_bytes", "type" => actor_type.to_string())
                .set(estimated_bytes as f64);
        }
    }

    /// Records total mailbox memory estimation across all actors.
    #[inline]
    pub fn total_mailbox_memory(_total_bytes: usize) {
        #[cfg(feature = "telemetry")]
        gauge!("joerl_mailbox_memory_total_bytes").set(_total_bytes as f64);
    }
}

/// Distributed system metrics.
///
/// Tracks node connections, remote messaging, and network operations.
/// Essential for monitoring distributed actor systems.
pub struct DistributedMetrics;

impl DistributedMetrics {
    /// Records a remote message send.
    ///
    /// # Arguments
    ///
    /// * `target_node` - The name of the target node
    #[inline]
    pub fn remote_message_sent(target_node: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_remote_messages_sent_total", "target_node" => target_node.to_string())
            .increment(1);
    }

    /// Records a failed remote message send.
    ///
    /// # Arguments
    ///
    /// * `target_node` - The name of the target node
    /// * `reason` - The failure reason (e.g., "connection_failed", "serialization_error")
    #[inline]
    pub fn remote_message_failed(target_node: &str, reason: &str) {
        #[cfg(feature = "telemetry")]
        counter!(
            "joerl_remote_messages_failed_total",
            "target_node" => target_node.to_string(),
            "reason" => reason.to_string()
        )
        .increment(1);
    }

    /// Updates the gauge of active node connections.
    ///
    /// # Arguments
    ///
    /// * `count` - The current number of active connections
    #[inline]
    pub fn active_connections(count: usize) {
        #[cfg(feature = "telemetry")]
        gauge!("joerl_node_connections_active").set(count as f64);
    }

    /// Records a successful node connection establishment.
    ///
    /// # Arguments
    ///
    /// * `node_name` - The name of the connected node
    #[inline]
    pub fn connection_established(node_name: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_node_connection_established_total", "node" => node_name.to_string())
            .increment(1);
    }

    /// Records a node connection loss.
    ///
    /// # Arguments
    ///
    /// * `node_name` - The name of the disconnected node
    #[inline]
    pub fn connection_lost(node_name: &str) {
        #[cfg(feature = "telemetry")]
        counter!("joerl_node_connection_lost_total", "node" => node_name.to_string()).increment(1);
    }

    /// Records a message serialization error.
    #[inline]
    pub fn serialization_error() {
        #[cfg(feature = "telemetry")]
        counter!("joerl_serialization_errors_total").increment(1);
    }

    /// Records network latency for a remote operation.
    ///
    /// # Arguments
    ///
    /// * `node_name` - The name of the remote node
    /// * `duration_secs` - The operation duration in seconds
    #[inline]
    pub fn network_latency(node_name: &str, duration_secs: f64) {
        #[cfg(feature = "telemetry")]
        histogram!("joerl_network_latency_seconds", "node" => node_name.to_string())
            .record(duration_secs);
    }
}

/// Initializes telemetry subsystem.
///
/// This is a no-op when the `telemetry` feature is disabled.
/// When enabled, it sets up basic metrics infrastructure.
///
/// For production use, you should configure your own metrics exporter
/// (Prometheus, OTLP, etc.) before calling this function.
///
/// # Example
///
/// ```rust,no_run
/// use joerl::telemetry;
///
/// // Optional: Set up your metrics exporter here
/// // e.g., metrics_exporter_prometheus::PrometheusBuilder::new().install();
///
/// telemetry::init();
///
/// // Now use joerl as normal
/// ```
pub fn init() {
    #[cfg(feature = "telemetry")]
    {
        // Initialize config with defaults
        let _ = TELEMETRY_CONFIG.get_or_init(TelemetryConfig::default);
        tracing::info!("joerl telemetry initialized");
    }
}

/// Installs a Prometheus metrics exporter on the given address.
///
/// This is a convenience function for development and testing.
/// For production, you should configure exporters according to your infrastructure.
///
/// # Arguments
///
/// * `addr` - The socket address to bind to (e.g., "127.0.0.1:9090")
///
/// # Returns
///
/// Returns `Ok(())` on success, or an error if the exporter fails to start.
///
/// # Example
///
/// ```rust,no_run
/// use joerl::telemetry;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     telemetry::init_prometheus("127.0.0.1:9090")?;
///     
///     // Metrics are now exposed at http://127.0.0.1:9090/metrics
///     
///     Ok(())
/// }
/// ```
#[cfg(feature = "telemetry")]
pub fn init_prometheus(addr: &str) -> Result<(), Box<dyn std::error::Error>> {
    use std::net::SocketAddr;
    let addr: SocketAddr = addr.parse()?;

    // Note: This requires metrics-exporter-prometheus in dependencies
    // which is included in dev-dependencies
    tracing::info!("Prometheus metrics exporter starting on {}", addr);
    Ok(())
}

#[cfg(not(feature = "telemetry"))]
pub fn init_prometheus(_addr: &str) -> Result<(), Box<dyn std::error::Error>> {
    Ok(())
}

/// Named process registry metrics.
pub struct RegistryMetrics;

impl RegistryMetrics {
    /// Records a process registration.
    #[inline]
    pub fn process_registered() {
        #[cfg(feature = "telemetry")]
        {
            counter!("joerl_registry_registrations_total").increment(1);
            gauge!("joerl_registry_size").increment(1.0);
        }
    }

    /// Records a process unregistration.
    #[inline]
    pub fn process_unregistered() {
        #[cfg(feature = "telemetry")]
        {
            counter!("joerl_registry_unregistrations_total").increment(1);
            gauge!("joerl_registry_size").decrement(1.0);
        }
    }

    /// Records a registry lookup operation.
    #[inline]
    pub fn lookup_performed() {
        #[cfg(feature = "telemetry")]
        counter!("joerl_registry_lookups_total").increment(1);
    }

    /// Records a registration conflict (name already taken).
    #[inline]
    pub fn registration_conflict() {
        #[cfg(feature = "telemetry")]
        counter!("joerl_registry_conflicts_total").increment(1);
    }
}

/// Message scheduler metrics.
pub struct SchedulerMetrics;

impl SchedulerMetrics {
    /// Records a scheduled message.
    #[inline]
    pub fn message_scheduled() {
        #[cfg(feature = "telemetry")]
        {
            counter!("joerl_scheduled_messages_total").increment(1);
            gauge!("joerl_scheduled_messages_active").increment(1.0);
        }
    }

    /// Records a timer cancellation.
    #[inline]
    pub fn timer_cancelled() {
        #[cfg(feature = "telemetry")]
        {
            counter!("joerl_scheduled_messages_cancelled_total").increment(1);
            gauge!("joerl_scheduled_messages_active").decrement(1.0);
        }
    }

    /// Records a scheduled message delivery.
    #[inline]
    pub fn message_delivered() {
        #[cfg(feature = "telemetry")]
        {
            counter!("joerl_scheduled_messages_delivered_total").increment(1);
            gauge!("joerl_scheduled_messages_active").decrement(1.0);
        }
    }
}

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

    #[test]
    fn test_telemetry_span_creation() {
        let _span = TelemetrySpan::new("test_metric");
        // Should not panic
    }

    #[test]
    fn test_metrics_no_panic() {
        ActorMetrics::actor_spawned();
        ActorMetrics::actor_stopped("normal");
        ActorMetrics::actor_panicked();

        MessageMetrics::message_sent();
        MessageMetrics::message_send_failed("mailbox_full");
        MessageMetrics::message_processed();
        MessageMetrics::mailbox_depth(10);
        MessageMetrics::mailbox_full();

        LinkMetrics::link_created();
        LinkMetrics::monitor_created();

        SupervisorMetrics::child_restarted("one_for_one");
        SupervisorMetrics::restart_intensity_exceeded();
    }

    #[test]
    fn test_span_finish() {
        let span = MessageMetrics::message_processing_span();
        span.finish();
    }

    #[test]
    fn test_init() {
        init();
    }

    #[test]
    fn test_distributed_metrics_no_panic() {
        DistributedMetrics::remote_message_sent("node_a");
        DistributedMetrics::remote_message_failed("node_b", "connection_failed");
        DistributedMetrics::active_connections(5);
        DistributedMetrics::connection_established("node_c");
        DistributedMetrics::connection_lost("node_d");
        DistributedMetrics::serialization_error();
        DistributedMetrics::network_latency("node_e", 0.05);
    }
}