metrics library
Advanced metrics system for monitoring channel performance.
This module provides comprehensive metrics collection and export capabilities for channel operations. Use this for production monitoring, performance profiling, and observability in high-throughput applications.
Usage Patterns
import 'package:cross_channel/cross_channel.dart';
import 'package:cross_channel/metrics.dart';
Future<void> main() async {
// 1. Enable metrics globally
MetricsConfig.enabled = true;
MetricsConfig.sampleLatency = true;
// 2. Configure exporter (Standard output)
MetricsConfig.exporter = StdExporter();
// 3. Create channels with metrics
final (tx, rx) = XChannel.mpsc<String>(
capacity: 100,
metricsId: 'data_pipeline',
);
// 4. Metrics collected automatically
await tx.send('performance data');
await rx.recv();
// 5. Access snapshots (advanced)
final snapshot = MetricsRegistry().snapshot();
print('Total messages: ${snapshot.sent}');
}
Performance Impact
- Disabled: unmeasured
- Enabled: unmeasured
- Latency sampling: unmeasured
Metrics Available
Channel Operations:
sent,recv- Total operation countsdropped,closed- Loss and lifecycle eventstrySendOk/Fail,tryRecvOk/Empty- Non-blocking operation results
Latency Quantiles (P² algorithm):
sendP50,sendP95,sendP99- Send operation latenciesrecvP50,recvP95,recvP99- Receive operation latencies
Performance Metrics:
mops- Million operations per secondns_per_op- Nanoseconds per operationdrop_rate- Percentage of dropped messages
This is an advanced feature - most users should start with the main
cross_channel import for basic channel usage.
Classes
- ActiveMetricsRecorder
- ChannelMetrics
- ChannelSnapshot
- CsvExporter
- GlobalMetrics
- MetricsConfig
- Global metrics configuration (lightweight singleton pattern).
- MetricsExporter
- MetricsRecorder
- MetricsRegistry
- NoopExporter
- NoopMetricsRecorder
- StdExporter
Functions
-
buildMetricsRecorder(
String? channelId) → MetricsRecorder