vector/sinks/greptimedb/metrics/
batch.rs

1use super::request_builder::{
2    DISTRIBUTION_QUANTILES, DISTRIBUTION_STAT_FIELD_COUNT, SUMMARY_STAT_FIELD_COUNT,
3};
4use vector_lib::{
5    event::{Metric, MetricValue},
6    stream::batcher::limiter::ItemBatchSize,
7};
8
9const F64_BYTE_SIZE: usize = 8;
10const I64_BYTE_SIZE: usize = 8;
11
12/// GreptimeDBBatchSizer is a batch sizer for metrics.
13#[derive(Default)]
14pub struct GreptimeDBBatchSizer;
15
16impl GreptimeDBBatchSizer {
17    pub fn estimated_size_of(&self, item: &Metric) -> usize {
18        // Metric name.
19        item.series().name().name().len()
20        // Metric namespace, with an additional 1 to account for the namespace separator.
21        + item.series().name().namespace().map(|s| s.len() + 1).unwrap_or(0)
22        // Metric tags, with an additional 1 per tag to account for the tag key/value separator.
23        + item.series().tags().map(|t| {
24            t.iter_all().map(|(k, v)| {
25                k.len() + 1 + v.map(|v| v.len()).unwrap_or(0)
26            })
27            .sum()
28        })
29            .unwrap_or(0)
30            // timestamp
31            + I64_BYTE_SIZE
32            +
33        // value size
34            match item.value() {
35                MetricValue::Counter { .. } | MetricValue::Gauge { .. } | MetricValue::Set { ..} => F64_BYTE_SIZE,
36                MetricValue::Distribution { .. } => F64_BYTE_SIZE * (DISTRIBUTION_QUANTILES.len() + DISTRIBUTION_STAT_FIELD_COUNT),
37                MetricValue::AggregatedHistogram { buckets, .. }  => F64_BYTE_SIZE * (buckets.len() + SUMMARY_STAT_FIELD_COUNT),
38                MetricValue::AggregatedSummary { quantiles, .. } => F64_BYTE_SIZE * (quantiles.len() + SUMMARY_STAT_FIELD_COUNT),
39                MetricValue::Sketch { .. } => F64_BYTE_SIZE * (DISTRIBUTION_QUANTILES.len() + DISTRIBUTION_STAT_FIELD_COUNT),
40            }
41    }
42}
43
44impl ItemBatchSize<Metric> for GreptimeDBBatchSizer {
45    fn size(&self, item: &Metric) -> usize {
46        self.estimated_size_of(item)
47    }
48}