graphyne 0.2.5

A simple rust client for sending messages to Graphite
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
//! # Graphyne
//!
//! A simple, reliable Rust client for sending metrics to [Graphite](https://graphiteapp.org/) Carbon daemons.
//!
//! Graphyne provides a straightforward API for sending time-series metrics to Graphite using either
//! the plaintext TCP or UDP protocol. It features automatic reconnection (TCP), configurable retry
//! logic, and an ergonomic builder pattern for easy configuration.
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use graphyne::{GraphiteClient, GraphiteMessage, Protocol};
//! use std::time::Duration;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a TCP client (default)
//! let mut client = GraphiteClient::builder()
//!     .address("127.0.0.1")
//!     .port(2003)
//!     .build()?;
//!
//! // Or create a UDP client
//! let mut udp_client = GraphiteClient::builder()
//!     .address("127.0.0.1")
//!     .port(2003)
//!     .protocol(Protocol::Udp)
//!     .build()?;
//!
//! // Send a metric
//! let message = GraphiteMessage::new("my.metric.path", "42");
//! client.send_message(&message)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Features
//!
//! - **Builder Pattern**: Intuitive, type-safe configuration
//! - **TCP & UDP Support**: Choose between reliable TCP or high-performance UDP
//! - **Auto-reconnection**: Automatic retry and reconnection on failure (TCP)
//! - **Zero-copy Writes**: Efficient metric transmission
//! - **Timestamp Generation**: Automatic Unix timestamp creation
//!
//! ## Protocol
//!
//! Graphyne uses the Graphite plaintext protocol over TCP or UDP. Each metric is formatted as:
//! ```text
//! metric.path.name value timestamp\n
//! ```
//!
//! For example:
//! ```text
//! servers.web01.cpu.usage 45.2 1609459200\n
//! ```

use bon::bon;
use std::{
    fmt,
    io::{Error, Write},
    net::{AddrParseError, IpAddr, SocketAddr, TcpStream, UdpSocket},
    str::FromStr,
    time::{Duration, SystemTime, UNIX_EPOCH},
};

/// Default number of retry attempts for connection and send operations.
///
/// If a connection or send fails, the client will retry up to this many times
/// before returning an error.
const DEFAULT_RETRIES: u8 = 3;

/// Default timeout duration for TCP connection attempts.
///
/// This timeout applies to both initial connections and reconnection attempts.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);

/// Default time to live for TCP packets
const DEFAULT_TCP_TTL: Duration = Duration::from_secs(240);

/// Protocol to use for sending metrics to Graphite.
///
/// # TCP vs UDP
///
/// - **TCP**: Reliable delivery with connection management and automatic retry.
///   Use for critical metrics where delivery must be guaranteed.
/// - **UDP**: Fire-and-forget delivery with no connection overhead.
///   Use for high-throughput scenarios where occasional metric loss is acceptable.
///
/// # Examples
///
/// ```rust,no_run
/// use graphyne::{GraphiteClient, Protocol};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // TCP for critical metrics
/// let tcp_client = GraphiteClient::builder()
///     .address("127.0.0.1")
///     .port(2003)
///     .protocol(Protocol::Tcp)
///     .build()?;
///
/// // UDP for high-volume metrics
/// let udp_client = GraphiteClient::builder()
///     .address("127.0.0.1")
///     .port(2003)
///     .protocol(Protocol::Udp)
///     .build()?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Protocol {
    #[default]
    Tcp,
    Udp,
}

/// Connection type for the Graphite client.
#[derive(Debug)]
enum Connection {
    Tcp(TcpStream),
    Udp(UdpSocket),
}

/// A client for sending metrics to a Graphite Carbon daemon.
///
/// `GraphiteClient` maintains a connection (TCP) or socket (UDP) to a Graphite server and provides
/// methods for sending metrics. For TCP, it automatically handles connection failures with
/// configurable retry logic.
///
/// # Protocol Selection
///
/// The client supports both TCP and UDP protocols:
/// - **TCP** (default): Provides reliable delivery with automatic reconnection
/// - **UDP**: Provides higher throughput with no connection overhead
///
/// # Connection Management
///
/// **TCP**: The client maintains a persistent connection which is automatically reestablished if it
/// fails. When `send_message` encounters a connection error, it will attempt to reconnect
/// up to `retries` times before failing.
///
/// **UDP**: No connection management is needed; packets are sent directly without retries.
///
/// # Thread Safety
///
/// `GraphiteClient` is **not** thread-safe due to the mutable reference required by `send_message`.
/// For concurrent access, wrap it in a `Mutex` or use multiple client instances.
///
/// # Examples
///
/// ## TCP Usage
///
/// ```rust,no_run
/// use graphyne::{GraphiteClient, GraphiteMessage, Protocol};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = GraphiteClient::builder()
///     .address("127.0.0.1")
///     .port(2003)
///     .protocol(Protocol::Tcp)
///     .build()?;
///
/// let msg = GraphiteMessage::new("app.requests", "100");
/// client.send_message(&msg)?;
/// # Ok(())
/// # }
/// ```
///
/// ## UDP Usage
///
/// ```rust,no_run
/// use graphyne::{GraphiteClient, GraphiteMessage, Protocol};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = GraphiteClient::builder()
///     .address("127.0.0.1")
///     .port(2003)
///     .protocol(Protocol::Udp)
///     .build()?;
///
/// let msg = GraphiteMessage::new("app.requests", "100");
/// client.send_message(&msg)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct GraphiteClient {
    /// The connection to the Graphite server.
    connection: Connection,

    /// Socket address used for reconnection attempts and UDP sends.
    sock_addr: SocketAddr,

    /// Original address string (currently unused but reserved for future DNS support).
    _address: String,

    /// Original port number (currently unused but reserved for future use).
    _port: u16,

    /// Protocol being used (TCP or UDP).
    protocol: Protocol,

    /// Number of times to retry failed operations (TCP only).
    ///
    /// This applies to both connection attempts and send operations. A value of 3
    /// means up to 4 total attempts (1 initial + 3 retries).
    retries: u8,

    /// Timeout duration for connection attempts (TCP only).
    ///
    /// This timeout is applied to each individual connection attempt during both
    /// initial connection and reconnection operations.
    timeout: Duration,

    /// Time to live for tcp packets (TCP only).
    tcp_ttl: Duration,
}

#[bon]
impl GraphiteClient {
    /// Creates a new `GraphiteClient` using the builder pattern.
    ///
    /// This constructor establishes an initial connection (TCP) or creates a socket (UDP)
    /// to the Graphite server. If the operation fails, it returns a `GraphiteError`.
    ///
    /// # Arguments
    ///
    /// * `address` - IP address of the Graphite server (IPv4 or IPv6). **Note**: DNS hostnames
    ///   are not currently supported.
    /// * `port` - Port number where the Carbon daemon is listening (typically 2003 for TCP,
    ///   2003 for UDP plaintext)
    /// * `protocol` - Protocol to use: `Protocol::Tcp` (default) or `Protocol::Udp`
    /// * `retries` - Number of retry attempts for failed operations (default: 3, TCP only)
    /// * `timeout` - Maximum duration to wait for connection attempts (default: 5 seconds, TCP only)
    /// * `tcp_ttl` - Time to live for TCP packets (default: 240 seconds, TCP only)
    ///
    /// # Returns
    ///
    /// Returns `Ok(GraphiteClient)` if successful, or `Err(GraphiteError)` if:
    /// - The address cannot be parsed as an IP address
    /// - The connection times out (TCP)
    /// - The connection is refused (TCP)
    /// - The socket cannot be created (UDP)
    ///
    /// # Examples
    ///
    /// ## TCP with defaults
    ///
    /// ```rust,no_run
    /// use graphyne::GraphiteClient;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = GraphiteClient::builder()
    ///     .address("10.0.0.5")
    ///     .port(2003)
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## UDP for high-throughput
    ///
    /// ```rust,no_run
    /// use graphyne::{GraphiteClient, Protocol};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = GraphiteClient::builder()
    ///     .address("10.0.0.5")
    ///     .port(2003)
    ///     .protocol(Protocol::Udp)
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## TCP with custom retry and timeout
    ///
    /// ```rust,no_run
    /// use graphyne::{GraphiteClient, Protocol};
    /// use std::time::Duration;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = GraphiteClient::builder()
    ///     .address("192.168.1.100")
    ///     .port(2003)
    ///     .protocol(Protocol::Tcp)
    ///     .retries(10)
    ///     .timeout(Duration::from_millis(500))
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    #[builder]
    pub fn new(
        /// IP address of the Graphite server (IPv4 or IPv6).
        ///
        /// **Note**: DNS hostnames are not currently supported.
        address: impl Into<String>,
        /// Port number where the Carbon daemon is listening (typically 2003)
        port: u16,
        /// Protocol to use: TCP (default) or UDP
        #[builder(default = Protocol::default())]
        protocol: Protocol,
        /// Number of times to retry failed operations (TCP only).
        ///
        /// This applies to both connection attempts and send operations.
        /// A value of 3 means up to 4 total attempts (1 initial + 3 retries).
        #[builder(default = DEFAULT_RETRIES)]
        retries: u8,
        /// Timeout duration for connection attempts (TCP only).
        ///
        /// This timeout is applied to each individual connection attempt during both
        /// initial connection and reconnection operations.
        #[builder(default = DEFAULT_TIMEOUT)]
        timeout: Duration,

        /// Time to live for tcp packets (TCP only).
        #[builder(default = DEFAULT_TCP_TTL)]
        tcp_ttl: Duration,
    ) -> Result<Self, GraphiteError> {
        let address = address.into();
        let sock_addr = SocketAddr::new(IpAddr::from_str(&address)?, port);

        let connection = match protocol {
            Protocol::Tcp => {
                let tcp_stream = TcpStream::connect_timeout(&sock_addr, timeout)?;
                tcp_stream.set_ttl(tcp_ttl.as_secs() as u32)?;
                tcp_stream.set_nodelay(true)?;
                Connection::Tcp(tcp_stream)
            }
            Protocol::Udp => {
                let udp_socket = UdpSocket::bind("0.0.0.0:0")?;
                udp_socket.connect(sock_addr)?;
                Connection::Udp(udp_socket)
            }
        };

        Ok(Self {
            connection,
            sock_addr,
            _address: address,
            _port: port,
            protocol,
            retries,
            timeout,
            tcp_ttl,
        })
    }

    /// Attempts to reestablish the TCP connection to the Graphite server.
    ///
    /// This method tries to create a new connection up to `retries` times, replacing the
    /// existing connection if successful. It's called automatically by `send_message` when
    /// a send operation fails, but can also be called manually.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if reconnection succeeds, or `Err(GraphiteError)` if all retry
    /// attempts are exhausted.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use graphyne::{GraphiteClient, GraphiteMessage};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = GraphiteClient::builder()
    ///     .address("127.0.0.1")
    ///     .port(2003)
    ///     .build()?;
    ///
    /// // Manually reconnect if needed
    /// client.reconnect()?;
    ///
    /// let msg = GraphiteMessage::new("test.metric", "1");
    /// client.send_message(&msg)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn reconnect(&mut self) -> Result<(), GraphiteError> {
        // UDP doesn't need reconnection
        if self.protocol == Protocol::Udp {
            return Ok(());
        }

        let mut last_err: Error = Error::last_os_error();
        let mut i = 0;
        while i < self.retries {
            let connect = TcpStream::connect_timeout(&self.sock_addr, self.timeout);
            match connect {
                Ok(tcp) => {
                    tcp.set_ttl(self.tcp_ttl.as_secs() as u32)?;
                    tcp.set_nodelay(true)?;
                    self.connection = Connection::Tcp(tcp);
                    return Ok(());
                }
                Err(err) => last_err = err,
            }
            i += 1;
        }
        Err(GraphiteError {
            msg: format!("Graphite Error: {last_err}"),
        })
    }

    /// Sends a metric message to the Graphite server.
    ///
    /// This method writes the formatted metric to the connection. Behavior differs by protocol:
    ///
    /// **TCP**: If the write fails (e.g., due to a broken connection), it automatically attempts
    /// to reconnect and retry the send operation up to `retries` times.
    ///
    /// **UDP**: Sends the metric as a datagram. No retry logic is applied; if the send fails,
    /// an error is returned immediately.
    ///
    /// # Arguments
    ///
    /// * `msg` - A reference to the `GraphiteMessage` to send
    ///
    /// # Returns
    ///
    /// Returns `Ok(usize)` with the number of bytes written if successful, or
    /// `Err(GraphiteError)` if all retry attempts fail (TCP) or the send fails (UDP).
    ///
    /// # Examples
    ///
    /// ## TCP: Single metric with retry
    ///
    /// ```rust,no_run
    /// use graphyne::{GraphiteClient, GraphiteMessage, Protocol};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = GraphiteClient::builder()
    ///     .address("127.0.0.1")
    ///     .port(2003)
    ///     .protocol(Protocol::Tcp)
    ///     .build()?;
    ///
    /// let msg = GraphiteMessage::new("cpu.usage", "45.2");
    /// let bytes_sent = client.send_message(&msg)?;
    /// println!("Sent {} bytes", bytes_sent);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## UDP: High-throughput metrics
    ///
    /// ```rust,no_run
    /// use graphyne::{GraphiteClient, GraphiteMessage, Protocol};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = GraphiteClient::builder()
    ///     .address("127.0.0.1")
    ///     .port(2003)
    ///     .protocol(Protocol::Udp)
    ///     .build()?;
    ///
    /// let msg = GraphiteMessage::new("requests.count", "1000");
    /// client.send_message(&msg)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn send_message(&mut self, msg: &GraphiteMessage) -> Result<usize, GraphiteError> {
        let data = msg.to_string();

        match &mut self.connection {
            Connection::Udp(udp_socket) => {
                udp_socket.send(data.as_bytes())?;
                Ok(data.len())
            }
            Connection::Tcp(_) => {
                let mut last_err: Error = Error::last_os_error();
                // Handle TCP with retries
                for _ in 0..self.retries {
                    if let Connection::Tcp(tcp_stream) = &mut self.connection {
                        let res = tcp_stream.write_all(data.as_bytes());
                        match res {
                            Ok(_) => return Ok(data.len()),
                            Err(err) => {
                                last_err = err;
                                // Reconnect after error
                                self.reconnect()?;
                            }
                        }
                    }
                }

                Err(GraphiteError {
                    msg: format!("Graphite Error: {last_err}"),
                })
            }
        }
    }

    /// Sends multiple metric messages in a single operation.
    ///
    /// This method combines multiple messages and sends them together, which can be more
    /// efficient than sending messages individually.
    ///
    /// **TCP**: Messages are concatenated and sent over the TCP stream with retry logic.
    ///
    /// **UDP**: Messages are concatenated and sent as a single datagram. Note that large
    /// batches may exceed UDP packet size limits (typically ~64KB).
    ///
    /// # Arguments
    ///
    /// * `msgs` - A slice of `GraphiteMessage` references to send
    ///
    /// # Returns
    ///
    /// Returns `Ok(usize)` with the total number of bytes written if successful, or
    /// `Err(GraphiteError)` if the operation fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use graphyne::{GraphiteClient, GraphiteMessage};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = GraphiteClient::builder()
    ///     .address("127.0.0.1")
    ///     .port(2003)
    ///     .build()?;
    ///
    /// let metrics = vec![
    ///     GraphiteMessage::new("server1.cpu", "45"),
    ///     GraphiteMessage::new("server1.memory", "80"),
    ///     GraphiteMessage::new("server1.disk", "65"),
    /// ];
    ///
    /// client.send_batch_message(&metrics)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn send_batch_message(&mut self, msgs: &[GraphiteMessage]) -> Result<usize, GraphiteError> {
        match &mut self.connection {
            Connection::Udp(udp_socket) => {
                /// Maximum total payload size per UDP message.
                const MAX_UDP_SIZE: usize = 1400;
                let mut total_sent_bytes = 0;
                let mut current_batch = String::with_capacity(MAX_UDP_SIZE);

                for msg in msgs {
                    let msg_str = msg.to_string();
                    if !current_batch.is_empty()
                        && current_batch.len() + msg_str.len() > MAX_UDP_SIZE
                    {
                        udp_socket.send(current_batch.as_bytes())?;
                        total_sent_bytes += current_batch.len();
                        current_batch.clear();
                    }

                    // If the size of the message is larger than 1400 bytes, we need to send it
                    // on its own with the understanding that it may be truncated during transmit.
                    if msg_str.len() > MAX_UDP_SIZE {
                        udp_socket.send(msg_str.as_bytes())?;
                        total_sent_bytes += msg_str.len();
                    } else {
                        current_batch.push_str(&msg_str);
                    }
                }

                if !current_batch.is_empty() {
                    udp_socket.send(current_batch.as_bytes())?;
                    total_sent_bytes += current_batch.len();
                }

                Ok(total_sent_bytes)
            }
            Connection::Tcp(_) => {
                let combined: String = msgs.iter().map(ToString::to_string).collect();
                let mut last_err: Error = Error::last_os_error();
                // Handle TCP with retries
                for _ in 0..self.retries {
                    if let Connection::Tcp(tcp_stream) = &mut self.connection {
                        let res = tcp_stream.write_all(combined.as_bytes());
                        match res {
                            Ok(_) => return Ok(combined.len()),
                            Err(err) => {
                                last_err = err;
                                // Reconnect after error
                                self.reconnect()?;
                            }
                        }
                    }
                }

                Err(GraphiteError {
                    msg: format!("Graphite Error: {last_err}"),
                })
            }
        }
    }
}

impl Drop for GraphiteClient {
    /// Any errors during shutdown are silently ignored.
    fn drop(&mut self) {
        if let Connection::Tcp(tcp_stream) = &self.connection {
            let _ = tcp_stream.shutdown(std::net::Shutdown::Both);
        }
    }
}

/// A metric message to be sent to Graphite.
///
/// `GraphiteMessage` represents a single metric data point in the Graphite plaintext protocol
/// format. It consists of a metric path, a value, and a Unix timestamp.
///
/// # Format
///
/// Messages are formatted as: `metric.path value timestamp\n`
///
/// For example: `servers.web01.cpu.usage 45.2 1609459200\n`
///
/// # Timestamps
///
/// Timestamps are automatically generated at message creation time using the system clock.
/// They represent seconds since the Unix epoch (January 1, 1970 00:00:00 UTC).
///
/// # Metric Paths
///
/// Metric paths should follow Graphite's hierarchical naming conventions using dots as
/// separators. Common patterns include:
///
/// - `company.application.server.component.metric`
/// - `environment.service.host.subsystem.value`
///
/// # Examples
///
/// ```rust
/// use graphyne::GraphiteMessage;
///
/// // Create a message for CPU usage
/// let cpu_msg = GraphiteMessage::new("servers.web01.cpu.usage", "45.2");
///
/// // Create a message for request count
/// let req_msg = GraphiteMessage::new("app.requests.total", "1000");
///
/// // Create a message with namespace hierarchy
/// let metric = GraphiteMessage::new("prod.api.gateway.latency.p95", "125");
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct GraphiteMessage {
    /// The hierarchical path identifying this metric in Graphite.
    ///
    /// Should use dots as separators (e.g., "servers.web01.cpu.usage").
    metric_path: String,

    /// The numeric value of the metric as a string.
    ///
    /// While Graphite expects numeric values, this is stored as a string to avoid
    /// unnecessary conversions and to preserve the exact formatting provided by the caller.
    value: String,

    /// Unix timestamp (seconds since epoch) when this message was created.
    ///
    /// Generated automatically at construction time using `SystemTime::now()`.
    timestamp: u64,
}

impl GraphiteMessage {
    /// Creates a new metric message with the current timestamp.
    ///
    /// The timestamp is automatically generated using the system clock at the moment
    /// this method is called.
    ///
    /// # Arguments
    ///
    /// * `metric_path` - The hierarchical path for this metric (e.g., "app.cpu.usage")
    /// * `value` - The metric value as a string (e.g., "42" or "3.14")
    ///
    /// # Examples
    ///
    /// ```rust
    /// use graphyne::GraphiteMessage;
    ///
    /// // Integer value
    /// let count = GraphiteMessage::new("requests.count", "150");
    ///
    /// // Floating point value
    /// let temp = GraphiteMessage::new("sensors.temperature", "23.5");
    ///
    /// // Large value
    /// let bytes = GraphiteMessage::new("network.bytes.sent", "1048576");
    /// ```
    pub fn new(metric_path: &str, value: &str) -> Self {
        Self {
            metric_path: metric_path.to_string(),
            value: value.to_string(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        }
    }

    /// Creates a new metric message with the provided timestamp.
    ///
    ///
    /// # Arguments
    ///
    /// * `metric_path` - The hierarchical path for this metric (e.g., "app.cpu.usage")
    /// * `value` - The metric value as a string (e.g., "42" or "3.14")
    /// * `ts` - The metric timestamp as a unix epoch.
    /// # Examples
    ///
    /// ```rust
    /// use graphyne::GraphiteMessage;
    ///
    /// // Integer value
    /// let count = GraphiteMessage::new_with_ts("requests.count", "150", 1768405993);
    ///
    /// // Floating point value
    /// let temp = GraphiteMessage::new_with_ts("sensors.temperature", "23.5", 1768405993);
    ///
    /// // Large value
    /// let bytes = GraphiteMessage::new_with_ts("network.bytes.sent", "1048576", 1768405993);
    /// ```
    pub fn new_with_ts(metric_path: &str, value: &str, ts: u64) -> Self {
        Self {
            metric_path: metric_path.to_string(),
            value: value.to_string(),
            timestamp: ts,
        }
    }
}

impl fmt::Display for GraphiteMessage {
    /// Formats the message according to the Graphite plaintext protocol.
    ///
    /// The output format is: `metric_path value timestamp\n`
    ///
    /// This format is used when sending messages to the Graphite server.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use graphyne::GraphiteMessage;
    ///
    /// let msg = GraphiteMessage::new("test.metric", "42");
    /// let formatted = msg.to_string();
    /// // Output: "test.metric 42 1609459200\n" (timestamp will vary)
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "{} {} {}", self.metric_path, self.value, self.timestamp)
    }
}

/// Error type for Graphite client operations.
///
/// `GraphiteError` wraps various error conditions that can occur during client operations,
/// including connection failures, send failures, and address parsing errors.
///
/// # Examples
///
/// ```rust
/// use graphyne::{GraphiteClient, GraphiteError};
/// use std::time::Duration;
///
/// fn try_connect() -> Result<GraphiteClient, GraphiteError> {
///     GraphiteClient::builder()
///         .address("127.0.0.1")
///         .port(2003)
///         .timeout(Duration::from_millis(100))
///         .build()
/// }
///
/// match try_connect() {
///     Ok(client) => println!("Connected successfully"),
///     Err(e) => eprintln!("Connection failed: {}", e),
/// }
/// ```
#[derive(Clone)]
pub struct GraphiteError {
    /// Human-readable error message describing what went wrong.
    pub msg: String,
}

impl fmt::Display for GraphiteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}

impl fmt::Debug for GraphiteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "GraphiteError {{ msg: {:?} }}", self.msg)
    }
}

impl std::error::Error for GraphiteError {}

impl From<AddrParseError> for GraphiteError {
    /// Converts address parsing errors into `GraphiteError`.
    ///
    /// This is called when the provided address string cannot be parsed as a valid IP address.
    fn from(err: AddrParseError) -> Self {
        GraphiteError {
            msg: err.to_string(),
        }
    }
}

impl From<Error> for GraphiteError {
    /// Converts I/O errors into `GraphiteError`.
    ///
    /// This handles connection errors, timeout errors, and write failures.
    fn from(err: Error) -> Self {
        GraphiteError {
            msg: err.to_string(),
        }
    }
}