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"
//! Request hooks (SPEC §2.5). Logging uses the `tracing` crate instead of a logger object.

use std::collections::BTreeMap;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::time::Duration;

use super::error::Error;

/// Data passed to [`Hooks::on_request`] before every attempt.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct RequestInfo {
    /// HTTP method.
    pub method: String,
    /// Request URL with sensitive query values redacted.
    pub url: String,
    /// Request headers, sensitive values redacted, names lower-cased.
    pub headers: BTreeMap<String, String>,
    /// Attempt number, starting at 1.
    pub attempt: u32,
}

/// Data passed to [`Hooks::on_response`] for every response received.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ResponseInfo {
    /// HTTP method.
    pub method: String,
    /// Redacted URL.
    pub url: String,
    /// HTTP status.
    pub status: u16,
    /// Attempt duration.
    pub duration: Duration,
    /// Attempt number, starting at 1.
    pub attempt: u32,
    /// Vendor request id when present.
    pub request_id: Option<String>,
}

/// Data passed to [`Hooks::on_error`] for every failed attempt.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ErrorInfo {
    /// HTTP method.
    pub method: String,
    /// Redacted URL.
    pub url: String,
    /// The (redacted) error of this attempt.
    pub error: Error,
    /// Attempt number, starting at 1.
    pub attempt: u32,
    /// Whether the transport retries.
    pub will_retry: bool,
    /// Sleep before the next attempt when `will_retry`.
    pub retry_delay: Option<Duration>,
}

/// Observer for HTTP attempts. Every method has an empty default; panics are caught and ignored.
pub trait Hooks: Send + Sync {
    /// Called before each attempt.
    fn on_request(&self, _info: &RequestInfo) {}
    /// Called for each response (any status).
    fn on_response(&self, _info: &ResponseInfo) {}
    /// Called for each failed attempt.
    fn on_error(&self, _info: &ErrorInfo) {}
}

/// Runs `f` and swallows any panic (hooks must never break a request).
pub(crate) fn guarded(f: impl FnOnce()) {
    let _ = catch_unwind(AssertUnwindSafe(f));
}