Skip to main content

ExceptionBuffer

Struct ExceptionBuffer 

Source
pub struct ExceptionBuffer { /* private fields */ }
Expand description

In-memory and SQLite-backed buffer for exception aggregation.

Concurrent writes go through the DashMap for lock-free aggregation. flush persists the in-memory state to SQLite, and load restores it on restart.

§Examples

use exception_collector::{ExceptionBuffer, ExceptionRecord, ExceptionKind};

let buffer = ExceptionBuffer::new("/tmp/exceptions.db")?;
let record = ExceptionRecord::new("my-comp", ExceptionKind::Panic, "oops", "");
buffer.collect(record);
buffer.flush()?;

Implementations§

Source§

impl ExceptionBuffer

Source

pub fn new(db_path: &Path) -> Result<Self, BufferError>

Create a new buffer backed by a SQLite database at db_path.

The SQLite schema is created if it does not already exist. Call load afterwards to restore previous state.

§Errors

Returns [BufferError::Sqlite] when the database cannot be opened or the schema cannot be created.

Source

pub fn with_default_dir(component: &str) -> Result<Self, BufferError>

使用默认目录创建缓冲区,遵循以下优先级:

  1. 环境变量 TOKENFLEET_EXCEPTIONS_DIR(如果设置)
  2. 平台默认:
    • Linux / macOS: ~/.tokenfleet-ai/exceptions/
    • Windows: %APPDATA%/tokenfleet-ai/exceptions/

如果目标目录不存在会自动创建。

§Errors

Returns [BufferError] when the data directory cannot be determined or the database cannot be created.

Source

pub fn collect(&self, record: ExceptionRecord)

Aggregate an exception record into the in-memory buffer.

If the computed signature already exists, the count is incremented and last_seen is updated. Otherwise, a new aggregated entry is created with the record as the sample.

Source

pub fn flush(&self) -> Result<(), BufferError>

Persist all in-memory aggregated exceptions to SQLite.

Existing rows are upserted (insert-or-replace) so that both new entries and updated counts/timestamps are synced.

§Errors

Returns [BufferError::Sqlite] or [BufferError::SerdeJson] when the persistence layer fails.

Source

pub fn load(&self) -> Result<Vec<AggregatedException>, BufferError>

Load aggregated exceptions from SQLite into the in-memory buffer.

Intended to be called once at startup to recover state from the previous run. Entries already present in memory are overwritten by the SQLite version.

§Errors

Returns [BufferError] when the database cannot be read.

Source

pub fn mark_reported(&self, signatures: &[String]) -> Result<(), BufferError>

Mark the given signatures as reported in both memory and SQLite.

Signatures not present in the buffer are silently ignored.

§Errors

Returns [BufferError::Sqlite] when the database update fails.

Source

pub fn delete_reported_older_than( &self, cutoff: &DateTime<Utc>, ) -> Result<usize, BufferError>

Delete reported exceptions older than cutoff from the database.

Prevents unbounded disk growth by cleaning up old, already-reported data. Returns the number of deleted rows.

§Errors

Returns [BufferError::Sqlite] when the database operation fails.

Source

pub fn len(&self) -> usize

Return the number of distinct signatures in the in-memory buffer.

Source

pub fn is_empty(&self) -> bool

Return true if the in-memory buffer contains no entries.

Source

pub fn contains_signature(&self, signature: &str) -> bool

Check whether the given signature exists in the in-memory buffer.

Source

pub fn unreported_samples(&self) -> Vec<ExceptionRecord>

Return all unreported sample records from the in-memory buffer.

Trait Implementations§

Source§

impl Debug for ExceptionBuffer

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.