Skip to main content

grow_better_stack_logs/logging/
exporter.rs

1// Generated by sdk-kit 0.3.0 — do not edit; change sdk-kit and run "sdk-kit sync"
2//! The exporter interface a service package implements.
3
4use async_trait::async_trait;
5
6use super::LogRecord;
7use crate::core::{Error, Result};
8
9/// Outcome of one export call (SPEC §4 `ExportResult`).
10#[derive(Debug, Default)]
11pub struct ExportResult {
12    /// Records the service accepted.
13    pub accepted: usize,
14    /// Records the service rejected (partial success).
15    pub rejected: usize,
16    /// Error describing the rejection, when any.
17    pub error: Option<Error>,
18}
19
20impl ExportResult {
21    /// Every record accepted.
22    pub fn accepted(n: usize) -> Self {
23        ExportResult {
24            accepted: n,
25            ..Default::default()
26        }
27    }
28
29    /// Partial success: `accepted` taken, `rejected` refused with `error`.
30    pub fn partial(accepted: usize, rejected: usize, error: Option<Error>) -> Self {
31        ExportResult {
32            accepted,
33            rejected,
34            error,
35        }
36    }
37}
38
39/// Per-call export options.
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct ExportOptions {
43    /// Whether the payload should be gzip-compressed (`BatchOptions::gzip`).
44    pub gzip: bool,
45}
46
47impl ExportOptions {
48    /// Options with the given gzip flag.
49    pub fn new(gzip: bool) -> Self {
50        ExportOptions { gzip }
51    }
52}
53
54impl Default for ExportOptions {
55    fn default() -> Self {
56        ExportOptions { gzip: true }
57    }
58}
59
60/// Sends batches of records to a service (SPEC §4 `LogExporter`).
61///
62/// `export` is never called concurrently by [`BatchProcessor`](super::BatchProcessor).
63/// Return `Err` when the whole batch failed (retryable errors are retried by the processor) and
64/// `Ok` with `rejected > 0` for partial success. Cancellation is by dropping the future
65/// (the processor enforces `exportTimeout` that way).
66#[async_trait]
67pub trait LogExporter: Send + Sync {
68    /// Exports one batch.
69    async fn export(&self, records: &[LogRecord], opts: ExportOptions) -> Result<ExportResult>;
70
71    /// Releases resources; called once by the processor's `shutdown`.
72    async fn shutdown(&self) -> Result<()> {
73        Ok(())
74    }
75}