Skip to main content

InventoryTracker

Struct InventoryTracker 

Source
pub struct InventoryTracker<P: Producer> { /* private fields */ }
Expand description

Emits inventory records for one shared resource.

§Hashing and sampling

InventoryTracker decides whether to emit a message for a given record based on the configured sample_rate and a hash of the record ID passed in by the caller. A sample rate of 1.0 means it will emit messages for 100% of records. A sample rate of 0.5 means it will emit messages for 50% of records. If a record is sampled out, no change to that record will ever emit a message. If a record is included in the sample, every change to that record will emit a message.

The hash is what InventoryTracker actually uses to populate the record_id message field. Each message also includes the sample rate that was in effect at the time.

The hash is a permanent wire contract. Changing the algorithm, the byte ranges, or introducing a salt renames every record, and consumers will double-count until the old identifiers age out.

§Example

use objectstore_inventory_tracker::{InventoryTracker, NoopProducer};
use std::time::SystemTime;

let tracker = InventoryTracker::new(NoopProducer, "example_resource", 1.0);

tracker.write(
    "example_feature/org.123/project.456/objects/abc",
    "example_feature",
    4096,
    SystemTime::now(),
    None,
    Some(123),
    Some(456),
)?;

Implementations§

Source§

impl<P: Producer> InventoryTracker<P>

Source

pub fn new( producer: P, shared_resource_id: impl Into<String>, sample_rate: f64, ) -> Self

Creates a tracker emitting for shared_resource_id at sample_rate.

shared_resource_id is meant to match a label on a provisioned storage backend so that downstream consumers can join the change stream dataset with, for instance, billing info.

sample_rate is clamped to [0, 1]. A rate of 1.0 emits every record and short-circuits the sampling check entirely. A rate of 0.0 emits no records.

Source

pub fn shared_resource_id(&self) -> &str

The storage resource this tracker emits for.

Source

pub fn sample_rate(&self) -> f64

The fraction of records being emitted.

Source

pub fn write( &self, storage_key: &str, app_feature: &str, size: u64, timestamp: SystemTime, expiration_time: Option<SystemTime>, organization_id: Option<u64>, project_id: Option<u64>, ) -> Result<(), P::Error>

Emits a WRITE: the record was created, or replaced with new contents.

Does nothing and returns Ok(()) if storage_key is not sampled.

Source

pub fn update( &self, storage_key: &str, app_feature: &str, timestamp: SystemTime, expiration_time: Option<SystemTime>, organization_id: Option<u64>, project_id: Option<u64>, ) -> Result<(), P::Error>

Emits an UPDATE: metadata changed but the stored size did not.

Does nothing and returns Ok(()) if storage_key is not sampled.

Source

pub fn delete( &self, storage_key: &str, app_feature: &str, timestamp: SystemTime, ) -> Result<(), P::Error>

Emits a DELETE: the record is gone.

Does nothing and returns Ok(()) if storage_key is not sampled.

Source

pub fn join( &self, timeout: Duration, ) -> impl Future<Output = Result<(), P::Error>> + Send + use<P>
where P: Clone + Send + Sync + 'static, P::Error: Send + 'static,

Waits for emitted records to be delivered, or until timeout elapses.

Call this during shutdown, within whatever budget the service allows for draining. Without it, records emitted moments before exit are still in a local queue and are lost with the process.

Trait Implementations§

Source§

impl<P: Clone + Producer> Clone for InventoryTracker<P>

Source§

fn clone(&self) -> InventoryTracker<P>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<P: Debug + Producer> Debug for InventoryTracker<P>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P> Freeze for InventoryTracker<P>
where P: Freeze,

§

impl<P> RefUnwindSafe for InventoryTracker<P>
where P: RefUnwindSafe,

§

impl<P> Send for InventoryTracker<P>
where P: Send,

§

impl<P> Sync for InventoryTracker<P>
where P: Sync,

§

impl<P> Unpin for InventoryTracker<P>
where P: Unpin,

§

impl<P> UnsafeUnpin for InventoryTracker<P>
where P: UnsafeUnpin,

§

impl<P> UnwindSafe for InventoryTracker<P>
where P: 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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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> 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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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