pub mod bstream;
pub mod decode_limits;
pub mod histogram;
pub mod varbit;
pub mod xor;
pub mod xor2;
use crate::error::{Error, Result};
pub use decode_limits::DecodeLimits;
pub const STALE_NAN_BITS: u64 = 0x7ff0_0000_0000_0002;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub offset: i32,
pub length: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HistogramKind {
Int,
Float,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum CounterResetHint {
Unknown = 0,
CounterReset = 1,
NotCounterReset = 2,
GaugeType = 3,
}
impl CounterResetHint {
pub fn from_header_bits(bits: u8) -> Self {
match bits & 0b11 {
1 => Self::NotCounterReset,
2 => Self::CounterReset,
3 => Self::GaugeType,
_ => Self::Unknown,
}
}
pub fn as_u8(self) -> u8 {
self as u8
}
pub fn for_sample(chunk_hint: Self, sample_index: u64) -> Self {
match chunk_hint {
Self::GaugeType => Self::GaugeType,
_ if sample_index > 1 => Self::NotCounterReset,
_ => Self::Unknown,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct IntHistogramSample<'a> {
pub ts: i64,
pub st: i64,
pub count: u64,
pub zero_count: u64,
pub sum: f64,
pub schema: i32,
pub zero_threshold: f64,
pub pos_spans: &'a [Span],
pub neg_spans: &'a [Span],
pub pos_counts: &'a [i64],
pub neg_counts: &'a [i64],
pub pos_deltas: &'a [i64],
pub neg_deltas: &'a [i64],
pub custom_values: &'a [f64],
pub chunk_reset_hint: CounterResetHint,
pub counter_reset_hint: CounterResetHint,
}
#[derive(Debug, Clone, Copy)]
pub struct FloatHistogramSample<'a> {
pub ts: i64,
pub st: i64,
pub count: f64,
pub zero_count: f64,
pub sum: f64,
pub schema: i32,
pub zero_threshold: f64,
pub pos_spans: &'a [Span],
pub neg_spans: &'a [Span],
pub pos_buckets: &'a [f64],
pub neg_buckets: &'a [f64],
pub custom_values: &'a [f64],
pub chunk_reset_hint: CounterResetHint,
pub counter_reset_hint: CounterResetHint,
}
pub trait SampleSink {
fn push_float(&mut self, ts: i64, value: f64);
fn push_int_histogram(&mut self, h: &IntHistogramSample<'_>);
fn push_float_histogram(&mut self, h: &FloatHistogramSample<'_>);
}
#[derive(Debug, Clone, Copy, Default)]
pub struct DecodeOutcome {
pub floats: u64,
pub int_histograms: u64,
pub float_histograms: u64,
}
impl DecodeOutcome {
pub fn histograms(&self) -> u64 {
self.int_histograms + self.float_histograms
}
pub fn samples(&self) -> u64 {
self.floats + self.histograms()
}
}
pub const ENC_NONE: u8 = 0;
pub const ENC_XOR: u8 = 1;
pub const ENC_HISTOGRAM: u8 = 2;
pub const ENC_FLOAT_HISTOGRAM: u8 = 3;
pub const ENC_XOR2: u8 = 4;
pub const ENC_HISTOGRAM_ST: u8 = 5;
pub const ENC_FLOAT_HISTOGRAM_ST: u8 = 6;
pub fn encoding_name(enc: u8) -> &'static str {
match enc {
ENC_NONE => "none",
ENC_XOR => "XOR",
ENC_HISTOGRAM => "histogram",
ENC_FLOAT_HISTOGRAM => "floathistogram",
ENC_XOR2 => "XOR2",
ENC_HISTOGRAM_ST => "histogramST",
ENC_FLOAT_HISTOGRAM_ST => "floathistogramST",
_ => "unknown",
}
}
pub fn is_known_encoding(enc: u8) -> bool {
matches!(
enc,
ENC_NONE
| ENC_XOR
| ENC_HISTOGRAM
| ENC_FLOAT_HISTOGRAM
| ENC_XOR2
| ENC_HISTOGRAM_ST
| ENC_FLOAT_HISTOGRAM_ST
)
}
pub fn encoding_histogram_kind(enc: u8) -> Option<HistogramKind> {
match enc {
ENC_HISTOGRAM | ENC_HISTOGRAM_ST => Some(HistogramKind::Int),
ENC_FLOAT_HISTOGRAM | ENC_FLOAT_HISTOGRAM_ST => Some(HistogramKind::Float),
_ => None,
}
}
pub fn decode<S: SampleSink>(encoding: u8, data: &[u8], sink: &mut S) -> Result<DecodeOutcome> {
decode_with_limits(encoding, data, &DecodeLimits::default(), sink)
}
pub fn decode_with_limits<S: SampleSink>(
encoding: u8,
data: &[u8],
limits: &DecodeLimits,
sink: &mut S,
) -> Result<DecodeOutcome> {
match encoding {
ENC_NONE => Ok(DecodeOutcome::default()),
ENC_XOR => Ok(DecodeOutcome {
floats: xor::decode(data, limits, sink)?,
..Default::default()
}),
ENC_XOR2 => Ok(DecodeOutcome {
floats: xor2::decode(data, limits, sink)?,
..Default::default()
}),
ENC_HISTOGRAM => Ok(DecodeOutcome {
int_histograms: histogram::decode_int(data, false, limits, sink)?,
..Default::default()
}),
ENC_HISTOGRAM_ST => Ok(DecodeOutcome {
int_histograms: histogram::decode_int(data, true, limits, sink)?,
..Default::default()
}),
ENC_FLOAT_HISTOGRAM => Ok(DecodeOutcome {
float_histograms: histogram::decode_float(data, false, limits, sink)?,
..Default::default()
}),
ENC_FLOAT_HISTOGRAM_ST => Ok(DecodeOutcome {
float_histograms: histogram::decode_float(data, true, limits, sink)?,
..Default::default()
}),
other => Err(Error::Unsupported(format!(
"chunk encoding {other} ({})",
encoding_name(other)
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reset_hint_mapping() {
assert_eq!(
CounterResetHint::from_header_bits(0),
CounterResetHint::Unknown
);
assert_eq!(
CounterResetHint::from_header_bits(1),
CounterResetHint::NotCounterReset
);
assert_eq!(
CounterResetHint::from_header_bits(2),
CounterResetHint::CounterReset
);
assert_eq!(
CounterResetHint::from_header_bits(3),
CounterResetHint::GaugeType
);
assert_eq!(
CounterResetHint::from_header_bits(0xff),
CounterResetHint::GaugeType
);
assert_eq!(CounterResetHint::CounterReset.as_u8(), 1);
assert_eq!(CounterResetHint::NotCounterReset.as_u8(), 2);
}
#[test]
fn per_sample_hint_follows_prometheus() {
use CounterResetHint::*;
assert_eq!(CounterResetHint::for_sample(GaugeType, 1), GaugeType);
assert_eq!(CounterResetHint::for_sample(CounterReset, 1), Unknown);
assert_eq!(
CounterResetHint::for_sample(CounterReset, 2),
NotCounterReset
);
assert_eq!(CounterResetHint::for_sample(Unknown, 7), NotCounterReset);
}
#[test]
fn histogram_kind_per_encoding() {
assert_eq!(
encoding_histogram_kind(ENC_HISTOGRAM),
Some(HistogramKind::Int)
);
assert_eq!(
encoding_histogram_kind(ENC_FLOAT_HISTOGRAM_ST),
Some(HistogramKind::Float)
);
assert_eq!(encoding_histogram_kind(ENC_XOR2), None);
assert!(is_known_encoding(ENC_NONE));
assert!(is_known_encoding(ENC_FLOAT_HISTOGRAM_ST));
assert!(!is_known_encoding(7));
}
#[test]
fn outcome_counts_add_up() {
let o = DecodeOutcome {
floats: 3,
int_histograms: 2,
float_histograms: 1,
};
assert_eq!(o.histograms(), 3);
assert_eq!(o.samples(), 6);
}
}