fast-telemetry-export 0.4.0

Export adapters for fast-telemetry metrics: DogStatsD, OTLP, ClickHouse, span export, and stale-series sweeping
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
//! OTLP HTTP/protobuf metrics exporter.
//!
//! Exports metrics to an OTLP-compatible collector (OpenTelemetry Collector,
//! Grafana Alloy, Datadog OTLP intake, etc.) via HTTP POST of protobuf-encoded
//! `ExportMetricsServiceRequest` to `/v1/metrics`.
//!
//! Uses cumulative temporality — no state tracking needed between export cycles.
//! Larger payloads are gzip-compressed automatically, and failed exports retry
//! with exponential backoff.
//!
//! The actual metric collection is provided by the caller via a closure,
//! making this exporter work with any metrics struct.

use std::time::Duration;

use fast_telemetry::otlp::{build_export_request, build_resource, pb};
use prost::Message;
use tokio::time::{MissedTickBehavior, interval};
use tokio_util::sync::CancellationToken;

/// Configuration for the OTLP HTTP metrics exporter.
#[derive(Clone)]
pub struct OtlpConfig {
    /// OTLP collector endpoint (scheme + host + port), e.g. `"http://localhost:4318"`.
    /// The path `/v1/metrics` is appended automatically.
    pub endpoint: String,
    /// Export interval (default: 60s).
    pub interval: Duration,
    /// `service.name` resource attribute.
    pub service_name: String,
    /// Instrumentation scope name (default: "fast-telemetry").
    pub scope_name: String,
    /// Additional resource attributes (e.g. `("service.version", "1.0")`).
    pub resource_attributes: Vec<(String, String)>,
    /// Request timeout (default: 10s).
    pub timeout: Duration,
    /// Extra HTTP headers sent with every export request.
    ///
    /// Use this for collector authentication, e.g.:
    /// - `("Authorization", "Bearer <token>")`
    /// - `("x-api-key", "<key>")`
    pub headers: Vec<(String, String)>,
}

impl Default for OtlpConfig {
    fn default() -> Self {
        Self {
            endpoint: "http://localhost:4318".to_string(),
            interval: Duration::from_secs(60),
            service_name: "unknown_service".to_string(),
            scope_name: "fast-telemetry".to_string(),
            resource_attributes: Vec::new(),
            timeout: Duration::from_secs(10),
            headers: Vec::new(),
        }
    }
}

impl OtlpConfig {
    pub fn new(endpoint: impl Into<String>) -> Self {
        Self {
            endpoint: endpoint.into(),
            ..Default::default()
        }
    }

    pub fn with_interval(mut self, interval: Duration) -> Self {
        self.interval = interval;
        self
    }

    pub fn with_service_name(mut self, name: impl Into<String>) -> Self {
        self.service_name = name.into();
        self
    }

    pub fn with_scope_name(mut self, name: impl Into<String>) -> Self {
        self.scope_name = name.into();
        self
    }

    pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.resource_attributes.push((key.into(), value.into()));
        self
    }

    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }
}

/// Maximum backoff delay between retries after export failures.
const MAX_BACKOFF: Duration = Duration::from_secs(300);

/// Base backoff delay after the first failure.
const BASE_BACKOFF: Duration = Duration::from_secs(5);

/// Minimum payload size (bytes) before gzip compression is applied.
/// Below this threshold, compression overhead exceeds savings.
const GZIP_THRESHOLD: usize = 1024;

/// Gzip-compress `data` into `out` using fast compression (level 1).
///
/// Returns `true` if compression was applied, `false` if the payload was below
/// the threshold (in which case `out` is untouched).
fn gzip_compress(data: &[u8], out: &mut Vec<u8>) -> bool {
    if data.len() < GZIP_THRESHOLD {
        return false;
    }
    use flate2::Compression;
    use flate2::write::GzEncoder;
    use std::io::Write;

    out.clear();
    let mut encoder = GzEncoder::new(out, Compression::fast());
    let _ = encoder.write_all(data);
    let _ = encoder.finish();
    true
}

/// Send a protobuf-encoded body, applying gzip when beneficial.
async fn send_otlp(
    client: &reqwest::Client,
    url: &str,
    body: &[u8],
    gzip_buf: &mut Vec<u8>,
    extra_headers: &[(String, String)],
) -> Result<reqwest::Response, reqwest::Error> {
    let mut req = client
        .post(url)
        .header("Content-Type", "application/x-protobuf");

    for (name, value) in extra_headers {
        req = req.header(name, value);
    }

    if gzip_compress(body, gzip_buf) {
        req.header("Content-Encoding", "gzip")
            .body(gzip_buf.clone())
            .send()
            .await
    } else {
        req.body(body.to_vec()).send().await
    }
}

/// Run the OTLP metrics export loop.
///
/// `collect_fn` is called each cycle with a `&mut Vec<pb::Metric>`. The closure
/// should append OTLP metric messages (typically via `ExportMetrics::export_otlp`).
/// The exporter handles protobuf encoding, gzip compression, HTTP transport, and
/// exponential backoff on failures.
///
/// On cancellation, a final export is performed to flush pending metrics.
///
/// # Example
///
/// ```ignore
/// use std::sync::Arc;
/// use std::time::Duration;
///
/// use fast_telemetry_export::otlp::{OtlpConfig, run};
/// use tokio_util::sync::CancellationToken;
///
/// let metrics = Arc::new(MyMetrics::new());
/// let cancel = CancellationToken::new();
/// let config = OtlpConfig::new("http://otel-collector:4318")
///     .with_service_name("myapp")
///     .with_scope_name("proxy")
///     .with_attribute("service.version", "1.0")
///     .with_header("Authorization", "Bearer <token>")
///     .with_timeout(Duration::from_secs(5));
///
/// let m = metrics.clone();
/// tokio::spawn(run(config, cancel, move |out| {
///     m.export_otlp(out);
/// }));
/// ```
pub async fn run<F>(config: OtlpConfig, cancel: CancellationToken, mut collect_fn: F)
where
    F: FnMut(&mut Vec<pb::Metric>),
{
    let url = format!("{}/v1/metrics", config.endpoint.trim_end_matches('/'));

    log::info!(
        "Starting OTLP metrics exporter, endpoint={url}, interval={}s",
        config.interval.as_secs()
    );

    let attr_refs: Vec<(&str, &str)> = config
        .resource_attributes
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect();
    let resource = build_resource(&config.service_name, &attr_refs);

    let client = match reqwest::Client::builder().timeout(config.timeout).build() {
        Ok(c) => c,
        Err(e) => {
            log::error!("Failed to build HTTP client for OTLP exporter: {e}");
            return;
        }
    };

    let mut interval = interval(config.interval);
    interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
    interval.tick().await;

    let mut consecutive_failures: u32 = 0;
    let mut bufs = ExportBufs::default();

    let ctx = ExportContext {
        client: &client,
        url: &url,
        resource: &resource,
        scope_name: &config.scope_name,
        extra_headers: &config.headers,
    };

    loop {
        tokio::select! {
            _ = interval.tick() => {}
            _ = cancel.cancelled() => {
                log::info!("OTLP metrics exporter shutting down, performing final export");
                export_once(&ctx, &mut collect_fn, &mut bufs).await;
                return;
            }
        }

        if consecutive_failures > 0 {
            let backoff = backoff_with_jitter(consecutive_failures);
            log::debug!(
                "OTLP export backing off {}ms (failures={consecutive_failures})",
                backoff.as_millis()
            );
            tokio::select! {
                _ = tokio::time::sleep(backoff) => {}
                _ = cancel.cancelled() => {
                    export_once(&ctx, &mut collect_fn, &mut bufs).await;
                    return;
                }
            }
        }

        let mut metric_messages = Vec::new();
        collect_fn(&mut metric_messages);

        if metric_messages.is_empty() {
            continue;
        }

        let metric_count = metric_messages.len();
        let request = build_export_request(&resource, &config.scope_name, metric_messages);

        bufs.encode.clear();
        if let Err(e) = request.encode(&mut bufs.encode) {
            log::warn!("OTLP protobuf encode failed: {e}");
            continue;
        }
        let body_len = bufs.encode.len();

        match send_otlp(&client, &url, &bufs.encode, &mut bufs.gzip, &config.headers).await {
            Ok(resp) if resp.status().is_success() => {
                consecutive_failures = 0;
                log::debug!("Exported {metric_count} OTLP metrics ({body_len} bytes)");
            }
            Ok(resp) => {
                consecutive_failures = consecutive_failures.saturating_add(1);
                let status = resp.status();
                let body = resp.text().await.unwrap_or_default();
                log::warn!("OTLP export failed: status={status}, body={body}");
            }
            Err(e) => {
                consecutive_failures = consecutive_failures.saturating_add(1);
                log::warn!("OTLP export request failed: {e}");
            }
        }
    }
}

/// Run the OTLP metrics export loop on a monoio runtime.
///
/// This is a monoio-native alternative to [`run`] for applications that do not
/// want their metrics exporter to run on Tokio. It sends OTLP HTTP/protobuf over
/// a fresh monoio TCP connection per export attempt.
///
/// Currently this supports plaintext `http://` collector endpoints only. Keep
/// using [`run`] for `https://` endpoints until a monoio TLS transport is added.
/// The caller must run this inside a monoio runtime with timers enabled.
#[cfg(feature = "monoio")]
pub async fn run_monoio<F>(config: OtlpConfig, cancel: CancellationToken, mut collect_fn: F)
where
    F: FnMut(&mut Vec<pb::Metric>),
{
    use monoio::time::MissedTickBehavior;

    let url = format!("{}/v1/metrics", config.endpoint.trim_end_matches('/'));
    let target = match MonoioHttpTarget::parse(&url) {
        Ok(target) => target,
        Err(e) => {
            log::error!("Invalid monoio OTLP endpoint {url}: {e}");
            return;
        }
    };

    log::info!(
        "Starting monoio OTLP metrics exporter, endpoint={url}, interval={}s",
        config.interval.as_secs()
    );

    let attr_refs: Vec<(&str, &str)> = config
        .resource_attributes
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect();
    let resource = build_resource(&config.service_name, &attr_refs);

    let mut interval = monoio::time::interval(config.interval);
    interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
    interval.tick().await;

    let mut consecutive_failures: u32 = 0;
    let mut bufs = ExportBufs::default();

    let ctx = MonoioExportContext {
        target: &target,
        resource: &resource,
        scope_name: &config.scope_name,
        extra_headers: &config.headers,
        timeout: config.timeout,
    };

    loop {
        monoio::select! {
            _ = interval.tick() => {}
            _ = cancel.cancelled() => {
                log::info!("monoio OTLP metrics exporter shutting down, performing final export");
                export_once_monoio(&ctx, &mut collect_fn, &mut bufs).await;
                return;
            }
        }

        if consecutive_failures > 0 {
            let backoff = backoff_with_jitter(consecutive_failures);
            log::debug!(
                "monoio OTLP export backing off {}ms (failures={consecutive_failures})",
                backoff.as_millis()
            );
            monoio::select! {
                _ = monoio::time::sleep(backoff) => {}
                _ = cancel.cancelled() => {
                    export_once_monoio(&ctx, &mut collect_fn, &mut bufs).await;
                    return;
                }
            }
        }

        let mut metric_messages = Vec::new();
        collect_fn(&mut metric_messages);

        if metric_messages.is_empty() {
            continue;
        }

        let metric_count = metric_messages.len();
        let request = build_export_request(&resource, &config.scope_name, metric_messages);

        bufs.encode.clear();
        if let Err(e) = request.encode(&mut bufs.encode) {
            log::warn!("monoio OTLP protobuf encode failed: {e}");
            continue;
        }
        let body_len = bufs.encode.len();

        match send_otlp_monoio(
            &target,
            &bufs.encode,
            &mut bufs.gzip,
            &config.headers,
            config.timeout,
        )
        .await
        {
            Ok(resp) if resp.is_success() => {
                consecutive_failures = 0;
                log::debug!("Exported {metric_count} monoio OTLP metrics ({body_len} bytes)");
            }
            Ok(resp) => {
                consecutive_failures = consecutive_failures.saturating_add(1);
                log::warn!(
                    "monoio OTLP export failed: status={}, body={}",
                    resp.status,
                    resp.body_text_lossy()
                );
            }
            Err(e) => {
                consecutive_failures = consecutive_failures.saturating_add(1);
                log::warn!("monoio OTLP export request failed: {e}");
            }
        }
    }
}

struct ExportContext<'a> {
    client: &'a reqwest::Client,
    url: &'a str,
    resource: &'a pb::Resource,
    scope_name: &'a str,
    extra_headers: &'a [(String, String)],
}

#[derive(Default)]
struct ExportBufs {
    encode: Vec<u8>,
    gzip: Vec<u8>,
}

async fn export_once<F>(ctx: &ExportContext<'_>, collect_fn: &mut F, bufs: &mut ExportBufs)
where
    F: FnMut(&mut Vec<pb::Metric>),
{
    let mut metric_messages = Vec::new();
    collect_fn(&mut metric_messages);

    if metric_messages.is_empty() {
        return;
    }

    let request = build_export_request(ctx.resource, ctx.scope_name, metric_messages);

    bufs.encode.clear();
    if let Err(e) = request.encode(&mut bufs.encode) {
        log::warn!("Final OTLP protobuf encode failed: {e}");
        return;
    }

    match send_otlp(
        ctx.client,
        ctx.url,
        &bufs.encode,
        &mut bufs.gzip,
        ctx.extra_headers,
    )
    .await
    {
        Ok(resp) if !resp.status().is_success() => {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            log::warn!("Final OTLP export returned {status}: {body}");
        }
        Err(e) => log::warn!("Final OTLP export failed: {e}"),
        _ => {}
    }
}

#[cfg(feature = "monoio")]
struct MonoioExportContext<'a> {
    target: &'a MonoioHttpTarget,
    resource: &'a pb::Resource,
    scope_name: &'a str,
    extra_headers: &'a [(String, String)],
    timeout: Duration,
}

#[cfg(feature = "monoio")]
async fn export_once_monoio<F>(
    ctx: &MonoioExportContext<'_>,
    collect_fn: &mut F,
    bufs: &mut ExportBufs,
) where
    F: FnMut(&mut Vec<pb::Metric>),
{
    let mut metric_messages = Vec::new();
    collect_fn(&mut metric_messages);

    if metric_messages.is_empty() {
        return;
    }

    let request = build_export_request(ctx.resource, ctx.scope_name, metric_messages);

    bufs.encode.clear();
    if let Err(e) = request.encode(&mut bufs.encode) {
        log::warn!("Final monoio OTLP protobuf encode failed: {e}");
        return;
    }

    match send_otlp_monoio(
        ctx.target,
        &bufs.encode,
        &mut bufs.gzip,
        ctx.extra_headers,
        ctx.timeout,
    )
    .await
    {
        Ok(resp) if !resp.is_success() => {
            log::warn!(
                "Final monoio OTLP export returned {}: {}",
                resp.status,
                resp.body_text_lossy()
            );
        }
        Err(e) => log::warn!("Final monoio OTLP export failed: {e}"),
        _ => {}
    }
}

#[cfg(feature = "monoio")]
#[derive(Debug)]
struct MonoioHttpTarget {
    connect_addr: String,
    host_header: String,
    path: String,
}

#[cfg(feature = "monoio")]
impl MonoioHttpTarget {
    fn parse(url: &str) -> Result<Self, MonoioHttpError> {
        let rest = url
            .strip_prefix("http://")
            .ok_or_else(|| MonoioHttpError::InvalidEndpoint("only http:// is supported".into()))?;
        let (authority, path) = match rest.find('/') {
            Some(idx) => (&rest[..idx], &rest[idx..]),
            None => (rest, "/"),
        };

        if authority.is_empty() {
            return Err(MonoioHttpError::InvalidEndpoint(
                "missing host in endpoint".into(),
            ));
        }

        let connect_addr = connect_addr_for_authority(authority)?;

        Ok(Self {
            connect_addr,
            host_header: authority.to_string(),
            path: path.to_string(),
        })
    }
}

#[cfg(feature = "monoio")]
fn connect_addr_for_authority(authority: &str) -> Result<String, MonoioHttpError> {
    if let Some(rest) = authority.strip_prefix('[') {
        let end = rest.find(']').ok_or_else(|| {
            MonoioHttpError::InvalidEndpoint("IPv6 hosts must use [addr] syntax".into())
        })?;
        let suffix = &rest[end + 1..];
        return if suffix.is_empty() {
            Ok(format!("{authority}:80"))
        } else if let Some(port) = suffix.strip_prefix(':') {
            validate_port(port)?;
            Ok(authority.to_string())
        } else {
            Err(MonoioHttpError::InvalidEndpoint(
                "unexpected characters after IPv6 host".into(),
            ))
        };
    }

    match authority.rsplit_once(':') {
        Some((_host, port)) => {
            validate_port(port)?;
            Ok(authority.to_string())
        }
        None => Ok(format!("{authority}:80")),
    }
}

#[cfg(feature = "monoio")]
fn validate_port(port: &str) -> Result<(), MonoioHttpError> {
    if port.parse::<u16>().is_ok() {
        Ok(())
    } else {
        Err(MonoioHttpError::InvalidEndpoint(
            "invalid port in endpoint".into(),
        ))
    }
}

#[cfg(feature = "monoio")]
#[derive(Debug)]
struct MonoioHttpResponse {
    status: u16,
    body: Vec<u8>,
}

#[cfg(feature = "monoio")]
impl MonoioHttpResponse {
    fn is_success(&self) -> bool {
        (200..300).contains(&self.status)
    }

    fn body_text_lossy(&self) -> String {
        String::from_utf8_lossy(&self.body).into_owned()
    }
}

#[cfg(feature = "monoio")]
#[derive(Debug)]
enum MonoioHttpError {
    InvalidEndpoint(String),
    InvalidHeader(String),
    Io(std::io::Error),
    Timeout,
    InvalidResponse,
}

#[cfg(feature = "monoio")]
impl std::fmt::Display for MonoioHttpError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidEndpoint(msg) => write!(f, "{msg}"),
            Self::InvalidHeader(name) => write!(f, "invalid HTTP header {name:?}"),
            Self::Io(e) => write!(f, "{e}"),
            Self::Timeout => f.write_str("request timed out"),
            Self::InvalidResponse => f.write_str("invalid HTTP response"),
        }
    }
}

#[cfg(feature = "monoio")]
impl From<std::io::Error> for MonoioHttpError {
    fn from(value: std::io::Error) -> Self {
        Self::Io(value)
    }
}

#[cfg(feature = "monoio")]
async fn send_otlp_monoio(
    target: &MonoioHttpTarget,
    body: &[u8],
    gzip_buf: &mut Vec<u8>,
    extra_headers: &[(String, String)],
    timeout: Duration,
) -> Result<MonoioHttpResponse, MonoioHttpError> {
    let compressed = gzip_compress(body, gzip_buf);
    let request_body = if compressed {
        gzip_buf.as_slice()
    } else {
        body
    };
    let request = build_monoio_http_request(target, request_body, compressed, extra_headers)?;

    match monoio::time::timeout(timeout, send_monoio_http_request(target, request)).await {
        Ok(result) => result,
        Err(_) => Err(MonoioHttpError::Timeout),
    }
}

#[cfg(feature = "monoio")]
fn build_monoio_http_request(
    target: &MonoioHttpTarget,
    body: &[u8],
    compressed: bool,
    extra_headers: &[(String, String)],
) -> Result<Vec<u8>, MonoioHttpError> {
    use std::fmt::Write as _;

    let mut head = String::with_capacity(512 + extra_headers.len() * 64);
    write!(
        &mut head,
        "POST {} HTTP/1.1\r\nHost: {}\r\nContent-Type: application/x-protobuf\r\nContent-Length: {}\r\nConnection: close\r\n",
        target.path,
        target.host_header,
        body.len()
    )
    .expect("writing to String cannot fail");

    if compressed {
        head.push_str("Content-Encoding: gzip\r\n");
    }

    for (name, value) in extra_headers {
        validate_header(name, value)?;
        write!(&mut head, "{name}: {value}\r\n").expect("writing to String cannot fail");
    }

    head.push_str("\r\n");
    let mut request = head.into_bytes();
    request.extend_from_slice(body);
    Ok(request)
}

#[cfg(feature = "monoio")]
fn validate_header(name: &str, value: &str) -> Result<(), MonoioHttpError> {
    let invalid_name = name.is_empty()
        || name
            .bytes()
            .any(|b| b <= b' ' || b == b':' || b == b'\r' || b == b'\n');
    let invalid_value = value.bytes().any(|b| b == b'\r' || b == b'\n');

    if invalid_name || invalid_value {
        Err(MonoioHttpError::InvalidHeader(name.to_string()))
    } else {
        Ok(())
    }
}

#[cfg(feature = "monoio")]
async fn send_monoio_http_request(
    target: &MonoioHttpTarget,
    request: Vec<u8>,
) -> Result<MonoioHttpResponse, MonoioHttpError> {
    use monoio::io::{AsyncReadRent, AsyncWriteRentExt};
    use monoio::net::TcpStream;

    let mut stream = TcpStream::connect(target.connect_addr.as_str()).await?;
    let (write_result, _request) = stream.write_all(request).await;
    write_result?;

    let mut response = Vec::with_capacity(8192);
    loop {
        let buf = Vec::with_capacity(8192);
        let (read_result, buf) = stream.read(buf).await;
        let n = read_result?;
        if n == 0 {
            break;
        }
        let available = buf.len().min(n);
        response.extend_from_slice(&buf[..available]);
        if response.len() >= 64 * 1024 {
            break;
        }
    }

    parse_monoio_http_response(response)
}

#[cfg(feature = "monoio")]
fn parse_monoio_http_response(response: Vec<u8>) -> Result<MonoioHttpResponse, MonoioHttpError> {
    let header_end = response
        .windows(4)
        .position(|w| w == b"\r\n\r\n")
        .map(|idx| idx + 4)
        .ok_or(MonoioHttpError::InvalidResponse)?;

    let status_line_end = response[..header_end]
        .windows(2)
        .position(|w| w == b"\r\n")
        .ok_or(MonoioHttpError::InvalidResponse)?;
    let status_line = std::str::from_utf8(&response[..status_line_end])
        .map_err(|_| MonoioHttpError::InvalidResponse)?;
    let status = parse_status_code(status_line).ok_or(MonoioHttpError::InvalidResponse)?;
    let body = response[header_end..].to_vec();

    Ok(MonoioHttpResponse { status, body })
}

#[cfg(feature = "monoio")]
fn parse_status_code(status_line: &str) -> Option<u16> {
    let mut parts = status_line.split_whitespace();
    let version = parts.next()?;
    if !version.starts_with("HTTP/") {
        return None;
    }
    parts.next()?.parse().ok()
}

/// Compute backoff with jitter: min(MAX_BACKOFF, BASE_BACKOFF * 2^failures) +/- 25% jitter.
fn backoff_with_jitter(consecutive_failures: u32) -> Duration {
    let exp = consecutive_failures.min(10);
    let base_ms = BASE_BACKOFF.as_millis() as u64;
    let backoff_ms = base_ms
        .saturating_mul(1u64 << exp)
        .min(MAX_BACKOFF.as_millis() as u64);

    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .subsec_nanos();
    let jitter_range = (backoff_ms / 4).max(1);
    let jitter = (nanos as u64 % (jitter_range * 2 + 1)).saturating_sub(jitter_range);
    let final_ms = backoff_ms.saturating_add(jitter);

    Duration::from_millis(final_ms)
}

#[cfg(all(test, feature = "monoio"))]
mod monoio_tests {
    use super::*;

    #[test]
    fn parses_http_target_with_port() {
        let target = MonoioHttpTarget::parse("http://localhost:4318/v1/metrics").unwrap();
        assert_eq!(target.connect_addr, "localhost:4318");
        assert_eq!(target.host_header, "localhost:4318");
        assert_eq!(target.path, "/v1/metrics");
    }

    #[test]
    fn parses_http_target_without_port() {
        let target = MonoioHttpTarget::parse("http://collector/v1/metrics").unwrap();
        assert_eq!(target.connect_addr, "collector:80");
        assert_eq!(target.host_header, "collector");
        assert_eq!(target.path, "/v1/metrics");
    }

    #[test]
    fn rejects_https_target() {
        let err = MonoioHttpTarget::parse("https://collector:4318/v1/metrics").unwrap_err();
        assert!(matches!(err, MonoioHttpError::InvalidEndpoint(_)));
    }

    #[test]
    fn rejects_invalid_port() {
        let err = MonoioHttpTarget::parse("http://collector:nope/v1/metrics").unwrap_err();
        assert!(matches!(err, MonoioHttpError::InvalidEndpoint(_)));
    }

    #[test]
    fn builds_http_request() {
        let target = MonoioHttpTarget::parse("http://localhost:4318/v1/metrics").unwrap();
        let request = build_monoio_http_request(
            &target,
            b"abc",
            true,
            &[("Authorization".to_string(), "Bearer token".to_string())],
        )
        .unwrap();
        let request = String::from_utf8(request).unwrap();
        assert!(request.starts_with("POST /v1/metrics HTTP/1.1\r\n"));
        assert!(request.contains("Host: localhost:4318\r\n"));
        assert!(request.contains("Content-Length: 3\r\n"));
        assert!(request.contains("Content-Encoding: gzip\r\n"));
        assert!(request.ends_with("\r\n\r\nabc"));
    }

    #[test]
    fn parses_http_status() {
        let response = parse_monoio_http_response(
            b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\n\r\n".to_vec(),
        )
        .unwrap();
        assert_eq!(response.status, 204);
        assert!(response.body.is_empty());
    }
}