forked from open-telemetry/otel-arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
67 lines (58 loc) · 1.68 KB
/
Copy pathlib.rs
File metadata and controls
67 lines (58 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//! State stores
use serde::ser::{SerializeSeq, Serializer};
use std::collections::VecDeque;
pub mod conditions;
pub mod error;
pub mod phase;
mod pipeline_rt_status;
pub mod pipeline_status;
pub mod reporter;
pub mod store;
use otap_df_telemetry::event::ObservedEvent;
use serde::Serialize;
/// A ring buffer for storing recent observed events.
///
/// When the buffer reaches capacity, the oldest event is dropped to make room
/// for new events. Events are serialized in reverse order (newest first).
#[derive(Debug, Clone)]
pub struct ObservedEventRingBuffer {
buf: VecDeque<ObservedEvent>,
cap: usize,
}
impl Serialize for ObservedEventRingBuffer {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.buf.len()))?;
for ev in self.buf.iter().rev() {
// <— reverse iteration
seq.serialize_element(ev)?;
}
seq.end()
}
}
impl ObservedEventRingBuffer {
/// Create a new ring buffer with the given capacity.
#[must_use]
pub fn new(cap: usize) -> Self {
Self {
buf: VecDeque::with_capacity(cap),
cap,
}
}
/// Push an event into the ring buffer, dropping the oldest if full.
pub fn push(&mut self, event: ObservedEvent) {
if self.buf.len() == self.cap {
_ = self.buf.pop_front(); // drop oldest
}
self.buf.push_back(event);
}
/// Returns true if the buffer is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
}