Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,8 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml

# Cargo build artifacts
target/

# Cargo lock
Cargo.lock
4 changes: 3 additions & 1 deletion examples/sample-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async fn main() {
interval_secs: 1,
timeout: 5,
export_severity: Some(Severity::Error),
target_filters: None,
ca_cert_path: args.ca_cert_path,
bearer_token_provider_fn: Some(get_dummy_bearer_token),
}];
Expand All @@ -65,6 +66,7 @@ async fn main() {
};

let mut otel_component = Otel::new(config);
let shutdown_handle = otel_component.shutdown_handle();
// Start the otel running task
let otel_long_running_task = otel_component.run();
// initialize static metrics
Expand Down Expand Up @@ -95,7 +97,7 @@ async fn main() {
});

let _ = join!(instrumentation_task, otel_long_running_task);
otel_component.shutdown().await;
let _ = shutdown_handle.shutdown().await;
}

#[derive(Parser, Debug)]
Expand Down
100 changes: 87 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ pub struct Otel {
shutdown_rx: mpsc::Receiver<()>,
}

pub struct ShutdownHandle {
sender: mpsc::Sender<()>,
}

impl ShutdownHandle {
/// Send a shutdown signal
/// # Errors
/// * `mpsc::error::SendError` - If the shutdown signal could not be sent
pub async fn shutdown(&self) -> Result<(), mpsc::error::SendError<()>> {
self.sender.send(()).await
}
}

impl Otel {
#[must_use]
pub fn new(config: Config) -> Otel {
Expand Down Expand Up @@ -144,28 +157,41 @@ impl Otel {
return Err(OtelError::PrometheusServerStopped)
}
_ = self.shutdown_rx.recv() => {
// Graceful shutdown that flushes any pending metrics and logs to the exporter.
info!("shutting down otel component");

if let Err(metrics_error) = self.meter_provider.force_flush() {
warn!("encountered error while flushing metrics: {metrics_error:?}");
}
if let Err(metrics_error) = self.meter_provider.shutdown() {
warn!("encountered error while shutting down meter provider: {metrics_error:?}");
}

if let Some(logger_provider) = self.logger_provider.clone() {
logger_provider.force_flush();
let _ = logger_provider.shutdown();
}

}
}

Ok(())
}

/// Graceful shutdown that flushes any pending metrics and logs to the exporter.
pub async fn shutdown(&self) {
if let Err(metrics_error) = self.meter_provider.force_flush() {
warn!("ecountered error while flushing metrics: {metrics_error:?}");
}
if let Err(metrics_error) = self.meter_provider.shutdown() {
warn!("ecountered error while shutting down meter provider: {metrics_error:?}");
}

if let Some(logger_provider) = self.logger_provider.clone() {
logger_provider.force_flush();
let _ = logger_provider.shutdown();
/// Get a shutdown handle that can be used to trigger shutdown from other contexts.
#[must_use]
pub fn shutdown_handle(&self) -> ShutdownHandle {
ShutdownHandle {
sender: self.shutdown_tx.clone(),
}
}

let _ = self.shutdown_tx.send(()).await;
/// Convenience function to trigger shutdown from the Otel struct directly.
///
/// # Errors
/// * `mpsc::error::SendError` - If the shutdown signal could not be sent
pub async fn shutdown(&self) -> Result<(), mpsc::error::SendError<()>> {
self.shutdown_tx.send(()).await // Just sends signal, run() handles the rest
}
}

Expand Down Expand Up @@ -508,3 +534,51 @@ impl Interceptor for AuthIntercepter {
Ok(modified_request)
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
use tokio::time::timeout;

#[tokio::test]
async fn test_shutdown_approaches_both_work() {
// Test that both shutdown approaches trigger the same graceful shutdown behavior
{
// Arrange
let config = Config::default();
let mut otel = Otel::new(config);
let shutdown_handle = otel.shutdown_handle();

let run_task = tokio::spawn(async move { otel.run().await });

tokio::time::sleep(Duration::from_millis(50)).await;

// Act
shutdown_handle
.shutdown()
.await
.expect("Should be able to send shutdown signal");

// Assert
let result = timeout(Duration::from_secs(1), run_task).await;
assert!(
result.is_ok(),
"Run task should complete after direct shutdown"
);
assert!(result.unwrap().is_ok(), "Run task should exit cleanly");
}

{
// Arrange
let config = Config::default();
let otel = Otel::new(config);

// Act
let shutdown_result = otel.shutdown().await;

// Assert
assert!(shutdown_result.is_ok(), "Convenience shutdown should work");
}
}
}