Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: merge gauges properly and avoid dropping existing points #444

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/saluki-context/src/tags/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,22 @@ impl Tagged for TagSet {
}
}

impl fmt::Display for TagSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[")?;

for (i, tag) in self.0.iter().enumerate() {
if i > 0 {
write!(f, ",")?;
}

write!(f, "{}", tag.as_str())?;
}

write!(f, "]")
}
}

/// A shared, read-only set of tags.
#[derive(Clone, Debug)]
pub struct SharedTagSet(Arc<TagSet>);
Expand Down
60 changes: 59 additions & 1 deletion lib/saluki-event/src/metric/value/histogram.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::num::NonZeroU64;
use std::{fmt, num::NonZeroU64};

use ordered_float::OrderedFloat;
use smallvec::SmallVec;
Expand Down Expand Up @@ -225,6 +225,15 @@ impl From<f64> for HistogramPoints {
}
}

impl From<(u64, f64)> for HistogramPoints {
fn from((ts, value): (u64, f64)) -> Self {
let mut histogram = Histogram::default();
histogram.insert(value, SampleRate::unsampled());

Self(TimestampedValue::from((ts, histogram)).into())
}
}

impl<const N: usize> From<[f64; N]> for HistogramPoints {
fn from(values: [f64; N]) -> Self {
let mut histogram = Histogram::default();
Expand All @@ -236,6 +245,33 @@ impl<const N: usize> From<[f64; N]> for HistogramPoints {
}
}

impl<const N: usize> From<(u64, [f64; N])> for HistogramPoints {
fn from((ts, values): (u64, [f64; N])) -> Self {
let mut histogram = Histogram::default();
for value in values {
histogram.insert(value, SampleRate::unsampled());
}

Self(TimestampedValue::from((ts, histogram)).into())
}
}

impl<const N: usize> From<[(u64, f64); N]> for HistogramPoints {
fn from(values: [(u64, f64); N]) -> Self {
Self(
values
.into_iter()
.map(|(ts, value)| {
let mut histogram = Histogram::default();
histogram.insert(value, SampleRate::unsampled());

(ts, histogram)
})
.into(),
)
}
}

impl<'a> From<&'a [f64]> for HistogramPoints {
fn from(values: &'a [f64]) -> Self {
let mut histogram = Histogram::default();
Expand Down Expand Up @@ -291,6 +327,28 @@ impl<'a> IntoIterator for &'a mut HistogramPoints {
}
}

impl fmt::Display for HistogramPoints {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[")?;
for (i, point) in self.0.values.iter().enumerate() {
if i > 0 {
write!(f, ",")?;
}

let ts = point.timestamp.map(|ts| ts.get()).unwrap_or_default();
write!(f, "({}, [", ts)?;
for (j, sample) in point.value.samples().iter().enumerate() {
if j > 0 {
write!(f, ",")?;
}
write!(f, "{{{} * {}}}", sample.value, sample.weight)?;
}
write!(f, "])")?;
}
write!(f, "]")
}
}

pub struct HistogramIter {
inner: smallvec::IntoIter<[TimestampedValue<Histogram>; 1]>,
}
Expand Down
Loading