Skip to main content

grow_graylog/
logquery.rs

1//! `LogQueryClient` of the logging category on top of Graylog's Views search API.
2
3use std::collections::HashMap;
4use std::time::{Duration, SystemTime};
5
6use async_trait::async_trait;
7
8use crate::client::GraylogClient;
9use crate::core::{Error, Result};
10use crate::logging::{
11    LogQueryClient, LogRecord, LogSearchResult, RecordCallback, SearchOptions, SortOrder,
12    TailHandle, TailOptions, WaitOptions,
13};
14use crate::search::{
15    MessageSearchOptions, SearchSort, DEFAULT_MESSAGE_SORT_FIELD, MAX_SEARCH_LIMIT,
16};
17
18/// Poll interval of [`LogQuery`]'s `tail` when the options keep the template default (2 s).
19pub const DEFAULT_TAIL_INTERVAL: Duration = Duration::from_secs(2);
20/// Search window of [`LogQuery`]'s `search` when no start is given (1 h).
21pub const DEFAULT_LOG_QUERY_WINDOW: Duration = Duration::from_secs(3600);
22/// How far back each tail poll looks before the newest record seen, so records that become
23/// searchable late (ingest takes 1–2 s) are not missed; duplicates are removed by message id.
24const TAIL_OVERLAP: Duration = Duration::from_secs(10);
25
26/// Options of [`GraylogClient::log_query`]. The default is valid.
27#[derive(Debug, Clone, Default, PartialEq, Eq)]
28pub struct LogQueryOptions {
29    /// Limits every search to these stream ids (default: every readable stream).
30    pub streams: Vec<String>,
31    /// Search window when `SearchOptions::start` is unset: end minus this (default 1 h).
32    pub default_window: Option<Duration>,
33}
34
35/// The `LogQueryClient` of this SDK. Queries use Graylog's Lucene syntax (`marker:abc`,
36/// `"exact phrase"`, `level:<=3`, `trace_id:4bf9…`).
37#[derive(Debug, Clone)]
38pub struct LogQuery {
39    client: GraylogClient,
40    streams: Vec<String>,
41    window: Duration,
42}
43
44impl GraylogClient {
45    /// The `LogQueryClient` of this client.
46    pub fn log_query(&self, opts: LogQueryOptions) -> LogQuery {
47        LogQuery {
48            client: self.clone(),
49            streams: opts.streams,
50            window: opts
51                .default_window
52                .filter(|w| !w.is_zero())
53                .unwrap_or(DEFAULT_LOG_QUERY_WINDOW),
54        }
55    }
56}
57
58fn sorts(order: SortOrder) -> Vec<SearchSort> {
59    vec![
60        SearchSort {
61            field: DEFAULT_MESSAGE_SORT_FIELD.into(),
62            order,
63        },
64        SearchSort {
65            field: "gl2_message_id".into(),
66            order,
67        },
68    ]
69}
70
71#[async_trait]
72impl LogQueryClient for LogQuery {
73    /// Normalized records (see [`log_record_from_message`](crate::log_record_from_message))
74    /// matching `query` between `opts.start` and `opts.end` (defaults: end = now, start = end −
75    /// the default window), newest first by default, ties broken by `gl2_message_id`. The cursor
76    /// is the offset of the next page; `next_cursor` is set while more matches exist and `total`
77    /// is Graylog's match count.
78    async fn search(&self, query: &str, opts: SearchOptions) -> Result<LogSearchResult> {
79        let offset = match &opts.cursor {
80            Some(c) => c
81                .parse::<usize>()
82                .map_err(|_| Error::validation("logs.search: invalid cursor"))?,
83            None => 0,
84        };
85        let end = opts.end.unwrap_or_else(SystemTime::now);
86        let start = opts.start.unwrap_or_else(|| {
87            end.checked_sub(self.window)
88                .unwrap_or(SystemTime::UNIX_EPOCH)
89        });
90        let res = self
91            .client
92            .search(
93                query,
94                MessageSearchOptions {
95                    start: Some(start),
96                    end: Some(end),
97                    limit: Some(opts.limit.clamp(1, MAX_SEARCH_LIMIT)),
98                    offset,
99                    streams: self.streams.clone(),
100                    sort: sorts(opts.order),
101                    ..MessageSearchOptions::default()
102                },
103            )
104            .await?;
105        Ok(LogSearchResult {
106            records: res.records(),
107            next_cursor: res.next_offset.map(|n| n.to_string()),
108            total: Some(res.total),
109        })
110    }
111
112    /// Polls `search` every `opts.interval` (2 s when the template default of 1 s is kept;
113    /// Graylog has no streaming API) and calls `on_record` once per new matching record, oldest
114    /// first, starting at `opts.start` (default now). Each poll looks back 10 s before the
115    /// newest record seen and drops duplicates by message id, so late records are delivered
116    /// too. Search errors are logged and polling continues. The tail stops with the handle or
117    /// when the client is closed.
118    async fn tail(
119        &self,
120        query: &str,
121        on_record: RecordCallback,
122        opts: TailOptions,
123    ) -> Result<TailHandle> {
124        if self.client.is_closed() {
125            return Err(Error::cancelled("logs.tail: client is closed"));
126        }
127        let interval =
128            if opts.interval == TailOptions::default().interval || opts.interval.is_zero() {
129                DEFAULT_TAIL_INTERVAL
130            } else {
131                opts.interval
132            };
133        let handle = TailHandle::new();
134        let stopper = handle.clone();
135        self.client.on_close(move || {
136            Box::pin(async move {
137                stopper.stop();
138            })
139        });
140        let mut tailer = Tailer {
141            q: self.clone(),
142            query: query.to_owned(),
143            since: opts.start.unwrap_or_else(SystemTime::now),
144            newest: None,
145            seen: HashMap::new(),
146        };
147        let h = handle.clone();
148        tokio::spawn(async move {
149            while !h.is_stopped() {
150                tailer.poll(&h, &on_record).await;
151                tokio::select! {
152                    _ = h.stopped() => break,
153                    _ = tokio::time::sleep(interval) => {}
154                }
155            }
156        });
157        Ok(handle)
158    }
159
160    /// Titles of every stream (all pages, sorted).
161    async fn list_streams(&self) -> Result<Vec<String>> {
162        let mut out: Vec<String> = self
163            .client
164            .list_all_streams()
165            .await?
166            .into_iter()
167            .map(|s| s.title)
168            .collect();
169        out.sort();
170        Ok(out)
171    }
172
173    /// Every indexed field name (`GET /api/system/fields`), sorted.
174    async fn list_labels(&self) -> Result<Vec<String>> {
175        self.client.list_fields().await
176    }
177
178    /// Polls `search` until a record matches (see [`wait_for_log`](crate::logging::wait_for_log)).
179    async fn wait_for_log(&self, query: &str, opts: WaitOptions) -> Result<LogRecord> {
180        crate::logging::wait_for_log(self, query, opts).await
181    }
182}
183
184struct Tailer {
185    q: LogQuery,
186    query: String,
187    since: SystemTime,
188    newest: Option<SystemTime>,
189    seen: HashMap<String, SystemTime>,
190}
191
192impl Tailer {
193    async fn poll(&mut self, h: &TailHandle, on_record: &RecordCallback) {
194        let mut from = self.since;
195        if let Some(n) = self.newest.and_then(|n| n.checked_sub(TAIL_OVERLAP)) {
196            if n > from {
197                from = n;
198            }
199        }
200        let mut offset = 0;
201        loop {
202            let res = self
203                .q
204                .client
205                .search(
206                    &self.query,
207                    MessageSearchOptions {
208                        start: Some(from),
209                        end: Some(SystemTime::now() + Duration::from_secs(60)),
210                        limit: Some(1000),
211                        offset,
212                        streams: self.q.streams.clone(),
213                        sort: sorts(SortOrder::Asc),
214                        ..MessageSearchOptions::default()
215                    },
216                )
217                .await;
218            let res = match res {
219                Ok(r) => r,
220                Err(e) => {
221                    if !h.is_stopped() {
222                        ::tracing::warn!(target: "grow_graylog::tail", error = %e, "logs.tail: search failed");
223                    }
224                    return;
225                }
226            };
227            for m in &res.messages {
228                if h.is_stopped() {
229                    return;
230                }
231                if !m.id.is_empty() && self.seen.contains_key(&m.id) {
232                    continue;
233                }
234                let ts = m.timestamp.unwrap_or(self.since);
235                self.seen.insert(m.id.clone(), ts);
236                if self.newest.is_none_or(|n| ts > n) {
237                    self.newest = Some(ts);
238                }
239                let cb = on_record.clone();
240                let rec = m.record();
241                let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| cb(rec)));
242            }
243            offset += res.messages.len();
244            if res.messages.is_empty() || offset as u64 >= res.total {
245                break;
246            }
247        }
248        // Forget ids that can no longer appear in a window.
249        if let Some(cutoff) = self.newest.and_then(|n| n.checked_sub(TAIL_OVERLAP * 2)) {
250            self.seen.retain(|_, ts| *ts >= cutoff);
251        }
252    }
253}