-
Notifications
You must be signed in to change notification settings - Fork 557
fix: Modify SpanExporter trait to use immutable references for shutdown and force_flush methods #3066
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
base: main
Are you sure you want to change the base?
fix: Modify SpanExporter trait to use immutable references for shutdown and force_flush methods #3066
Changes from 5 commits
1e5da1e
d296a3c
d4cd0ee
71fc798
6dfee01
94817d7
85cdc91
99da387
e512fba
49c9bca
8fb8894
d65d0ec
35e236d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,7 @@ impl InMemoryLogExporterBuilder { | |
|
||
/// If set, the records will not be [`InMemoryLogExporter::reset`] on shutdown. | ||
#[cfg(test)] | ||
#[allow(dead_code)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're correct - the |
||
pub(crate) fn keep_records_on_shutdown(self) -> Self { | ||
Self { | ||
reset_on_shutdown: false, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -607,8 +607,8 @@ mod tests { | |
tracer.in_span("test-span", |cx| { | ||
let ambient_ctxt = cx.span().span_context().clone(); | ||
let explicit_ctxt = TraceContext { | ||
trace_id: TraceId::from_u128(13), | ||
span_id: SpanId::from_u64(14), | ||
trace_id: TraceId::from_bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot This change seems irrelevant to the PR - can it removed if so? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right - those changes to the test code were unrelated to the SpanExporter trait modifications. I've reverted them back to use |
||
span_id: SpanId::from_bytes([0, 0, 0, 0, 0, 0, 0, 14]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://gh.io/copilot-coding-agent-docs This change seems irrelevant to the PR - can it removed if so? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right - those changes to the test code were unrelated to the SpanExporter trait modifications. I've reverted them back to use |
||
trace_flags: None, | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,7 +159,7 @@ | |
} | ||
|
||
fn shutdown_with_timeout(&self, timeout: Duration) -> OTelSdkResult { | ||
if let Ok(mut exporter) = self.exporter.lock() { | ||
if let Ok(exporter) = self.exporter.lock() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot if exporter.shutdown does not require mutable ref, then is there any reason for processor to wrap the exporter inside mutex? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're absolutely right! Since the exporter methods now use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Excellent! We can do similar change to BatchSpanProcessor as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! I've optimized BatchSpanProcessor in commit 85cdc91. Since the SpanExporter methods now use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
BTW, each greeting like "Excellent" adds few cents to monthly LLM bill 😆 |
||
exporter.shutdown_with_timeout(timeout) | ||
} else { | ||
Err(OTelSdkError::InternalFailure( | ||
|
@@ -1089,7 +1089,7 @@ | |
Ok(()) | ||
} | ||
|
||
fn shutdown(&mut self) -> OTelSdkResult { | ||
fn shutdown(&self) -> OTelSdkResult { | ||
Ok(()) | ||
} | ||
fn set_resource(&mut self, resource: &Resource) { | ||
|
@@ -1373,4 +1373,68 @@ | |
let exported_spans = exporter_shared.lock().unwrap(); | ||
assert_eq!(exported_spans.len(), 10); | ||
} | ||
|
||
#[test] | ||
fn test_span_exporter_immutable_reference() { | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use crate::error::OTelSdkError; | ||
|
||
// Simple test exporter that demonstrates the &self pattern | ||
#[derive(Debug)] | ||
struct TestExporter { | ||
is_shutdown: AtomicBool, | ||
} | ||
|
||
impl TestExporter { | ||
fn new() -> Self { | ||
Self { | ||
is_shutdown: AtomicBool::new(false), | ||
} | ||
} | ||
|
||
fn is_shutdown(&self) -> bool { | ||
self.is_shutdown.load(Ordering::Relaxed) | ||
} | ||
} | ||
|
||
impl SpanExporter for TestExporter { | ||
async fn export(&self, _batch: Vec<SpanData>) -> OTelSdkResult { | ||
if self.is_shutdown() { | ||
return Err(OTelSdkError::AlreadyShutdown); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn shutdown(&self) -> OTelSdkResult { | ||
self.is_shutdown.store(true, Ordering::Relaxed); | ||
Ok(()) | ||
} | ||
|
||
fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult { | ||
self.shutdown() | ||
} | ||
|
||
fn force_flush(&self) -> OTelSdkResult { | ||
Ok(()) | ||
} | ||
} | ||
|
||
let exporter = TestExporter::new(); | ||
|
||
// These methods now work with &self | ||
assert!(!exporter.is_shutdown()); | ||
|
||
let result = exporter.shutdown(); | ||
assert!(result.is_ok()); | ||
|
||
assert!(exporter.is_shutdown()); | ||
|
||
// Test that export fails after shutdown | ||
let export_result = futures_executor::block_on(exporter.export(vec![])); | ||
assert!(export_result.is_err()); | ||
|
||
// Test force_flush | ||
let flush_result = exporter.force_flush(); | ||
assert!(flush_result.is_ok()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot This looks incorrect. The previous implementation showed the correct thing. need to continue to do the same even though we dont need mutable ref anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right! I've restored the proper shutdown semantics in commit d65d0ec. The TonicTracesClient now uses
Mutex<Option<ClientInner>>
to properly track shutdown state with&self
, preserving the original behavior where the first call to shutdown() returns Ok(()) and subsequent calls return Err(AlreadyShutdown).