DDSketch

Struct DDSketch 

Source
pub struct DDSketch<M: IndexMapping = LogarithmicMapping, S: Store = CollapsingLowestDenseStore> { /* private fields */ }
Expand description

A fast and fully-mergeable quantile sketch with relative-error guarantees.

This implementation supports most of the capabilities of the various official DDSketch implementations, such as:

  • support for tracking negative and positive values
  • multiple store types (sparse, dense, collapsing)
  • configurable index interpolation schemes (only logarithmic currently supported)

Defaults to using a “low collapsing” dense store with a logarithmic index mapping. This works well for tracking values like time durations/latencies where the tail latencies (higher percentiles) matter most.

§Example

use ddsketch::canonical::DDSketch;

let mut sketch = DDSketch::with_relative_accuracy(0.01).unwrap();
sketch.add(1.0);
sketch.add(2.0);
sketch.add(3.0);

let median = sketch.quantile(0.5).unwrap();

Implementations§

Source§

impl DDSketch<LogarithmicMapping, CollapsingLowestDenseStore>

Source

pub fn with_relative_accuracy( relative_accuracy: f64, ) -> Result<Self, &'static str>

Creates a new DDSketch with the given relative accuracy.

Defaults to logarithmic mapping and the “low collapsing” dense store, with a maximum of 2048 bins per store.

§Errors

If the relative accuracy is not between 0 and 1, an error is returned.

Source§

impl<M: IndexMapping, S: Store> DDSketch<M, S>

Source

pub fn new(mapping: M, positive_store: S, negative_store: S) -> Self

Creates a new DDSketch with the given mapping and stores.

Source

pub fn add(&mut self, value: f64)

Adds a single value to the sketch.

Source

pub fn add_n(&mut self, value: f64, n: u64)

Adds a value to the sketch with the given count.

This is useful for weighted values or pre-aggregated data.

Source

pub fn quantile(&self, q: f64) -> Option<f64>

Returns the approximate value at the given quantile.

The quantile must be in the range of [0, 1].

Returns None if the sketch is empty, or if the quantile is out of bounds. Otherwise, returns the approximate value.

Source

pub fn merge(&mut self, other: &Self)
where M: PartialEq,

Merges another sketch into this one.

The other sketch must use the same mapping type.

Source

pub fn is_empty(&self) -> bool

Returns true if the sketch is empty.

Source

pub fn count(&self) -> u64

Returns the total number of values added to the sketch.

Source

pub fn clear(&mut self)

Clears the sketch, removing all values.

Source

pub fn mapping(&self) -> &M

Returns a reference to the index mapping.

Source

pub fn positive_store(&self) -> &S

Returns a reference to the positive value store.

Source

pub fn negative_store(&self) -> &S

Returns a reference to the negative value store.

Source

pub fn zero_count(&self) -> u64

Returns the count of values mapped to zero.

Source

pub fn relative_accuracy(&self) -> f64

Returns the relative accuracy of this sketch.

Source

pub fn from_proto( proto: &ProtoDDSketch, mapping: M, ) -> Result<Self, ProtoConversionError>
where S: Default,

Creates a DDSketch from a protobuf DDSketch message.

This validates that the protobuf’s index mapping is compatible with the mapping type M, then populates the stores with the bin data.

§Arguments
  • proto - The protobuf DDSketch message to convert from
  • mapping - The mapping instance to use (must be compatible with proto’s mapping)
§Errors

Returns an error if:

  • The protobuf is missing a mapping
  • The mapping parameters don’t match the provided mapping
  • Any bin counts are negative or non-integer
  • The zero count is negative or non-integer
§Note

The protobuf DDSketch does not include sum, min, max, or count fields. These are computed or set to defaults:

  • count: sum of all bin counts plus zero_count
  • sum, min, max: set to sentinel defaults (cannot be recovered from proto)
Source

pub fn to_proto(&self) -> ProtoDDSketch

Converts this DDSketch to a protobuf DDSketch message.

§Note

The protobuf DDSketch does not include sum, min, max, or count fields. This information is lost in the conversion.

Trait Implementations§

Source§

impl<M: Clone + IndexMapping, S: Clone + Store> Clone for DDSketch<M, S>

Source§

fn clone(&self) -> DDSketch<M, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Debug + IndexMapping, S: Debug + Store> Debug for DDSketch<M, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M: IndexMapping + Default, S: Store + Default> Default for DDSketch<M, S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<M: IndexMapping + PartialEq, S: Store + PartialEq> PartialEq for DDSketch<M, S>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<M: IndexMapping + PartialEq, S: Store + PartialEq> Eq for DDSketch<M, S>

Auto Trait Implementations§

§

impl<M, S> Freeze for DDSketch<M, S>
where M: Freeze, S: Freeze,

§

impl<M, S> RefUnwindSafe for DDSketch<M, S>

§

impl<M, S> Send for DDSketch<M, S>

§

impl<M, S> Sync for DDSketch<M, S>

§

impl<M, S> Unpin for DDSketch<M, S>
where M: Unpin, S: Unpin,

§

impl<M, S> UnwindSafe for DDSketch<M, S>
where M: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more