Skip to content

feat: add shutdown with timeout for log exporter #2909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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: 2 additions & 1 deletion opentelemetry-otlp/src/exporter/http/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use opentelemetry::otel_debug;
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
use opentelemetry_sdk::logs::{LogBatch, LogExporter};
use std::time;

impl LogExporter for OtlpHttpClient {
async fn export(&self, batch: LogBatch<'_>) -> OTelSdkResult {
Expand Down Expand Up @@ -46,7 +47,7 @@
Ok(())
}

fn shutdown(&self) -> OTelSdkResult {
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {

Check warning on line 50 in opentelemetry-otlp/src/exporter/http/logs.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/http/logs.rs#L50

Added line #L50 was not covered by tests
let mut client_guard = self.client.lock().map_err(|e| {
OTelSdkError::InternalFailure(format!("Failed to acquire client lock: {}", e))
})?;
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry-otlp/src/exporter/tonic/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
};
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
use opentelemetry_sdk::logs::{LogBatch, LogExporter};
use std::time;
use tokio::sync::Mutex;
use tonic::{codegen::CompressionEncoding, service::Interceptor, transport::Channel, Request};

Expand Down Expand Up @@ -84,7 +85,7 @@
Ok(())
}

fn shutdown(&self) -> OTelSdkResult {
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {

Check warning on line 88 in opentelemetry-otlp/src/exporter/tonic/logs.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/tonic/logs.rs#L88

Added line #L88 was not covered by tests
// TODO: Implement actual shutdown
// Due to the use of tokio::sync::Mutex to guard
// the inner client, we need to await the call to lock the mutex
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-otlp/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#[cfg(feature = "grpc-tonic")]
use opentelemetry::otel_debug;
use std::fmt::Debug;

use opentelemetry_sdk::{error::OTelSdkResult, logs::LogBatch};
use std::fmt::Debug;
use std::time;

use crate::{ExporterBuildError, HasExportConfig, NoExporterBuilderSet};

Expand Down Expand Up @@ -157,7 +157,7 @@
}
}

fn shutdown(&self) -> OTelSdkResult {
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {

Check warning on line 160 in opentelemetry-otlp/src/logs.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/logs.rs#L160

Added line #L160 was not covered by tests
match &self.client {
#[cfg(feature = "grpc-tonic")]
SupportedTransportClient::Tonic(client) => client.shutdown(),
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ also modified to suppress telemetry before invoking exporters.
- Fixed the overflow attribute to correctly use the boolean value `true`
instead of the string `"true"`.
[#2878](https://github.com/open-telemetry/opentelemetry-rust/issues/2878)
- The `shutdown_with_timeout` method is added to LogExporter trait.
- *Breaking* `MetricError`, `MetricResult` no longer public (except when
`spec_unstable_metrics_views` feature flag is enabled). `OTelSdkResult` should
be used instead, wherever applicable. [#2906](https://github.com/open-telemetry/opentelemetry-rust/pull/2906)
Expand Down
4 changes: 0 additions & 4 deletions opentelemetry-sdk/benches/log_enabled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ impl LogExporter for NoopExporter {
Ok(())
}

fn shutdown(&self) -> OTelSdkResult {
Ok(())
}

#[inline]
fn event_enabled(
&self,
Expand Down
8 changes: 6 additions & 2 deletions opentelemetry-sdk/src/logs/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::Resource;
use opentelemetry::logs::Severity;
use opentelemetry::InstrumentationScope;
use std::fmt::Debug;
use std::time;

/// A batch of log records to be exported by a `LogExporter`.
///
Expand Down Expand Up @@ -134,11 +135,14 @@ pub trait LogExporter: Send + Sync + Debug {
&self,
batch: LogBatch<'_>,
) -> impl std::future::Future<Output = OTelSdkResult> + Send;

/// Shuts down the exporter.
fn shutdown(&self) -> OTelSdkResult {
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
Ok(())
}
/// Shuts down the exporter with a default timeout.
fn shutdown(&self) -> OTelSdkResult {
self.shutdown_with_timeout(time::Duration::from_secs(5))
}
#[cfg(feature = "spec_unstable_logs_enabled")]
/// Check if logs are enabled.
fn event_enabled(&self, _level: Severity, _target: &str, _name: Option<&str>) -> bool {
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry-sdk/src/logs/in_memory_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use opentelemetry::InstrumentationScope;
use std::borrow::Cow;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
use std::time;

/// An in-memory logs exporter that stores logs data in memory..
///
Expand Down Expand Up @@ -205,7 +206,7 @@ impl LogExporter for InMemoryLogExporter {
Ok(())
}

fn shutdown(&self) -> OTelSdkResult {
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
self.shutdown_called
.store(true, std::sync::atomic::Ordering::Relaxed);
if self.should_reset_on_shutdown {
Expand Down
4 changes: 0 additions & 4 deletions opentelemetry-sdk/src/logs/log_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ pub(crate) mod tests {
Ok(())
}

fn shutdown(&self) -> OTelSdkResult {
Ok(())
}

fn set_resource(&mut self, resource: &Resource) {
self.resource
.lock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ mod tests {
Ok(())
}

fn shutdown(&self) -> OTelSdkResult {
Ok(())
}

fn set_resource(&mut self, resource: &Resource) {
self.resource
.lock()
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-sdk/src/logs/logger_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
use std::fmt::{Debug, Formatter};
use std::sync::atomic::AtomicU64;
use std::sync::Mutex;
use std::thread;
use std::{thread, time};

struct ShutdownTestLogProcessor {
is_shutdown: Arc<Mutex<bool>>,
Expand Down Expand Up @@ -364,7 +364,7 @@
*res = resource.clone();
}

fn shutdown(&self) -> OTelSdkResult {
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {

Check warning on line 367 in opentelemetry-sdk/src/logs/logger_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/logs/logger_provider.rs#L367

Added line #L367 was not covered by tests
Ok(())
}
}
Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-sdk/src/logs/simple_log_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
use opentelemetry::KeyValue;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time;
use std::time::Duration;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -195,6 +196,9 @@
}
Ok(())
}
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
Ok(())
}

Check warning on line 201 in opentelemetry-sdk/src/logs/simple_log_processor.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/logs/simple_log_processor.rs#L199-L201

Added lines #L199 - L201 were not covered by tests
}

#[test]
Expand Down Expand Up @@ -460,10 +464,6 @@
}

impl LogExporter for ReentrantLogExporter {
fn shutdown(&self) -> OTelSdkResult {
Ok(())
}

async fn export(&self, _batch: LogBatch<'_>) -> OTelSdkResult {
let logger = self.logger.lock().unwrap();
if let Some(logger) = logger.as_ref() {
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry-stdout/src/logs/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use opentelemetry_sdk::Resource;
use std::sync::atomic;
use std::sync::atomic::Ordering;
use std::time;

/// An OpenTelemetry exporter that writes Logs to stdout on export.
pub struct LogExporter {
Expand Down Expand Up @@ -57,7 +58,7 @@
}
}

fn shutdown(&self) -> OTelSdkResult {
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {

Check warning on line 61 in opentelemetry-stdout/src/logs/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/logs/exporter.rs#L61

Added line #L61 was not covered by tests
self.is_shutdown.store(true, atomic::Ordering::SeqCst);
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion stress/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use opentelemetry_sdk::logs::concurrent_log_processor::SimpleConcurrentLogProces
use opentelemetry_sdk::logs::SdkLoggerProvider;
use opentelemetry_sdk::logs::{LogBatch, LogExporter};
use opentelemetry_sdk::Resource;
use std::time;
use tracing::error;
use tracing_subscriber::prelude::*;

Expand Down Expand Up @@ -52,7 +53,7 @@ impl LogExporter for NoopExporter {
Ok(())
}

fn shutdown(&self) -> OTelSdkResult {
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
Ok(())
}

Expand Down