re_perf_telemetry 0.36.0

In and out of process performance profiling utilities for Rerun & Redap
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
//! Prometheus-specific metric conversion and encoding utilities

#![expect(clippy::cast_possible_wrap)] // u64 -> i64

use std::collections::HashMap;
use std::fmt::Write as _;
use std::sync::Arc;

use opentelemetry::KeyValue;
use opentelemetry_sdk::metrics::data::ResourceMetrics;
use parking_lot::{Mutex, RwLock};
use prometheus_client::encoding::{EncodeLabelSet, EncodeMetric, MetricEncoder, NoLabelSet};
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::{Family, MetricConstructor};
use prometheus_client::metrics::gauge::Gauge;
use prometheus_client::metrics::{MetricType, TypedMetric};
use prometheus_client::registry::Registry;

/// Dynamic labels for metrics that support arbitrary key-value pairs
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct DynamicLabels(Vec<(String, String)>);

impl EncodeLabelSet for DynamicLabels {
    fn encode(
        &self,
        encoder: &mut prometheus_client::encoding::LabelSetEncoder<'_>,
    ) -> Result<(), std::fmt::Error> {
        let Self(labels) = self;
        for (key, value) in labels {
            let mut label_encoder = encoder.encode_label();
            let mut key_encoder = label_encoder.encode_label_key()?;
            key_encoder.write_str(key)?;
            let mut value_encoder = key_encoder.encode_label_value()?;
            value_encoder.write_str(value)?;
            value_encoder.finish()?;
        }
        Ok(())
    }
}

/// Container for different metric types with dynamic labels
pub struct MetricContainer {
    pub counters: HashMap<String, Family<DynamicLabels, Counter>>,
    pub gauges: HashMap<String, Family<DynamicLabels, Gauge<i64>>>,
    pub histograms: HashMap<
        String,
        Family<DynamicLabels, PreAggregatedHistogram, PreAggregatedHistogramConstructor>,
    >,
}

impl MetricContainer {
    pub fn new() -> Self {
        Self {
            counters: HashMap::new(),
            gauges: HashMap::new(),
            histograms: HashMap::new(),
        }
    }
}

/// Encode a Prometheus registry to text format
pub fn encode_registry(registry: &Registry) -> Result<String, std::fmt::Error> {
    let mut buffer = String::new();
    prometheus_client::encoding::text::encode(&mut buffer, registry)?;
    Ok(buffer)
}

/// Convert `OpenTelemetry` `ResourceMetrics` to Prometheus metrics and return a Registry
pub fn convert_to_prometheus(
    resource_metrics: &ResourceMetrics,
    metrics: &Arc<Mutex<MetricContainer>>,
) -> Registry {
    let mut registry = Registry::default();
    // Process each scope's metrics
    for scope in resource_metrics.scope_metrics() {
        for metric in scope.metrics() {
            let metric_name = sanitize_name(metric.name());

            // Handle different metric types using the enum pattern
            use opentelemetry_sdk::metrics::data::{AggregatedMetrics, MetricData};

            match metric.data() {
                AggregatedMetrics::F64(MetricData::Gauge(gauge)) => {
                    register_gauge_f64(
                        &mut registry,
                        &metric_name,
                        metric.description(),
                        gauge,
                        metrics,
                    );
                }
                AggregatedMetrics::I64(MetricData::Gauge(gauge)) => {
                    register_gauge_i64(
                        &mut registry,
                        &metric_name,
                        metric.description(),
                        gauge,
                        metrics,
                    );
                }
                AggregatedMetrics::U64(MetricData::Gauge(gauge)) => {
                    register_gauge_u64(
                        &mut registry,
                        &metric_name,
                        metric.description(),
                        gauge,
                        metrics,
                    );
                }
                AggregatedMetrics::F64(MetricData::Sum(sum)) => {
                    if sum.is_monotonic() {
                        register_counter_f64(
                            &mut registry,
                            &metric_name,
                            metric.description(),
                            sum,
                            metrics,
                        );
                    } else {
                        register_gauge_from_sum_f64(
                            &mut registry,
                            &metric_name,
                            metric.description(),
                            sum,
                            metrics,
                        );
                    }
                }
                AggregatedMetrics::I64(MetricData::Sum(sum)) => {
                    if sum.is_monotonic() {
                        register_counter_i64(
                            &mut registry,
                            &metric_name,
                            metric.description(),
                            sum,
                            metrics,
                        );
                    } else {
                        register_gauge_from_sum_i64(
                            &mut registry,
                            &metric_name,
                            metric.description(),
                            sum,
                            metrics,
                        );
                    }
                }
                AggregatedMetrics::U64(MetricData::Sum(sum)) => {
                    if sum.is_monotonic() {
                        register_counter_u64(
                            &mut registry,
                            &metric_name,
                            metric.description(),
                            sum,
                            metrics,
                        );
                    } else {
                        register_gauge_from_sum_u64(
                            &mut registry,
                            &metric_name,
                            metric.description(),
                            sum,
                            metrics,
                        );
                    }
                }
                AggregatedMetrics::F64(MetricData::ExponentialHistogram(histogram)) => {
                    register_exponential_histogram_f64(
                        &mut registry,
                        &metric_name,
                        metric.description(),
                        histogram,
                        metrics,
                    );
                }
                AggregatedMetrics::I64(MetricData::ExponentialHistogram(histogram)) => {
                    register_exponential_histogram_i64(
                        &mut registry,
                        &metric_name,
                        metric.description(),
                        histogram,
                        metrics,
                    );
                }
                AggregatedMetrics::U64(MetricData::ExponentialHistogram(histogram)) => {
                    register_exponential_histogram_u64(
                        &mut registry,
                        &metric_name,
                        metric.description(),
                        histogram,
                        metrics,
                    );
                }
                _ => {
                    // Other metric types not supported
                }
            }
        }
    }

    registry
}

// Helper functions to register different metric types

fn register_gauge_f64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    gauge: &opentelemetry_sdk::metrics::data::Gauge<f64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = gauge.data_points().collect();
    if points.is_empty() {
        return;
    }

    let gauge_family = Family::<DynamicLabels, Gauge<i64>>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        // Convert f64 to i64 with microsecond precision
        gauge_family
            .get_or_create(&labels)
            .set((point.value() * 1000000.0) as i64);
    }

    let mut container = metrics.lock();
    container
        .gauges
        .insert(name.to_owned(), gauge_family.clone());
    registry.register(name, description, gauge_family);
}

fn register_gauge_i64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    gauge: &opentelemetry_sdk::metrics::data::Gauge<i64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = gauge.data_points().collect();
    if points.is_empty() {
        return;
    }

    let gauge_family = Family::<DynamicLabels, Gauge<i64>>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        gauge_family.get_or_create(&labels).set(point.value());
    }

    let mut container = metrics.lock();
    container
        .gauges
        .insert(name.to_owned(), gauge_family.clone());
    registry.register(name, description, gauge_family);
}

fn register_gauge_u64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    gauge: &opentelemetry_sdk::metrics::data::Gauge<u64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = gauge.data_points().collect();
    if points.is_empty() {
        return;
    }

    let gauge_family = Family::<DynamicLabels, Gauge<i64>>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        gauge_family
            .get_or_create(&labels)
            .set(point.value() as i64);
    }

    let mut container = metrics.lock();
    container
        .gauges
        .insert(name.to_owned(), gauge_family.clone());
    registry.register(name, description, gauge_family);
}

fn register_counter_f64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    sum: &opentelemetry_sdk::metrics::data::Sum<f64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = sum.data_points().collect();
    if points.is_empty() {
        return;
    }

    let counter_family = Family::<DynamicLabels, Counter>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        // For counters from OTLP, we get absolute values
        // We need to increment by the value to match the current state
        counter_family
            .get_or_create(&labels)
            .inc_by(point.value() as u64);
    }

    let mut container = metrics.lock();
    container
        .counters
        .insert(name.to_owned(), counter_family.clone());
    registry.register(name, description, counter_family);
}

fn register_counter_i64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    sum: &opentelemetry_sdk::metrics::data::Sum<i64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = sum.data_points().collect();
    if points.is_empty() {
        return;
    }

    let counter_family = Family::<DynamicLabels, Counter>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        if point.value() >= 0 {
            counter_family
                .get_or_create(&labels)
                .inc_by(point.value() as u64);
        }
    }

    let mut container = metrics.lock();
    container
        .counters
        .insert(name.to_owned(), counter_family.clone());
    registry.register(name, description, counter_family);
}

fn register_counter_u64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    sum: &opentelemetry_sdk::metrics::data::Sum<u64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = sum.data_points().collect();
    if points.is_empty() {
        return;
    }

    let counter_family = Family::<DynamicLabels, Counter>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        counter_family.get_or_create(&labels).inc_by(point.value());
    }

    let mut container = metrics.lock();
    container
        .counters
        .insert(name.to_owned(), counter_family.clone());
    registry.register(name, description, counter_family);
}

fn register_gauge_from_sum_f64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    sum: &opentelemetry_sdk::metrics::data::Sum<f64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = sum.data_points().collect();
    if points.is_empty() {
        return;
    }

    let gauge_family = Family::<DynamicLabels, Gauge<i64>>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        // Convert f64 to i64 with microsecond precision
        gauge_family
            .get_or_create(&labels)
            .set((point.value() * 1000000.0) as i64);
    }

    let mut container = metrics.lock();
    container
        .gauges
        .insert(name.to_owned(), gauge_family.clone());
    registry.register(name, description, gauge_family);
}

fn register_gauge_from_sum_i64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    sum: &opentelemetry_sdk::metrics::data::Sum<i64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = sum.data_points().collect();
    if points.is_empty() {
        return;
    }

    let gauge_family = Family::<DynamicLabels, Gauge<i64>>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        gauge_family.get_or_create(&labels).set(point.value());
    }

    let mut container = metrics.lock();
    container
        .gauges
        .insert(name.to_owned(), gauge_family.clone());
    registry.register(name, description, gauge_family);
}

fn register_gauge_from_sum_u64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    sum: &opentelemetry_sdk::metrics::data::Sum<u64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = sum.data_points().collect();
    if points.is_empty() {
        return;
    }

    let gauge_family = Family::<DynamicLabels, Gauge<i64>>::default();

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        gauge_family
            .get_or_create(&labels)
            .set(point.value() as i64);
    }

    let mut container = metrics.lock();
    container
        .gauges
        .insert(name.to_owned(), gauge_family.clone());
    registry.register(name, description, gauge_family);
}

/// A histogram that holds pre-aggregated data (sum, count, buckets) directly,
/// avoiding the lossy `observe()` approximation. Implements `EncodeMetric` so
/// Prometheus text encoding emits exact values from the `OTel` source.
#[derive(Debug, Clone)]
pub struct PreAggregatedHistogram {
    inner: Arc<RwLock<PreAggregatedHistogramInner>>,
}

#[derive(Debug)]
struct PreAggregatedHistogramInner {
    sum: f64,
    count: u64,

    /// `(upper_bound, count)` pairs — non-cumulative per-bucket counts.
    /// The prometheus-client encoder converts these to cumulative during
    /// text encoding.
    buckets: Vec<(f64, u64)>,
}

impl PreAggregatedHistogram {
    /// Populate from an `OTel` exponential histogram data point.
    ///
    /// Only positive and zero observations are mapped to Prometheus buckets.
    /// Negative observations cannot be faithfully represented in Prometheus's
    /// `le`-based histogram model, so we log a warning if any are present.
    /// In practice all our histograms track durations and sizes which are
    /// always non-negative.
    fn set_from_exponential(
        &self,
        scale: i8,
        positive_bucket: &opentelemetry_sdk::metrics::data::ExponentialBucket,
        negative_bucket: &opentelemetry_sdk::metrics::data::ExponentialBucket,
        zero_count: u64,
        sum: f64,
        count: u64,
    ) {
        let negative_count: u64 = negative_bucket.counts().sum();
        if negative_count > 0 {
            tracing::warn!(
                negative_count,
                "Histogram has negative observations which \
                 cannot be represented in Prometheus and will be dropped"
            );
        }

        let positive_counts: Vec<u64> = positive_bucket.counts().collect();
        self.set_from_raw_buckets(
            scale,
            positive_bucket.offset(),
            &positive_counts,
            zero_count,
            sum,
            count,
        );
    }

    /// Populate from raw exponential histogram bucket data.
    ///
    /// `scale` controls bucket resolution: base = 2^(2^(-scale)).
    /// `offset` is the bucket index of the first entry in `positive_counts`.
    /// `positive_counts[i]` is the count for values in (base^(offset+i), base^(offset+i+1)].
    fn set_from_raw_buckets(
        &self,
        scale: i8,
        offset: i32,
        positive_counts: &[u64],
        zero_count: u64,
        sum: f64,
        count: u64,
    ) {
        let base = (2.0_f64).powf((2.0_f64).powi(-(scale as i32)));

        let mut buckets: Vec<(f64, u64)> = positive_counts
            .iter()
            .enumerate()
            .map(|(i, &c)| {
                let upper = base.powi(offset + i as i32 + 1);
                (upper, c)
            })
            .collect();

        // Place zero-count observations into the first bucket (or create one
        // before +Inf if there are no positive buckets).
        if zero_count > 0 {
            if let Some(first) = buckets.first_mut() {
                first.1 += zero_count;
            } else {
                buckets.push((0.0, zero_count));
            }
        }

        // Always add +Inf bucket — required by Prometheus exposition format.
        // Count of 0 is correct here because the encoder accumulates cumulatively.
        buckets.push((f64::MAX, 0));

        let mut inner = self.inner.write();
        inner.sum = sum;
        inner.count = count;
        inner.buckets = buckets;
    }
}

impl Default for PreAggregatedHistogram {
    fn default() -> Self {
        Self {
            inner: Arc::new(RwLock::new(PreAggregatedHistogramInner {
                sum: 0.0,
                count: 0,
                buckets: Vec::new(),
            })),
        }
    }
}

impl TypedMetric for PreAggregatedHistogram {
    const TYPE: MetricType = MetricType::Histogram;
}

impl EncodeMetric for PreAggregatedHistogram {
    fn encode(&self, mut encoder: MetricEncoder<'_>) -> Result<(), std::fmt::Error> {
        let inner = self.inner.read();
        encoder.encode_histogram::<NoLabelSet>(inner.sum, inner.count, &inner.buckets, None)
    }

    fn metric_type(&self) -> MetricType {
        Self::TYPE
    }
}

/// Default constructor for `Family<_, PreAggregatedHistogram, _>`.
#[derive(Clone, Default)]
pub struct PreAggregatedHistogramConstructor;

impl MetricConstructor<PreAggregatedHistogram> for PreAggregatedHistogramConstructor {
    fn new_metric(&self) -> PreAggregatedHistogram {
        PreAggregatedHistogram::default()
    }
}

fn register_exponential_histogram_f64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    histogram: &opentelemetry_sdk::metrics::data::ExponentialHistogram<f64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = histogram.data_points().collect();
    if points.is_empty() {
        return;
    }

    let histogram_family = Family::<DynamicLabels, PreAggregatedHistogram, _>::new_with_constructor(
        PreAggregatedHistogramConstructor,
    );

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        let hist = histogram_family.get_or_create(&labels);
        hist.set_from_exponential(
            point.scale(),
            point.positive_bucket(),
            point.negative_bucket(),
            point.zero_count(),
            point.sum(),
            point.count() as u64,
        );
    }

    let mut container = metrics.lock();
    container
        .histograms
        .insert(name.to_owned(), histogram_family.clone());
    registry.register(name, description, histogram_family);
}

fn register_exponential_histogram_i64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    histogram: &opentelemetry_sdk::metrics::data::ExponentialHistogram<i64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = histogram.data_points().collect();
    if points.is_empty() {
        return;
    }

    let histogram_family = Family::<DynamicLabels, PreAggregatedHistogram, _>::new_with_constructor(
        PreAggregatedHistogramConstructor,
    );

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        let hist = histogram_family.get_or_create(&labels);
        hist.set_from_exponential(
            point.scale(),
            point.positive_bucket(),
            point.negative_bucket(),
            point.zero_count(),
            point.sum() as f64,
            point.count() as u64,
        );
    }

    let mut container = metrics.lock();
    container
        .histograms
        .insert(name.to_owned(), histogram_family.clone());
    registry.register(name, description, histogram_family);
}

fn register_exponential_histogram_u64(
    registry: &mut Registry,
    name: &str,
    description: &str,
    histogram: &opentelemetry_sdk::metrics::data::ExponentialHistogram<u64>,
    metrics: &Arc<Mutex<MetricContainer>>,
) {
    let points: Vec<_> = histogram.data_points().collect();
    if points.is_empty() {
        return;
    }

    let histogram_family = Family::<DynamicLabels, PreAggregatedHistogram, _>::new_with_constructor(
        PreAggregatedHistogramConstructor,
    );

    for point in &points {
        let attrs: Vec<_> = point.attributes().cloned().collect();
        let labels = create_dynamic_labels(&attrs);
        let hist = histogram_family.get_or_create(&labels);
        hist.set_from_exponential(
            point.scale(),
            point.positive_bucket(),
            point.negative_bucket(),
            point.zero_count(),
            point.sum() as f64,
            point.count() as u64,
        );
    }

    let mut container = metrics.lock();
    container
        .histograms
        .insert(name.to_owned(), histogram_family.clone());
    registry.register(name, description, histogram_family);
}

// Helper functions

fn sanitize_name(name: &str) -> String {
    name.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

fn create_dynamic_labels(attributes: &[KeyValue]) -> DynamicLabels {
    let mut labels: Vec<(String, String)> = attributes
        .iter()
        .map(|kv| {
            (
                sanitize_name(kv.key.as_str()),
                kv.value.as_str().into_owned(),
            )
        })
        .collect();
    labels.sort_by(|a, b| a.0.cmp(&b.0)); // Ensure consistent ordering
    DynamicLabels(labels)
}

#[cfg(test)]
mod tests {
    use super::*;
    use prometheus_client::encoding::text::encode;

    /// Helper: encode a single `PreAggregatedHistogram` registered as "test"
    /// and return the Prometheus text exposition string.
    fn encode_histogram(hist: &PreAggregatedHistogram) -> String {
        let mut registry = Registry::default();
        registry.register("test", "help", hist.clone());
        let mut buf = String::new();
        encode(&mut buf, &registry).unwrap();
        buf
    }

    #[test]
    fn pre_aggregated_histogram_basic_encoding() {
        let hist = PreAggregatedHistogram::default();

        // Scale 0 → base = 2^(2^0) = 2.0
        // offset = 0, counts = [3, 5, 2]
        // Bucket boundaries: (0, 2^1=2], (2, 2^2=4], (4, 2^3=8]
        hist.set_from_raw_buckets(0, 0, &[3, 5, 2], 0, 42.0, 10);

        let output = encode_histogram(&hist);
        assert!(output.contains("test_sum 42.0"), "output: {output}");
        assert!(output.contains("test_count 10"), "output: {output}");
        // Cumulative: le=2 → 3, le=4 → 8, le=8 → 10, le=+Inf → 10
        assert!(
            output.contains(r#"test_bucket{le="2.0"} 3"#),
            "output: {output}"
        );
        assert!(
            output.contains(r#"test_bucket{le="4.0"} 8"#),
            "output: {output}"
        );
        assert!(
            output.contains(r#"test_bucket{le="8.0"} 10"#),
            "output: {output}"
        );
        assert!(
            output.contains(r#"test_bucket{le="+Inf"} 10"#),
            "output: {output}"
        );
    }

    #[test]
    fn pre_aggregated_histogram_with_zero_count() {
        let hist = PreAggregatedHistogram::default();

        // Scale 0, offset 0, one positive bucket with count 2, plus 3 zeros
        hist.set_from_raw_buckets(0, 0, &[2], 3, 5.0, 5);

        let output = encode_histogram(&hist);
        assert!(output.contains("test_count 5"), "output: {output}");
        assert!(output.contains("test_sum 5.0"), "output: {output}");
        // Zeros are added to the first bucket: 2 + 3 = 5
        // Cumulative: le=2 → 5, le=+Inf → 5
        assert!(
            output.contains(r#"test_bucket{le="2.0"} 5"#),
            "output: {output}"
        );
        assert!(
            output.contains(r#"test_bucket{le="+Inf"} 5"#),
            "output: {output}"
        );
    }

    #[test]
    fn pre_aggregated_histogram_zero_only() {
        let hist = PreAggregatedHistogram::default();

        // No positive buckets, only zeros
        hist.set_from_raw_buckets(0, 0, &[], 7, 0.0, 7);

        let output = encode_histogram(&hist);
        assert!(output.contains("test_count 7"), "output: {output}");
        assert!(output.contains("test_sum 0.0"), "output: {output}");
        // Zero-only: creates a (0.0, 7) bucket, then +Inf
        assert!(
            output.contains(r#"test_bucket{le="0.0"} 7"#),
            "output: {output}"
        );
        assert!(
            output.contains(r#"test_bucket{le="+Inf"} 7"#),
            "output: {output}"
        );
    }

    #[test]
    fn pre_aggregated_histogram_with_offset() {
        let hist = PreAggregatedHistogram::default();

        // Scale 0, offset 2, counts = [4]
        // Bucket boundary: base^(2+0+1) = 2^3 = 8
        hist.set_from_raw_buckets(0, 2, &[4], 0, 28.0, 4);

        let output = encode_histogram(&hist);
        assert!(output.contains("test_count 4"), "output: {output}");
        assert!(
            output.contains(r#"test_bucket{le="8.0"} 4"#),
            "output: {output}"
        );
        assert!(
            output.contains(r#"test_bucket{le="+Inf"} 4"#),
            "output: {output}"
        );
    }

    #[test]
    fn pre_aggregated_histogram_finer_scale() {
        let hist = PreAggregatedHistogram::default();

        // Scale 1 → base = 2^(2^(-1)) = 2^0.5 ≈ 1.4142
        // offset 0, counts = [10]
        // Bucket boundary: base^(0+0+1) = √2 ≈ 1.4142
        hist.set_from_raw_buckets(1, 0, &[10], 0, 12.0, 10);

        let output = encode_histogram(&hist);
        assert!(output.contains("test_count 10"), "output: {output}");
        // Check that the bucket upper bound is approximately √2
        // The exact value depends on floating point, so just check the prefix
        assert!(
            output.contains("test_bucket{le=\"1.41421"),
            "expected bucket ≈ √2, output: {output}"
        );
    }

    #[test]
    fn escape_dot_in_counter_label() {
        let attrs = vec![KeyValue::new("otel.metric.overflow", "true")];
        let labels = create_dynamic_labels(&attrs);
        let family = Family::<DynamicLabels, Counter>::default();
        family.get_or_create(&labels).inc();

        let mut registry = Registry::default();
        registry.register("test_counter", "help", family);
        let mut buf = String::new();
        encode(&mut buf, &registry).unwrap();

        assert!(buf.contains(r#"test_counter_total{otel_metric_overflow="true"} 1"#));
    }
}