opentelemetry_api/logs/
mod.rs

1//! # OpenTelemetry Logs API
2
3use crate::ExportError;
4use futures_channel::{mpsc::TrySendError, oneshot::Canceled};
5use std::time::Duration;
6use thiserror::Error;
7
8mod logger;
9mod noop;
10mod record;
11
12pub use logger::{Logger, LoggerProvider};
13pub use noop::NoopLoggerProvider;
14pub use record::{AnyValue, LogRecord, LogRecordBuilder, Severity, TraceContext};
15
16/// Describe the result of operations in log SDK.
17pub type LogResult<T> = Result<T, LogError>;
18
19#[derive(Error, Debug)]
20#[non_exhaustive]
21/// Errors returned by the log SDK.
22pub enum LogError {
23    /// Export failed with the error returned by the exporter.
24    #[error("Exporter {} encountered the following errors: {0}", .0.exporter_name())]
25    ExportFailed(Box<dyn ExportError>),
26
27    /// Export failed to finish after certain period and processor stopped the export.
28    #[error("Exporter timed out after {} seconds", .0.as_secs())]
29    ExportTimedOut(Duration),
30
31    /// Other errors propagated from log SDK that weren't covered above.
32    #[error(transparent)]
33    Other(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
34}
35
36impl<T> From<T> for LogError
37where
38    T: ExportError,
39{
40    fn from(err: T) -> Self {
41        LogError::ExportFailed(Box::new(err))
42    }
43}
44
45impl<T> From<TrySendError<T>> for LogError {
46    fn from(err: TrySendError<T>) -> Self {
47        LogError::Other(Box::new(err.into_send_error()))
48    }
49}
50
51impl From<Canceled> for LogError {
52    fn from(err: Canceled) -> Self {
53        LogError::Other(Box::new(err))
54    }
55}
56
57impl From<String> for LogError {
58    fn from(err_msg: String) -> Self {
59        LogError::Other(Box::new(Custom(err_msg)))
60    }
61}
62
63impl From<&'static str> for LogError {
64    fn from(err_msg: &'static str) -> Self {
65        LogError::Other(Box::new(Custom(err_msg.into())))
66    }
67}
68
69/// Wrap type for string
70#[derive(Error, Debug)]
71#[error("{0}")]
72struct Custom(String);