grow-graylog 0.2.0

Rust SDK for Graylog Open by Grow Codes
Documentation
// Generated by sdk-kit 0.3.0 — do not edit; change sdk-kit and run "sdk-kit sync"
//! The exporter interface a service package implements.

use async_trait::async_trait;

use super::LogRecord;
use crate::core::{Error, Result};

/// Outcome of one export call (SPEC §4 `ExportResult`).
#[derive(Debug, Default)]
pub struct ExportResult {
    /// Records the service accepted.
    pub accepted: usize,
    /// Records the service rejected (partial success).
    pub rejected: usize,
    /// Error describing the rejection, when any.
    pub error: Option<Error>,
}

impl ExportResult {
    /// Every record accepted.
    pub fn accepted(n: usize) -> Self {
        ExportResult {
            accepted: n,
            ..Default::default()
        }
    }

    /// Partial success: `accepted` taken, `rejected` refused with `error`.
    pub fn partial(accepted: usize, rejected: usize, error: Option<Error>) -> Self {
        ExportResult {
            accepted,
            rejected,
            error,
        }
    }
}

/// Per-call export options.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ExportOptions {
    /// Whether the payload should be gzip-compressed (`BatchOptions::gzip`).
    pub gzip: bool,
}

impl ExportOptions {
    /// Options with the given gzip flag.
    pub fn new(gzip: bool) -> Self {
        ExportOptions { gzip }
    }
}

impl Default for ExportOptions {
    fn default() -> Self {
        ExportOptions { gzip: true }
    }
}

/// Sends batches of records to a service (SPEC §4 `LogExporter`).
///
/// `export` is never called concurrently by [`BatchProcessor`](super::BatchProcessor).
/// Return `Err` when the whole batch failed (retryable errors are retried by the processor) and
/// `Ok` with `rejected > 0` for partial success. Cancellation is by dropping the future
/// (the processor enforces `exportTimeout` that way).
#[async_trait]
pub trait LogExporter: Send + Sync {
    /// Exports one batch.
    async fn export(&self, records: &[LogRecord], opts: ExportOptions) -> Result<ExportResult>;

    /// Releases resources; called once by the processor's `shutdown`.
    async fn shutdown(&self) -> Result<()> {
        Ok(())
    }
}