fast-telemetry-export 0.3.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
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
//! Built-in exporter targeting the metric table layout used by the
//! [OpenTelemetry Collector ClickHouse exporter].
//!
//! Writes to four tables — `otel_metrics_sum`, `otel_metrics_gauge`,
//! `otel_metrics_histogram`, `otel_metrics_exponential_histogram` — using
//! `Map(LowCardinality(String), String)` for resource and metric attributes
//! so common dashboards / queries / materialized views can use the same column
//! names.
//!
//! The schema includes Collector compatibility columns for scope, schema URL,
//! and exemplars; flat `export_otlp()` metrics populate those with defaults.
//! Summary metrics are not emitted by this exporter.
//!
//! All four tables are inserted into via a single shared client connection
//! per cycle; this is the convenient default for users who don't have a
//! custom schema and want metrics in ClickHouse with minimal configuration.
//!
//! For custom schemas, see the parent module's [`run`](super::run) primitive.
//!
//! [OpenTelemetry Collector ClickHouse exporter]: https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/clickhouseexporter

use std::time::Duration;

use fast_telemetry::clickhouse::{
    ClickHouseMetricBatch, ExpHistogramRow, GaugeRow, HistogramRow, SumRow,
};
use fast_telemetry::otlp::pb;
use indexmap::IndexMap;
use klickhouse::{Client, DateTime64, Tz};
use tokio::time::{MissedTickBehavior, interval};
use tokio_util::sync::CancellationToken;

use super::{
    ClickHouseConfig, backoff_with_jitter, connect, connect_with_database, qualified_table,
    quote_ident,
};

/// Configuration for the built-in OTel-standard schema exporter.
#[derive(Clone)]
pub struct OtelStandardConfig {
    /// Connection settings (endpoint, credentials, database, interval).
    pub clickhouse: ClickHouseConfig,
    /// `service.name` resource attribute, written into every row.
    pub service_name: String,
    /// Instrumentation scope name (default `fast-telemetry`).
    pub scope_name: String,
    /// Additional resource attributes attached to every row.
    pub resource_attributes: Vec<(String, String)>,
    /// Run `CREATE TABLE IF NOT EXISTS` for each metric table on startup
    /// (default true). Disable if schema is managed externally.
    pub auto_create_tables: bool,
    pub sum_table: String,
    pub gauge_table: String,
    pub histogram_table: String,
    pub exp_histogram_table: String,
}

impl Default for OtelStandardConfig {
    fn default() -> Self {
        Self {
            clickhouse: ClickHouseConfig::default(),
            service_name: "unknown_service".to_string(),
            scope_name: "fast-telemetry".to_string(),
            resource_attributes: Vec::new(),
            auto_create_tables: true,
            sum_table: "otel_metrics_sum".to_string(),
            gauge_table: "otel_metrics_gauge".to_string(),
            histogram_table: "otel_metrics_histogram".to_string(),
            exp_histogram_table: "otel_metrics_exponential_histogram".to_string(),
        }
    }
}

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

    pub fn with_credentials(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.clickhouse = self.clickhouse.with_credentials(username, password);
        self
    }

    pub fn with_database(mut self, database: impl Into<String>) -> Self {
        self.clickhouse = self.clickhouse.with_database(database);
        self
    }

    pub fn with_interval(mut self, interval: Duration) -> Self {
        self.clickhouse = self.clickhouse.with_interval(interval);
        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_auto_create_tables(mut self, enabled: bool) -> Self {
        self.auto_create_tables = enabled;
        self
    }
}

// ----------------------------------------------------------------------------
// Schema
// ----------------------------------------------------------------------------

fn sum_table_ddl(db: &str, table: &str) -> String {
    let table_ref = qualified_table(db, table);
    format!(
        "CREATE TABLE IF NOT EXISTS {table_ref} (
            ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            ResourceSchemaUrl String CODEC(ZSTD(1)),
            ServiceName LowCardinality(String),
            ScopeName String CODEC(ZSTD(1)),
            ScopeVersion String CODEC(ZSTD(1)),
            ScopeAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            ScopeDroppedAttrCount UInt32 CODEC(ZSTD(1)),
            ScopeSchemaUrl String CODEC(ZSTD(1)),
            MetricName String,
            MetricDescription String,
            MetricUnit String CODEC(ZSTD(1)),
            Attributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            StartTimeUnix DateTime64(9) CODEC(Delta, ZSTD(1)),
            TimeUnix DateTime64(9),
            Value Float64,
            Flags UInt32 CODEC(ZSTD(1)),
            Exemplars Nested (
                FilteredAttributes Map(LowCardinality(String), String),
                TimeUnix DateTime64(9),
                Value Float64,
                SpanId String,
                TraceId String
            ) CODEC(ZSTD(1)),
            AggregationTemporality Int32,
            IsMonotonic Bool
        ) ENGINE = MergeTree()
        PARTITION BY toDate(TimeUnix)
        ORDER BY (ServiceName, MetricName, TimeUnix)
        TTL toDateTime(TimeUnix) + toIntervalDay(30)
        SETTINGS index_granularity = 8192"
    )
}

fn gauge_table_ddl(db: &str, table: &str) -> String {
    let table_ref = qualified_table(db, table);
    format!(
        "CREATE TABLE IF NOT EXISTS {table_ref} (
            ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            ResourceSchemaUrl String CODEC(ZSTD(1)),
            ServiceName LowCardinality(String),
            ScopeName String CODEC(ZSTD(1)),
            ScopeVersion String CODEC(ZSTD(1)),
            ScopeAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            ScopeDroppedAttrCount UInt32 CODEC(ZSTD(1)),
            ScopeSchemaUrl String CODEC(ZSTD(1)),
            MetricName String,
            MetricDescription String,
            MetricUnit String CODEC(ZSTD(1)),
            Attributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            StartTimeUnix DateTime64(9) CODEC(Delta, ZSTD(1)),
            TimeUnix DateTime64(9),
            Value Float64,
            Flags UInt32 CODEC(ZSTD(1)),
            Exemplars Nested (
                FilteredAttributes Map(LowCardinality(String), String),
                TimeUnix DateTime64(9),
                Value Float64,
                SpanId String,
                TraceId String
            ) CODEC(ZSTD(1))
        ) ENGINE = MergeTree()
        PARTITION BY toDate(TimeUnix)
        ORDER BY (ServiceName, MetricName, TimeUnix)
        TTL toDateTime(TimeUnix) + toIntervalDay(30)
        SETTINGS index_granularity = 8192"
    )
}

fn histogram_table_ddl(db: &str, table: &str) -> String {
    let table_ref = qualified_table(db, table);
    format!(
        "CREATE TABLE IF NOT EXISTS {table_ref} (
            ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            ResourceSchemaUrl String CODEC(ZSTD(1)),
            ServiceName LowCardinality(String),
            ScopeName String CODEC(ZSTD(1)),
            ScopeVersion String CODEC(ZSTD(1)),
            ScopeAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            ScopeDroppedAttrCount UInt32 CODEC(ZSTD(1)),
            ScopeSchemaUrl String CODEC(ZSTD(1)),
            MetricName String,
            MetricDescription String,
            MetricUnit String CODEC(ZSTD(1)),
            Attributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            StartTimeUnix DateTime64(9) CODEC(Delta, ZSTD(1)),
            TimeUnix DateTime64(9),
            Count UInt64,
            Sum Float64,
            BucketCounts Array(UInt64),
            ExplicitBounds Array(Float64),
            Exemplars Nested (
                FilteredAttributes Map(LowCardinality(String), String),
                TimeUnix DateTime64(9),
                Value Float64,
                SpanId String,
                TraceId String
            ) CODEC(ZSTD(1)),
            Flags UInt32 CODEC(ZSTD(1)),
            Min Float64 CODEC(ZSTD(1)),
            Max Float64 CODEC(ZSTD(1)),
            AggregationTemporality Int32
        ) ENGINE = MergeTree()
        PARTITION BY toDate(TimeUnix)
        ORDER BY (ServiceName, MetricName, TimeUnix)
        TTL toDateTime(TimeUnix) + toIntervalDay(30)
        SETTINGS index_granularity = 8192"
    )
}

fn exp_histogram_table_ddl(db: &str, table: &str) -> String {
    let table_ref = qualified_table(db, table);
    format!(
        "CREATE TABLE IF NOT EXISTS {table_ref} (
            ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            ResourceSchemaUrl String CODEC(ZSTD(1)),
            ServiceName LowCardinality(String),
            ScopeName String CODEC(ZSTD(1)),
            ScopeVersion String CODEC(ZSTD(1)),
            ScopeAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            ScopeDroppedAttrCount UInt32 CODEC(ZSTD(1)),
            ScopeSchemaUrl String CODEC(ZSTD(1)),
            MetricName String,
            MetricDescription String,
            MetricUnit String CODEC(ZSTD(1)),
            Attributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
            StartTimeUnix DateTime64(9) CODEC(Delta, ZSTD(1)),
            TimeUnix DateTime64(9),
            Count UInt64,
            Sum Float64,
            Scale Int32,
            ZeroCount UInt64,
            PositiveOffset Int32,
            PositiveBucketCounts Array(UInt64),
            NegativeOffset Int32,
            NegativeBucketCounts Array(UInt64),
            Exemplars Nested (
                FilteredAttributes Map(LowCardinality(String), String),
                TimeUnix DateTime64(9),
                Value Float64,
                SpanId String,
                TraceId String
            ) CODEC(ZSTD(1)),
            Flags UInt32 CODEC(ZSTD(1)),
            Min Float64 CODEC(ZSTD(1)),
            Max Float64 CODEC(ZSTD(1)),
            AggregationTemporality Int32
        ) ENGINE = MergeTree()
        PARTITION BY toDate(TimeUnix)
        ORDER BY (ServiceName, MetricName, TimeUnix)
        TTL toDateTime(TimeUnix) + toIntervalDay(30)
        SETTINGS index_granularity = 8192"
    )
}

async fn ensure_schema(client: &Client, config: &OtelStandardConfig) -> klickhouse::Result<()> {
    client
        .execute(format!(
            "CREATE DATABASE IF NOT EXISTS {}",
            quote_ident(&config.clickhouse.database)
        ))
        .await?;
    client
        .execute(sum_table_ddl(
            &config.clickhouse.database,
            &config.sum_table,
        ))
        .await?;
    client
        .execute(gauge_table_ddl(
            &config.clickhouse.database,
            &config.gauge_table,
        ))
        .await?;
    client
        .execute(histogram_table_ddl(
            &config.clickhouse.database,
            &config.histogram_table,
        ))
        .await?;
    client
        .execute(exp_histogram_table_ddl(
            &config.clickhouse.database,
            &config.exp_histogram_table,
        ))
        .await?;
    Ok(())
}

async fn connect_and_prepare(config: &OtelStandardConfig) -> klickhouse::Result<Client> {
    match connect(&config.clickhouse).await {
        Ok(client) => {
            if config.auto_create_tables {
                ensure_schema(&client, config).await?;
            }
            Ok(client)
        }
        Err(_) if config.auto_create_tables && config.clickhouse.database != "default" => {
            let bootstrap = connect_with_database(&config.clickhouse, "default").await?;
            ensure_schema(&bootstrap, config).await?;
            connect(&config.clickhouse).await
        }
        Err(e) => Err(e),
    }
}

// ----------------------------------------------------------------------------
// pb::Metric → rows translation
// ----------------------------------------------------------------------------

fn attrs_to_map(attrs: &[pb::KeyValue]) -> IndexMap<String, String> {
    let mut map = IndexMap::with_capacity(attrs.len());
    for kv in attrs {
        if let Some(any) = &kv.value
            && let Some(value) = &any.value
        {
            let s = match value {
                pb::any_value::Value::StringValue(s) => s.clone(),
                pb::any_value::Value::IntValue(i) => i.to_string(),
                pb::any_value::Value::DoubleValue(f) => f.to_string(),
                pb::any_value::Value::BoolValue(b) => b.to_string(),
                _ => continue,
            };
            map.insert(kv.key.clone(), s);
        }
    }
    map
}

fn number_value(dp: &pb::NumberDataPoint) -> f64 {
    match dp.value {
        Some(pb::number_data_point::Value::AsInt(i)) => i as f64,
        Some(pb::number_data_point::Value::AsDouble(f)) => f,
        None => 0.0,
    }
}

fn time_unix(time_unix_nano: u64) -> DateTime64<9> {
    DateTime64::<9>(Tz::UTC, time_unix_nano)
}

#[derive(Default)]
struct Batches {
    sums: Vec<SumRow>,
    gauges: Vec<GaugeRow>,
    histograms: Vec<HistogramRow>,
    exp_histograms: Vec<ExpHistogramRow>,
}

impl Batches {
    fn clear(&mut self) {
        self.sums.clear();
        self.gauges.clear();
        self.histograms.clear();
        self.exp_histograms.clear();
    }

    fn is_empty(&self) -> bool {
        self.sums.is_empty()
            && self.gauges.is_empty()
            && self.histograms.is_empty()
            && self.exp_histograms.is_empty()
    }

    fn total_rows(&self) -> usize {
        self.sums.len() + self.gauges.len() + self.histograms.len() + self.exp_histograms.len()
    }
}

fn translate_metrics(
    metrics: &[pb::Metric],
    resource_attrs: &IndexMap<String, String>,
    service_name: &str,
    scope_name: &str,
    out: &mut Batches,
) {
    for metric in metrics {
        let Some(data) = &metric.data else { continue };
        match data {
            pb::metric::Data::Sum(sum) => {
                for dp in &sum.data_points {
                    out.sums.push(SumRow {
                        ResourceAttributes: resource_attrs.clone(),
                        ResourceSchemaUrl: String::new(),
                        ServiceName: service_name.to_string(),
                        ScopeName: scope_name.to_string(),
                        ScopeVersion: String::new(),
                        ScopeAttributes: IndexMap::new(),
                        ScopeDroppedAttrCount: 0,
                        ScopeSchemaUrl: String::new(),
                        MetricName: metric.name.clone(),
                        MetricDescription: metric.description.clone(),
                        MetricUnit: metric.unit.clone(),
                        Attributes: attrs_to_map(&dp.attributes),
                        StartTimeUnix: time_unix(dp.start_time_unix_nano),
                        TimeUnix: time_unix(dp.time_unix_nano),
                        Value: number_value(dp),
                        Flags: dp.flags,
                        AggregationTemporality: sum.aggregation_temporality,
                        IsMonotonic: sum.is_monotonic,
                    });
                }
            }
            pb::metric::Data::Gauge(g) => {
                for dp in &g.data_points {
                    out.gauges.push(GaugeRow {
                        ResourceAttributes: resource_attrs.clone(),
                        ResourceSchemaUrl: String::new(),
                        ServiceName: service_name.to_string(),
                        ScopeName: scope_name.to_string(),
                        ScopeVersion: String::new(),
                        ScopeAttributes: IndexMap::new(),
                        ScopeDroppedAttrCount: 0,
                        ScopeSchemaUrl: String::new(),
                        MetricName: metric.name.clone(),
                        MetricDescription: metric.description.clone(),
                        MetricUnit: metric.unit.clone(),
                        Attributes: attrs_to_map(&dp.attributes),
                        StartTimeUnix: time_unix(dp.start_time_unix_nano),
                        TimeUnix: time_unix(dp.time_unix_nano),
                        Value: number_value(dp),
                        Flags: dp.flags,
                    });
                }
            }
            pb::metric::Data::Histogram(h) => {
                for dp in &h.data_points {
                    out.histograms.push(HistogramRow {
                        ResourceAttributes: resource_attrs.clone(),
                        ResourceSchemaUrl: String::new(),
                        ServiceName: service_name.to_string(),
                        ScopeName: scope_name.to_string(),
                        ScopeVersion: String::new(),
                        ScopeAttributes: IndexMap::new(),
                        ScopeDroppedAttrCount: 0,
                        ScopeSchemaUrl: String::new(),
                        MetricName: metric.name.clone(),
                        MetricDescription: metric.description.clone(),
                        MetricUnit: metric.unit.clone(),
                        Attributes: attrs_to_map(&dp.attributes),
                        StartTimeUnix: time_unix(dp.start_time_unix_nano),
                        TimeUnix: time_unix(dp.time_unix_nano),
                        Count: dp.count,
                        Sum: dp.sum.unwrap_or(0.0),
                        BucketCounts: dp.bucket_counts.clone(),
                        ExplicitBounds: dp.explicit_bounds.clone(),
                        Flags: dp.flags,
                        Min: dp.min.unwrap_or(0.0),
                        Max: dp.max.unwrap_or(0.0),
                        AggregationTemporality: h.aggregation_temporality,
                    });
                }
            }
            pb::metric::Data::ExponentialHistogram(eh) => {
                for dp in &eh.data_points {
                    let (positive_offset, positive_counts) = match &dp.positive {
                        Some(b) => (b.offset, b.bucket_counts.clone()),
                        None => (0, Vec::new()),
                    };
                    let (negative_offset, negative_counts) = match &dp.negative {
                        Some(b) => (b.offset, b.bucket_counts.clone()),
                        None => (0, Vec::new()),
                    };
                    out.exp_histograms.push(ExpHistogramRow {
                        ResourceAttributes: resource_attrs.clone(),
                        ResourceSchemaUrl: String::new(),
                        ServiceName: service_name.to_string(),
                        ScopeName: scope_name.to_string(),
                        ScopeVersion: String::new(),
                        ScopeAttributes: IndexMap::new(),
                        ScopeDroppedAttrCount: 0,
                        ScopeSchemaUrl: String::new(),
                        MetricName: metric.name.clone(),
                        MetricDescription: metric.description.clone(),
                        MetricUnit: metric.unit.clone(),
                        Attributes: attrs_to_map(&dp.attributes),
                        StartTimeUnix: time_unix(dp.start_time_unix_nano),
                        TimeUnix: time_unix(dp.time_unix_nano),
                        Count: dp.count,
                        Sum: dp.sum.unwrap_or(0.0),
                        Scale: dp.scale,
                        ZeroCount: dp.zero_count,
                        PositiveOffset: positive_offset,
                        PositiveBucketCounts: positive_counts,
                        NegativeOffset: negative_offset,
                        NegativeBucketCounts: negative_counts,
                        Flags: dp.flags,
                        Min: dp.min.unwrap_or(0.0),
                        Max: dp.max.unwrap_or(0.0),
                        AggregationTemporality: eh.aggregation_temporality,
                    });
                }
            }
            _ => {}
        }
    }
}

/// Benchmark-only hook for measuring the production OTLP-metric to ClickHouse-row
/// translation path without exposing the internal row types as public API.
#[doc(hidden)]
pub fn benchmark_translate_row_count(metrics: &[pb::Metric]) -> usize {
    let mut resource_attrs = IndexMap::new();
    resource_attrs.insert("service.name".to_string(), "bench".to_string());

    let mut batches = Batches::default();
    translate_metrics(
        metrics,
        &resource_attrs,
        "bench",
        "fast-telemetry",
        &mut batches,
    );
    batches.total_rows()
}

// ----------------------------------------------------------------------------
// Insert
// ----------------------------------------------------------------------------

const SUM_COLUMNS: &str = "ResourceAttributes, ResourceSchemaUrl, ServiceName, ScopeName, ScopeVersion, ScopeAttributes, ScopeDroppedAttrCount, ScopeSchemaUrl, MetricName, MetricDescription, MetricUnit, Attributes, StartTimeUnix, TimeUnix, Value, Flags, AggregationTemporality, IsMonotonic";
const GAUGE_COLUMNS: &str = "ResourceAttributes, ResourceSchemaUrl, ServiceName, ScopeName, ScopeVersion, ScopeAttributes, ScopeDroppedAttrCount, ScopeSchemaUrl, MetricName, MetricDescription, MetricUnit, Attributes, StartTimeUnix, TimeUnix, Value, Flags";
const HISTOGRAM_COLUMNS: &str = "ResourceAttributes, ResourceSchemaUrl, ServiceName, ScopeName, ScopeVersion, ScopeAttributes, ScopeDroppedAttrCount, ScopeSchemaUrl, MetricName, MetricDescription, MetricUnit, Attributes, StartTimeUnix, TimeUnix, Count, Sum, BucketCounts, ExplicitBounds, Flags, Min, Max, AggregationTemporality";
const EXP_HISTOGRAM_COLUMNS: &str = "ResourceAttributes, ResourceSchemaUrl, ServiceName, ScopeName, ScopeVersion, ScopeAttributes, ScopeDroppedAttrCount, ScopeSchemaUrl, MetricName, MetricDescription, MetricUnit, Attributes, StartTimeUnix, TimeUnix, Count, Sum, Scale, ZeroCount, PositiveOffset, PositiveBucketCounts, NegativeOffset, NegativeBucketCounts, Flags, Min, Max, AggregationTemporality";

fn now_nanos() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos() as u64
}

async fn insert_batches(
    client: &Client,
    config: &OtelStandardConfig,
    batches: &mut Batches,
) -> klickhouse::Result<usize> {
    let mut total = 0;
    let db = &config.clickhouse.database;

    if !batches.sums.is_empty() {
        let rows = std::mem::take(&mut batches.sums);
        total += rows.len();
        let q = format!(
            "INSERT INTO {} ({SUM_COLUMNS}) FORMAT native",
            qualified_table(db, &config.sum_table)
        );
        client.insert_native_block(&q, rows).await?;
    }

    if !batches.gauges.is_empty() {
        let rows = std::mem::take(&mut batches.gauges);
        total += rows.len();
        let q = format!(
            "INSERT INTO {} ({GAUGE_COLUMNS}) FORMAT native",
            qualified_table(db, &config.gauge_table)
        );
        client.insert_native_block(&q, rows).await?;
    }

    if !batches.histograms.is_empty() {
        let rows = std::mem::take(&mut batches.histograms);
        total += rows.len();
        let q = format!(
            "INSERT INTO {} ({HISTOGRAM_COLUMNS}) FORMAT native",
            qualified_table(db, &config.histogram_table)
        );
        client.insert_native_block(&q, rows).await?;
    }

    if !batches.exp_histograms.is_empty() {
        let rows = std::mem::take(&mut batches.exp_histograms);
        total += rows.len();
        let q = format!(
            "INSERT INTO {} ({EXP_HISTOGRAM_COLUMNS}) FORMAT native",
            qualified_table(db, &config.exp_histogram_table)
        );
        client.insert_native_block(&q, rows).await?;
    }

    Ok(total)
}

async fn insert_metric_batch(
    client: &Client,
    config: &OtelStandardConfig,
    batch: &mut ClickHouseMetricBatch,
) -> klickhouse::Result<usize> {
    let mut total = 0;
    let db = &config.clickhouse.database;

    if !batch.sums.is_empty() {
        let rows = std::mem::take(&mut batch.sums);
        total += rows.len();
        let q = format!(
            "INSERT INTO {} ({SUM_COLUMNS}) FORMAT native",
            qualified_table(db, &config.sum_table)
        );
        client.insert_native_block(&q, rows).await?;
    }

    if !batch.gauges.is_empty() {
        let rows = std::mem::take(&mut batch.gauges);
        total += rows.len();
        let q = format!(
            "INSERT INTO {} ({GAUGE_COLUMNS}) FORMAT native",
            qualified_table(db, &config.gauge_table)
        );
        client.insert_native_block(&q, rows).await?;
    }

    if !batch.histograms.is_empty() {
        let rows = std::mem::take(&mut batch.histograms);
        total += rows.len();
        let q = format!(
            "INSERT INTO {} ({HISTOGRAM_COLUMNS}) FORMAT native",
            qualified_table(db, &config.histogram_table)
        );
        client.insert_native_block(&q, rows).await?;
    }

    if !batch.exp_histograms.is_empty() {
        let rows = std::mem::take(&mut batch.exp_histograms);
        total += rows.len();
        let q = format!(
            "INSERT INTO {} ({EXP_HISTOGRAM_COLUMNS}) FORMAT native",
            qualified_table(db, &config.exp_histogram_table)
        );
        client.insert_native_block(&q, rows).await?;
    }

    Ok(total)
}

// ----------------------------------------------------------------------------
// Run loop
// ----------------------------------------------------------------------------

/// Run the OTel-standard schema export loop.
///
/// `collect_fn` is called each cycle with a `&mut Vec<pb::Metric>` — typically
/// the same `export_otlp(out)` method you'd pass to the OTLP exporter. The
/// exporter handles connection setup, optional schema bootstrap, batched
/// native-protocol inserts to all four tables on a shared connection, and
/// exponential backoff on failures.
///
/// On cancellation, a final export is performed to flush pending metrics.
pub async fn run<F>(config: OtelStandardConfig, cancel: CancellationToken, mut collect_fn: F)
where
    F: FnMut(&mut Vec<pb::Metric>),
{
    log::info!(
        "Starting ClickHouse OTel-standard exporter, endpoint={}, database={}, interval={}s",
        config.clickhouse.endpoint,
        config.clickhouse.database,
        config.clickhouse.interval.as_secs()
    );

    let mut resource_attrs = IndexMap::with_capacity(config.resource_attributes.len() + 1);
    resource_attrs.insert("service.name".to_string(), config.service_name.clone());
    for (k, v) in &config.resource_attributes {
        resource_attrs.insert(k.clone(), v.clone());
    }

    let mut client = match connect_and_prepare(&config).await {
        Ok(c) => c,
        Err(e) => {
            log::error!(
                "Failed to connect to ClickHouse at {} or create ClickHouse metric tables: {e}",
                config.clickhouse.endpoint
            );
            return;
        }
    };

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

    let mut consecutive_failures: u32 = 0;
    let mut metrics_buf: Vec<pb::Metric> = Vec::new();
    let mut batches = Batches::default();

    loop {
        tokio::select! {
            _ = interval_timer.tick() => {}
            _ = cancel.cancelled() => {
                log::info!("ClickHouse OTel-standard exporter shutting down, performing final export");
                let _ = export_once(
                    &client,
                    &config,
                    &resource_attrs,
                    &mut collect_fn,
                    &mut metrics_buf,
                    &mut batches,
                ).await;
                return;
            }
        }

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

        if client.is_closed() {
            match connect(&config.clickhouse).await {
                Ok(c) => {
                    log::info!("Reconnected to ClickHouse");
                    client = c;
                }
                Err(e) => {
                    consecutive_failures = consecutive_failures.saturating_add(1);
                    log::warn!("ClickHouse reconnect failed: {e}");
                    continue;
                }
            }
        }

        metrics_buf.clear();
        collect_fn(&mut metrics_buf);
        if metrics_buf.is_empty() {
            continue;
        }

        batches.clear();
        translate_metrics(
            &metrics_buf,
            &resource_attrs,
            &config.service_name,
            &config.scope_name,
            &mut batches,
        );

        if batches.is_empty() {
            continue;
        }

        let row_count = batches.total_rows();
        match insert_batches(&client, &config, &mut batches).await {
            Ok(_) => {
                consecutive_failures = 0;
                log::debug!("Exported {row_count} rows to ClickHouse");
            }
            Err(e) => {
                consecutive_failures = consecutive_failures.saturating_add(1);
                log::warn!("ClickHouse insert failed: {e}");
            }
        }
    }
}

/// Run the first-party ClickHouse row export loop.
///
/// `collect_fn` is called each cycle with a reusable
/// [`ClickHouseMetricBatch`] and shared timestamp. Call derive-generated
/// `export_clickhouse(batch, time_unix_nano)` methods or individual
/// [`fast_telemetry::ClickHouseExport`] impls inside the closure.
///
/// This skips the `pb::Metric` intermediate used by [`run`] and writes the
/// same OTel-standard ClickHouse tables.
pub async fn run_first_party<F>(
    config: OtelStandardConfig,
    cancel: CancellationToken,
    mut collect_fn: F,
) where
    F: FnMut(&mut ClickHouseMetricBatch, u64),
{
    log::info!(
        "Starting first-party ClickHouse OTel-standard exporter, endpoint={}, database={}, interval={}s",
        config.clickhouse.endpoint,
        config.clickhouse.database,
        config.clickhouse.interval.as_secs()
    );

    let mut client = match connect_and_prepare(&config).await {
        Ok(c) => c,
        Err(e) => {
            log::error!(
                "Failed to connect to ClickHouse at {} or create ClickHouse metric tables: {e}",
                config.clickhouse.endpoint
            );
            return;
        }
    };

    let mut batch =
        ClickHouseMetricBatch::with_scope(config.service_name.clone(), config.scope_name.clone());
    for (key, value) in &config.resource_attributes {
        batch = batch.with_resource_attribute(key.clone(), value.clone());
    }

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

    let mut consecutive_failures: u32 = 0;

    loop {
        tokio::select! {
            _ = interval_timer.tick() => {}
            _ = cancel.cancelled() => {
                log::info!("First-party ClickHouse exporter shutting down, performing final export");
                let _ = export_first_party_once(
                    &client,
                    &config,
                    &mut collect_fn,
                    &mut batch,
                ).await;
                return;
            }
        }

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

        if client.is_closed() {
            match connect(&config.clickhouse).await {
                Ok(c) => {
                    log::info!("Reconnected to ClickHouse");
                    client = c;
                }
                Err(e) => {
                    consecutive_failures = consecutive_failures.saturating_add(1);
                    log::warn!("ClickHouse reconnect failed: {e}");
                    continue;
                }
            }
        }

        batch.clear();
        collect_fn(&mut batch, now_nanos());
        if batch.total_rows() == 0 {
            continue;
        }

        let row_count = batch.total_rows();
        match insert_metric_batch(&client, &config, &mut batch).await {
            Ok(_) => {
                consecutive_failures = 0;
                log::debug!("Exported {row_count} rows to ClickHouse");
            }
            Err(e) => {
                consecutive_failures = consecutive_failures.saturating_add(1);
                log::warn!("ClickHouse insert failed: {e}");
            }
        }
    }
}

async fn export_once<F>(
    client: &Client,
    config: &OtelStandardConfig,
    resource_attrs: &IndexMap<String, String>,
    collect_fn: &mut F,
    metrics_buf: &mut Vec<pb::Metric>,
    batches: &mut Batches,
) -> klickhouse::Result<()>
where
    F: FnMut(&mut Vec<pb::Metric>),
{
    metrics_buf.clear();
    collect_fn(metrics_buf);
    if metrics_buf.is_empty() {
        return Ok(());
    }

    batches.clear();
    translate_metrics(
        metrics_buf,
        resource_attrs,
        &config.service_name,
        &config.scope_name,
        batches,
    );

    if batches.is_empty() {
        return Ok(());
    }

    if let Err(e) = insert_batches(client, config, batches).await {
        log::warn!("Final ClickHouse insert failed: {e}");
        return Err(e);
    }
    Ok(())
}

async fn export_first_party_once<F>(
    client: &Client,
    config: &OtelStandardConfig,
    collect_fn: &mut F,
    batch: &mut ClickHouseMetricBatch,
) -> klickhouse::Result<()>
where
    F: FnMut(&mut ClickHouseMetricBatch, u64),
{
    batch.clear();
    collect_fn(batch, now_nanos());
    if batch.total_rows() == 0 {
        return Ok(());
    }

    if let Err(e) = insert_metric_batch(client, config, batch).await {
        log::warn!("Final ClickHouse insert failed: {e}");
        return Err(e);
    }
    Ok(())
}

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

    fn ts() -> u64 {
        1_700_000_000_000_000_000
    }

    fn make_kv(k: &str, v: &str) -> pb::KeyValue {
        pb::KeyValue {
            key: k.to_string(),
            value: Some(pb::AnyValue {
                value: Some(pb::any_value::Value::StringValue(v.to_string())),
            }),
        }
    }

    fn resource_for_test() -> IndexMap<String, String> {
        let mut m = IndexMap::new();
        m.insert("service.name".to_string(), "test".to_string());
        m
    }

    #[test]
    fn translates_sum_to_sum_row() {
        let metric = pb::Metric {
            name: "requests_total".to_string(),
            description: "request count".to_string(),
            data: Some(pb::metric::Data::Sum(pb::Sum {
                data_points: vec![pb::NumberDataPoint {
                    attributes: vec![make_kv("route", "/api")],
                    time_unix_nano: ts(),
                    value: Some(pb::number_data_point::Value::AsInt(42)),
                    ..Default::default()
                }],
                aggregation_temporality: pb::AggregationTemporality::Cumulative as i32,
                is_monotonic: true,
            })),
            ..Default::default()
        };

        let mut batches = Batches::default();
        translate_metrics(
            &[metric],
            &resource_for_test(),
            "test",
            "fast-telemetry",
            &mut batches,
        );

        assert_eq!(batches.sums.len(), 1);
        let row = &batches.sums[0];
        assert_eq!(row.MetricName, "requests_total");
        assert_eq!(row.Value, 42.0);
        assert!(row.IsMonotonic);
        assert_eq!(row.Attributes.get("route"), Some(&"/api".to_string()));
    }

    #[test]
    fn translates_gauge() {
        let metric = pb::Metric {
            name: "cpu".to_string(),
            description: String::new(),
            data: Some(pb::metric::Data::Gauge(pb::OtlpGauge {
                data_points: vec![pb::NumberDataPoint {
                    time_unix_nano: ts(),
                    value: Some(pb::number_data_point::Value::AsDouble(0.75)),
                    ..Default::default()
                }],
            })),
            ..Default::default()
        };

        let mut batches = Batches::default();
        translate_metrics(
            &[metric],
            &resource_for_test(),
            "test",
            "fast-telemetry",
            &mut batches,
        );

        assert_eq!(batches.gauges.len(), 1);
        assert!((batches.gauges[0].Value - 0.75).abs() < 1e-12);
    }

    #[test]
    fn translates_histogram() {
        let metric = pb::Metric {
            name: "lat".to_string(),
            description: String::new(),
            data: Some(pb::metric::Data::Histogram(pb::OtlpHistogram {
                data_points: vec![pb::HistogramDataPoint {
                    time_unix_nano: ts(),
                    count: 5,
                    sum: Some(123.0),
                    bucket_counts: vec![1, 2, 2],
                    explicit_bounds: vec![10.0, 100.0],
                    ..Default::default()
                }],
                aggregation_temporality: pb::AggregationTemporality::Cumulative as i32,
            })),
            ..Default::default()
        };

        let mut batches = Batches::default();
        translate_metrics(
            &[metric],
            &resource_for_test(),
            "test",
            "fast-telemetry",
            &mut batches,
        );

        assert_eq!(batches.histograms.len(), 1);
        let row = &batches.histograms[0];
        assert_eq!(row.Count, 5);
        assert_eq!(row.Sum, 123.0);
        assert_eq!(row.BucketCounts, vec![1, 2, 2]);
        assert_eq!(row.ExplicitBounds, vec![10.0, 100.0]);
    }

    #[test]
    fn translates_exponential_histogram() {
        let metric = pb::Metric {
            name: "dist".to_string(),
            description: String::new(),
            data: Some(pb::metric::Data::ExponentialHistogram(
                pb::OtlpExpHistogram {
                    data_points: vec![pb::ExponentialHistogramDataPoint {
                        time_unix_nano: ts(),
                        count: 3,
                        sum: Some(600.0),
                        scale: 0,
                        zero_count: 0,
                        positive: Some(pb::exponential_histogram_data_point::Buckets {
                            offset: 6,
                            bucket_counts: vec![1, 1, 1],
                        }),
                        negative: Some(pb::exponential_histogram_data_point::Buckets {
                            offset: -3,
                            bucket_counts: vec![2, 4],
                        }),
                        min: Some(-8.0),
                        max: Some(16.0),
                        ..Default::default()
                    }],
                    aggregation_temporality: pb::AggregationTemporality::Cumulative as i32,
                },
            )),
            ..Default::default()
        };

        let mut batches = Batches::default();
        translate_metrics(
            &[metric],
            &resource_for_test(),
            "test",
            "fast-telemetry",
            &mut batches,
        );

        assert_eq!(batches.exp_histograms.len(), 1);
        let row = &batches.exp_histograms[0];
        assert_eq!(row.Count, 3);
        assert_eq!(row.PositiveOffset, 6);
        assert_eq!(row.PositiveBucketCounts, vec![1, 1, 1]);
        assert_eq!(row.NegativeOffset, -3);
        assert_eq!(row.NegativeBucketCounts, vec![2, 4]);
        assert_eq!(row.Min, -8.0);
        assert_eq!(row.Max, 16.0);
    }

    #[test]
    fn empty_metrics_yield_empty_batches() {
        let mut batches = Batches::default();
        translate_metrics(&[], &resource_for_test(), "test", "scope", &mut batches);
        assert!(batches.is_empty());
    }
}