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
impl ExceptionBuffer
Sourcepub fn with_default_dir(component: &str) -> Result<Self, BufferError>
pub fn with_default_dir(component: &str) -> Result<Self, BufferError>
使用默认目录创建缓冲区,遵循以下优先级:
- 环境变量
TOKENFLEET_EXCEPTIONS_DIR(如果设置) - 平台默认:
- Linux / macOS:
~/.tokenfleet-ai/exceptions/ - Windows:
%APPDATA%/tokenfleet-ai/exceptions/
- Linux / macOS:
如果目标目录不存在会自动创建。
§Errors
Returns [BufferError] when the data directory cannot be determined
or the database cannot be created.
Sourcepub fn collect(&self, record: ExceptionRecord)
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.
Sourcepub fn flush(&self) -> Result<(), BufferError>
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.
Sourcepub fn load(&self) -> Result<Vec<AggregatedException>, BufferError>
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.
Sourcepub fn mark_reported(&self, signatures: &[String]) -> Result<(), BufferError>
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.
Sourcepub fn delete_reported_older_than(
&self,
cutoff: &DateTime<Utc>,
) -> Result<usize, BufferError>
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.
Sourcepub fn contains_signature(&self, signature: &str) -> bool
pub fn contains_signature(&self, signature: &str) -> bool
Check whether the given signature exists in the in-memory buffer.
Sourcepub fn unreported_samples(&self) -> Vec<ExceptionRecord>
pub fn unreported_samples(&self) -> Vec<ExceptionRecord>
Return all unreported sample records from the in-memory buffer.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ExceptionBuffer
impl Freeze for ExceptionBuffer
impl Send for ExceptionBuffer
impl Sync for ExceptionBuffer
impl Unpin for ExceptionBuffer
impl UnsafeUnpin for ExceptionBuffer
impl UnwindSafe for ExceptionBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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