grow_better_stack_logs/logging/
query.rs1use std::fmt;
5use std::sync::atomic::{AtomicBool, Ordering};
6use std::sync::Arc;
7use std::time::{Duration, SystemTime};
8
9use async_trait::async_trait;
10use serde::Serialize;
11use tokio::sync::Notify;
12
13use super::LogRecord;
14use crate::core::{poll_until, Error, PollOptions, Result};
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
18pub enum SortOrder {
19 #[default]
21 Desc,
22 Asc,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct SearchOptions {
29 pub start: Option<SystemTime>,
31 pub end: Option<SystemTime>,
33 pub limit: usize,
35 pub order: SortOrder,
37 pub cursor: Option<String>,
39}
40
41impl Default for SearchOptions {
42 fn default() -> Self {
43 SearchOptions {
44 start: None,
45 end: None,
46 limit: 100,
47 order: SortOrder::Desc,
48 cursor: None,
49 }
50 }
51}
52
53impl SearchOptions {
54 pub fn limit(mut self, limit: usize) -> Self {
56 self.limit = limit;
57 self
58 }
59 pub fn range(mut self, start: Option<SystemTime>, end: Option<SystemTime>) -> Self {
61 self.start = start;
62 self.end = end;
63 self
64 }
65 pub fn order(mut self, order: SortOrder) -> Self {
67 self.order = order;
68 self
69 }
70 pub fn cursor(mut self, cursor: impl Into<String>) -> Self {
72 self.cursor = Some(cursor.into());
73 self
74 }
75}
76
77#[derive(Debug, Clone, Default, PartialEq, Serialize)]
79#[serde(rename_all = "camelCase")]
80pub struct LogSearchResult {
81 pub records: Vec<LogRecord>,
83 #[serde(skip_serializing_if = "Option::is_none")]
85 pub next_cursor: Option<String>,
86 #[serde(skip_serializing_if = "Option::is_none")]
88 pub total: Option<u64>,
89}
90
91pub type RecordCallback = Arc<dyn Fn(LogRecord) + Send + Sync>;
93
94pub type RecordPredicate = Arc<dyn Fn(&LogRecord) -> bool + Send + Sync>;
96
97#[derive(Debug, Clone, PartialEq, Eq)]
99pub struct TailOptions {
100 pub interval: Duration,
102 pub start: Option<SystemTime>,
104}
105
106impl Default for TailOptions {
107 fn default() -> Self {
108 TailOptions {
109 interval: TailOptions::DEFAULT_INTERVAL,
110 start: None,
111 }
112 }
113}
114
115impl TailOptions {
116 pub const DEFAULT_INTERVAL: Duration = Duration::from_secs(1);
118
119 pub fn interval_or(&self, service_default: Duration) -> Duration {
123 if self.interval.is_zero() || self.interval == Self::DEFAULT_INTERVAL {
124 service_default
125 } else {
126 self.interval
127 }
128 }
129}
130
131#[derive(Clone)]
133pub struct TailHandle {
134 stopped: Arc<AtomicBool>,
135 notify: Arc<Notify>,
136}
137
138impl fmt::Debug for TailHandle {
139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 f.debug_struct("TailHandle")
141 .field("stopped", &self.is_stopped())
142 .finish()
143 }
144}
145
146impl Default for TailHandle {
147 fn default() -> Self {
148 Self::new()
149 }
150}
151
152impl TailHandle {
153 pub fn new() -> Self {
155 TailHandle {
156 stopped: Arc::new(AtomicBool::new(false)),
157 notify: Arc::new(Notify::new()),
158 }
159 }
160
161 pub fn stop(&self) {
163 self.stopped.store(true, Ordering::SeqCst);
164 self.notify.notify_waiters();
165 }
166
167 pub fn is_stopped(&self) -> bool {
169 self.stopped.load(Ordering::SeqCst)
170 }
171
172 pub async fn stopped(&self) {
174 loop {
175 let notified = self.notify.notified();
176 tokio::pin!(notified);
177 notified.as_mut().enable();
178 if self.is_stopped() {
179 return;
180 }
181 notified.await;
182 }
183 }
184}
185
186#[derive(Clone)]
188pub struct WaitOptions {
189 pub timeout: Duration,
191 pub interval: Duration,
193 pub predicate: Option<RecordPredicate>,
195}
196
197impl Default for WaitOptions {
198 fn default() -> Self {
199 WaitOptions {
200 timeout: Duration::from_secs(30),
201 interval: Duration::from_secs(1),
202 predicate: None,
203 }
204 }
205}
206
207impl fmt::Debug for WaitOptions {
208 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209 f.debug_struct("WaitOptions")
210 .field("timeout", &self.timeout)
211 .field("interval", &self.interval)
212 .field("predicate", &self.predicate.is_some())
213 .finish()
214 }
215}
216
217impl WaitOptions {
218 pub fn timeout(mut self, d: Duration) -> Self {
220 self.timeout = d;
221 self
222 }
223 pub fn interval(mut self, d: Duration) -> Self {
225 self.interval = d;
226 self
227 }
228 pub fn predicate(mut self, f: impl Fn(&LogRecord) -> bool + Send + Sync + 'static) -> Self {
230 self.predicate = Some(Arc::new(f));
231 self
232 }
233}
234
235#[async_trait]
237pub trait LogQueryClient: Send + Sync {
238 async fn search(&self, query: &str, opts: SearchOptions) -> Result<LogSearchResult>;
240
241 async fn tail(
243 &self,
244 query: &str,
245 on_record: RecordCallback,
246 opts: TailOptions,
247 ) -> Result<TailHandle> {
248 let _ = (query, on_record, opts);
249 Err(Error::unsupported(
250 "logs.tail: not supported by this service",
251 ))
252 }
253
254 async fn list_streams(&self) -> Result<Vec<String>> {
256 Err(Error::unsupported(
257 "logs.listStreams: not supported by this service",
258 ))
259 }
260
261 async fn list_labels(&self) -> Result<Vec<String>> {
263 Err(Error::unsupported(
264 "logs.listLabels: not supported by this service",
265 ))
266 }
267
268 async fn wait_for_log(&self, query: &str, opts: WaitOptions) -> Result<LogRecord> {
270 wait_for_log(self, query, opts).await
271 }
272}
273
274pub async fn wait_for_log<C>(client: &C, query: &str, opts: WaitOptions) -> Result<LogRecord>
276where
277 C: LogQueryClient + ?Sized,
278{
279 let predicate = opts.predicate.clone();
280 let poll = PollOptions {
281 timeout: opts.timeout,
282 interval: opts.interval,
283 };
284 poll_until("logs.waitForLog", poll, || async {
285 let page = client.search(query, SearchOptions::default()).await?;
286 Ok(page
287 .records
288 .into_iter()
289 .find(|r| predicate.as_ref().is_none_or(|p| p(r))))
290 })
291 .await
292}
293
294pub fn poll_tail(
297 client: Arc<dyn LogQueryClient>,
298 query: &str,
299 on_record: RecordCallback,
300 opts: TailOptions,
301) -> TailHandle {
302 let handle = TailHandle::new();
303 let h = handle.clone();
304 let query = query.to_string();
305 tokio::spawn(async move {
306 let mut since = opts.start.unwrap_or_else(SystemTime::now);
307 let mut seen_at_since: Vec<String> = Vec::new();
308 while !h.is_stopped() {
309 let search = SearchOptions::default()
310 .range(Some(since), None)
311 .order(SortOrder::Asc)
312 .limit(1000);
313 let page = tokio::select! {
314 _ = h.stopped() => break,
315 page = client.search(&query, search) => page,
316 };
317 if let Ok(page) = page {
318 for r in page.records {
319 if h.is_stopped() {
320 break;
321 }
322 let key = r.to_json();
323 if r.timestamp < since || (r.timestamp == since && seen_at_since.contains(&key))
324 {
325 continue;
326 }
327 if r.timestamp > since {
328 since = r.timestamp;
329 seen_at_since.clear();
330 }
331 seen_at_since.push(key);
332 on_record(r);
333 }
334 }
335 tokio::select! {
336 _ = h.stopped() => break,
337 _ = tokio::time::sleep(opts.interval) => {}
338 }
339 }
340 });
341 handle
342}