pub trait Loki: Fallback {
// Provided methods
fn work(&self, receiver: Receiver<LokiMessage>, data: Arc<LokiData>) { ... }
fn work_batch(
&self,
level: &str,
batch: &mut Vec<LokiMessage>,
data: &Arc<LokiData>,
) -> Result<Response, ServiceError> { ... }
}Expand description
The core logic provider for Loki interactions.
This trait defines how batches of logs are collected, formatted, and pushed. By implementing this trait, you can customize the batching strategy or adding custom logic (like GZIP compression) before sending.
§Required Hierarchy
Must implement LokiFallback to handle delivery failures.
Provided Methods§
Sourcefn work(&self, receiver: Receiver<LokiMessage>, data: Arc<LokiData>)
fn work(&self, receiver: Receiver<LokiMessage>, data: Arc<LokiData>)
Orchestrates the background worker loop.
This method is the entry point for the worker thread. It implements a double-buffer/drain strategy:
- It blocks on
receiver.recv()until at least one message is available, ensuring zero CPU usage during idle periods. - Once a message arrives, it performs a non-blocking
try_recvloop to “drain” the channel and form a batch. - It groups messages by their log level into a
BTreeMapto maintain consistent stream ordering before callingwork_batch.
This design assumes that network I/O is the bottleneck, allowing the in-memory collection of logs to be extremely fast.
Sourcefn work_batch(
&self,
level: &str,
batch: &mut Vec<LokiMessage>,
data: &Arc<LokiData>,
) -> Result<Response, ServiceError>
fn work_batch( &self, level: &str, batch: &mut Vec<LokiMessage>, data: &Arc<LokiData>, ) -> Result<Response, ServiceError>
Handles the transformation and transmission of a specific log batch.
This method performs the following steps:
- Serialization: Converts the batch of
LokiMessageinto a JSON payload compatible with the Loki Push API. - Transmission: Sends the payload via a POST request.
- Retry Logic: If the request fails or returns a non-success status,
it enters a retry loop up to the
max_retrieslimit defined inLokiConfig. - Safety Net: If all retries fail, it triggers the
LokiFallbackmechanism for every message in the batch. - Cleanup: Clears the batch vector to prepare it for the next iteration, reusing the allocated memory capacity.
§Returns
Returns the last [Response] received from the server, or a ServiceError
if the transmission was impossible.