Skip to main content

drain_flow/drains/
api.rs

1// Copyright Nicholas Harring. All rights reserved.
2//
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the Server Side Public License, version 1, as published by MongoDB, Inc.
5// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
6// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7// See the Server Side Public License for more details. You should have received a copy of the
8// Server Side Public License along with this program.
9// If not, see <http://www.mongodb.com/licensing/server-side-public-license>.
10
11use crate::log_group::LogGroup;
12use anyhow::Error;
13
14/// Defines the core interface for log processing drains.
15///
16/// The `Drain` trait abstracts the mechanism by which log lines are processed,
17/// clustered into `LogGroup`s, and subsequently retrieved. Implementations of
18/// this trait can offer various strategies for log analysis, such as simple
19/// in-memory storage or more complex, multi-stage processing pipelines.
20///
21/// This abstraction allows other parts of the system, like the `LogStore`,
22/// to operate on log data generically, without being coupled to a specific
23/// drain implementation.
24pub trait Drain {
25    /// Processes a single log line, potentially updating internal log group structures.
26    ///
27    /// # Arguments
28    ///
29    /// * `line` - A `String` representing the log line to be processed.
30    ///
31    /// # Returns
32    ///
33    /// * `Ok(true)` if processing the line resulted in the creation of a new `LogGroup`.
34    /// * `Ok(false)` if the line was successfully processed and added to an existing `LogGroup`.
35    /// * `Err(anyhow::Error)` if an error occurred during processing.
36    fn process_line(&mut self, line: String) -> Result<bool, Error>;
37
38    /// Retrieves all unique `LogGroup`s currently managed by the drain.
39    ///
40    /// This method provides a snapshot of the log groups at the time of calling.
41    /// The order of log groups in the returned vector is not guaranteed.
42    ///
43    /// # Returns
44    ///
45    /// A `Vec<LogGroup>` containing all log groups. If no log lines have been
46    /// processed or no groups have been formed, an empty vector is returned.
47    fn collect_log_groups(&self) -> Vec<LogGroup>;
48}