Skip to main content

signal_kit/
lib.rs

1//! # signal-kit
2//!
3//! Opinionated OpenTelemetry initialisation for async Rust services.
4//!
5//! Most applications should use [`ObservabilityBuilder`] or the convenience
6//! functions [`init`] and [`try_init`].
7//!
8//! ```no_run
9//! use signal_kit::ObservabilityBuilder;
10//!
11//! # fn main() -> Result<(), signal_kit::OtelInitError> {
12//! let _guard = ObservabilityBuilder::new("my-service")
13//!     .with_attribute("version", env!("CARGO_PKG_VERSION"))
14//!     .init()?;
15//! # Ok(())
16//! # }
17//! ```
18//!
19//! Use [`try_init`] for production applications and tests. Use [`init`] for
20//! examples, prototypes, and binaries where panicking on startup failure is
21//! acceptable. The crate installs the global tracing subscriber and should
22//! usually be initialized once near the start of `main`.
23//!
24//! ## Environment Variables
25//!
26//! Compile-time build metadata is read with `option_env!`: `GIT_COMMIT`,
27//! `GIT_BRANCH`, `GIT_REPOSITORY_URL`, and `BUILD_NUMBER`.
28//!
29//! Runtime exporter variables follow OpenTelemetry conventions:
30//! `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`,
31//! `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT`, and
32//! `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT`. Signal-specific endpoints take
33//! precedence over the general endpoint.
34//!
35//! Crate-specific runtime variables are namespaced under `SIGNAL_KIT_`:
36//! `SIGNAL_KIT_STRUCTURED_LOGGING`, `SIGNAL_KIT_FILE_ENABLED`,
37//! `SIGNAL_KIT_FILE_PATH`, `SIGNAL_KIT_FILE_ROTATION`, and
38//! `SIGNAL_KIT_FILE_RETENTION_DAYS`.
39//!
40//! Resource attributes are resolved in this order: explicit builder
41//! attributes, `OTEL_RESOURCE_ATTRIBUTES`, crate convenience aliases such as
42//! `ENVIRONMENT`, `CLOUD_*`, and `KUBERNETES_*`, then compile-time metadata.
43#![forbid(unsafe_code)]
44
45use std::time::Duration;
46
47mod observability;
48pub use observability::{
49    InitSummary, InitWarning, MetricsInitCallback, MetricsInitContext, MetricsInitError,
50    ObservabilityBuilder, OtelGuard, OtelInitError, OtelShutdownError, OtelShutdownFailure,
51    SignalKind, init, try_init,
52};
53
54/// Metrics initialization and configuration.
55pub mod metrics;
56
57mod traces;
58pub use traces::otel_resource_attributes::build_resource;
59
60mod otel_runtime;
61mod transport;
62pub use transport::{OtlpTransport, resolve_otlp_transport};
63
64mod logs;
65
66pub mod file_logging;
67pub use file_logging::{FileLoggingConfig, FileLoggingError, RotationConfig, RotationStrategy};
68
69/// The interval at which metrics and traces are exported to the otlp collector.
70pub const OBSERVABILITY_EXPORT_INTERVAL: Duration = Duration::from_secs(15);
71
72/// Check if OTLP endpoint is configured for a specific signal.
73///
74/// According to OpenTelemetry specification, signal-specific endpoints take precedence
75/// over the general endpoint.
76///
77/// # Arguments
78/// * `signal` - The signal type ("traces", "metrics", "logs")
79///
80/// # Returns
81/// * `Some(endpoint)` if an endpoint is configured, `None` otherwise
82fn get_otlp_endpoint(signal: &str) -> Option<String> {
83    // First check for signal-specific endpoint
84    let signal_env = format!("OTEL_EXPORTER_OTLP_{}_ENDPOINT", signal.to_uppercase());
85    if let Ok(endpoint) = std::env::var(&signal_env)
86        && !endpoint.trim().is_empty()
87    {
88        return Some(endpoint);
89    }
90
91    // Fall back to general endpoint
92    if let Ok(endpoint) = std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT")
93        && !endpoint.trim().is_empty()
94    {
95        return Some(endpoint);
96    }
97
98    None
99}