Skip to content

Commit b7efe12

Browse files
committed
Added mutable reference parameter for SpanData in on_end trait function and replaced where necessary
1 parent 7d82c3e commit b7efe12

File tree

8 files changed

+83
-69
lines changed

8 files changed

+83
-69
lines changed

opentelemetry-sdk/benches/batch_span_processor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ fn criterion_benchmark(c: &mut Criterion) {
6161
let span_processor = shared_span_processor.clone();
6262
let spans = get_span_data();
6363
handles.push(tokio::spawn(async move {
64-
for span in spans {
65-
span_processor.on_end(span);
64+
for mut span in spans {
65+
span_processor.on_end(&mut span);
6666
tokio::task::yield_now().await;
6767
}
6868
}));

opentelemetry-sdk/src/trace/export.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,30 @@ pub trait SpanExporter: Send + Sync + Debug {
7373
/// `SpanData` contains all the information collected by a `Span` and can be used
7474
/// by exporters as a standard input.
7575
#[derive(Clone, Debug, PartialEq)]
76-
pub struct SpanData<'a> {
76+
pub struct SpanData {
7777
/// Exportable `SpanContext`
7878
pub span_context: SpanContext,
7979
/// Span parent id
8080
pub parent_span_id: SpanId,
8181
/// Span kind
82-
pub span_kind: &'a SpanKind,
82+
pub span_kind: SpanKind,
8383
/// Span name
84-
pub name: &'a Cow<'static, str>,
84+
pub name: Cow<'static, str>,
8585
/// Span start time
8686
pub start_time: SystemTime,
8787
/// Span end time
8888
pub end_time: SystemTime,
8989
/// Span attributes
90-
pub attributes: &'a Vec<KeyValue>,
90+
pub attributes: Vec<KeyValue>,
9191
/// The number of attributes that were above the configured limit, and thus
9292
/// dropped.
9393
pub dropped_attributes_count: u32,
9494
/// Span events
95-
pub events: &'a crate::trace::SpanEvents,
95+
pub events: crate::trace::SpanEvents,
9696
/// Span Links
97-
pub links: &'a crate::trace::SpanLinks,
97+
pub links: crate::trace::SpanLinks,
9898
/// Span status
99-
pub status: &'a Status,
99+
pub status: Status,
100100
/// Instrumentation scope that produced this span
101101
pub instrumentation_scope: InstrumentationScope,
102102
}

opentelemetry-sdk/src/trace/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ mod tests {
136136
}
137137
}
138138

139-
fn on_end(&self, _span: SpanData) {}
139+
fn on_end(&self, _span: &mut SpanData) {}
140140

141141
fn force_flush(&self) -> crate::error::OTelSdkResult {
142142
Ok(())

opentelemetry-sdk/src/trace/provider.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ mod tests {
516516
.fetch_add(1, Ordering::SeqCst);
517517
}
518518

519-
fn on_end(&self, _span: SpanData) {
519+
fn on_end(&self, _span: &mut SpanData) {
520520
// ignore
521521
}
522522

@@ -779,7 +779,7 @@ mod tests {
779779
// No operation needed for this processor
780780
}
781781

782-
fn on_end(&self, _span: SpanData) {
782+
fn on_end(&self, _span: &mut SpanData) {
783783
// No operation needed for this processor
784784
}
785785

opentelemetry-sdk/src/trace/span.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ impl Span {
7474
/// Convert information in this span into `exporter::trace::SpanData`.
7575
/// This function copies all data from the current span, which will create a
7676
/// overhead.
77-
pub fn exported_data<'a>(&'a mut self) -> Option<crate::trace::SpanData<'a>> {
77+
pub fn exported_data(&self) -> Option<crate::trace::SpanData> {
7878
let (span_context, tracer) = (self.span_context.clone(), &self.tracer);
7979

8080
self.data
81-
.as_mut()
82-
.map(|data| build_export_data(data, span_context, tracer))
81+
.as_ref()
82+
.map(|data| build_export_data(data.clone(), span_context, tracer))
8383
}
8484
}
8585

@@ -199,8 +199,23 @@ impl opentelemetry::trace::Span for Span {
199199
impl Span {
200200
fn ensure_ended_and_exported(&mut self, timestamp: Option<SystemTime>) {
201201
// skip if data has already been exported
202-
let data = &mut match self.data.take() {
203-
Some(data) => data,
202+
let mut data = match self.data.take() {
203+
Some(data) => {
204+
crate::trace::SpanData {
205+
span_context: self.span_context.clone(),
206+
parent_span_id: data.parent_span_id,
207+
span_kind: data.span_kind,
208+
name: data.name,
209+
start_time: data.start_time,
210+
end_time: data.end_time,
211+
attributes: data.attributes,
212+
dropped_attributes_count: data.dropped_attributes_count,
213+
events: data.events,
214+
links: data.links,
215+
status: data.status,
216+
instrumentation_scope: self.tracer.instrumentation_scope().clone(),
217+
}
218+
},
204219
None => return,
205220
};
206221

@@ -220,11 +235,11 @@ impl Span {
220235
match provider.span_processors() {
221236
[] => {}
222237
[processor] => {
223-
processor.on_end();
238+
processor.on_end(&mut data)
224239
}
225240
processors => {
226241
for processor in processors {
227-
processor.on_end();
242+
processor.on_end(&mut data);
228243
}
229244
}
230245
}
@@ -238,23 +253,23 @@ impl Drop for Span {
238253
}
239254
}
240255

241-
fn build_export_data<'a>(
242-
data: &'a mut SpanData,
256+
fn build_export_data(
257+
data: SpanData,
243258
span_context: SpanContext,
244-
tracer: &'a crate::trace::SdkTracer,
245-
) -> crate::trace::SpanData<'a> {
259+
tracer: &crate::trace::SdkTracer,
260+
) -> crate::trace::SpanData {
246261
crate::trace::SpanData {
247262
span_context,
248263
parent_span_id: data.parent_span_id,
249-
span_kind: &data.span_kind,
250-
name: &data.name,
264+
span_kind: data.span_kind,
265+
name: data.name,
251266
start_time: data.start_time,
252267
end_time: data.end_time,
253-
attributes: &data.attributes,
268+
attributes: data.attributes,
254269
dropped_attributes_count: data.dropped_attributes_count,
255-
events: &data.events,
256-
links: &data.links,
257-
status: &data.status,
270+
events: data.events,
271+
links: data.links,
272+
status: data.status,
258273
instrumentation_scope: tracer.instrumentation_scope().clone(),
259274
}
260275
}
@@ -713,7 +728,7 @@ mod tests {
713728
let res = provider.shutdown();
714729
println!("{:?}", res);
715730
assert!(res.is_ok());
716-
let mut dropped_span = tracer.start("span_with_dropped_provider");
731+
let dropped_span = tracer.start("span_with_dropped_provider");
717732
// return none if the provider has already been dropped
718733
assert!(dropped_span.exported_data().is_none());
719734
}

opentelemetry-sdk/src/trace/span_processor.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ pub trait SpanProcessor: Send + Sync + std::fmt::Debug {
8282
/// `on_end` is called after a `Span` is ended (i.e., the end timestamp is
8383
/// already set). This method is called synchronously within the `Span::end`
8484
/// API, therefore it should not block or throw an exception.
85-
/// TODO - This method should take reference to `SpanData`
86-
fn on_end(&self, span: SpanData);
85+
fn on_end(&self, span: &mut SpanData);
8786
/// Force the spans lying in the cache to be exported.
8887
fn force_flush(&self) -> OTelSdkResult;
8988
/// Shuts down the processor. Called when SDK is shut down. This is an
@@ -129,7 +128,7 @@ impl<T: SpanExporter> SpanProcessor for SimpleSpanProcessor<T> {
129128
// Ignored
130129
}
131130

132-
fn on_end(&self, span: SpanData) {
131+
fn on_end(&self, span: &mut SpanData) {
133132
if !span.span_context.is_sampled() {
134133
return;
135134
}
@@ -138,7 +137,7 @@ impl<T: SpanExporter> SpanProcessor for SimpleSpanProcessor<T> {
138137
.exporter
139138
.lock()
140139
.map_err(|_| OTelSdkError::InternalFailure("SimpleSpanProcessor mutex poison".into()))
141-
.and_then(|exporter| futures_executor::block_on(exporter.export(vec![span])));
140+
.and_then(|exporter| futures_executor::block_on(exporter.export(vec![span.clone()])));
142141

143142
if let Err(err) = result {
144143
// TODO: check error type, and log `error` only if the error is user-actionable, else log `debug`
@@ -460,7 +459,7 @@ impl BatchSpanProcessor {
460459
E: SpanExporter + Send + Sync + 'static,
461460
{
462461
// Get upto `max_export_batch_size` amount of spans from the channel and push them to the span vec
463-
while let Ok(span) = spans_receiver.try_recv() {
462+
while let Ok(span) = spans_receiver.try_recv() {
464463
spans.push(span);
465464
if spans.len() == config.max_export_batch_size {
466465
break;
@@ -512,7 +511,7 @@ impl SpanProcessor for BatchSpanProcessor {
512511
}
513512

514513
/// Handles span end.
515-
fn on_end(&self, span: SpanData) {
514+
fn on_end(&self, span: &mut SpanData) {
516515
if self.is_shutdown.load(Ordering::Relaxed) {
517516
// this is a warning, as the user is trying to emit after the processor has been shutdown
518517
otel_warn!(
@@ -521,7 +520,7 @@ impl SpanProcessor for BatchSpanProcessor {
521520
);
522521
return;
523522
}
524-
let result = self.span_sender.try_send(span);
523+
let result = self.span_sender.try_send(span.clone());
525524

526525
if result.is_err() {
527526
// Increment dropped span count. The first time we have to drop a span,
@@ -875,8 +874,8 @@ mod tests {
875874
fn simple_span_processor_on_end_calls_export() {
876875
let exporter = InMemorySpanExporterBuilder::new().build();
877876
let processor = SimpleSpanProcessor::new(exporter.clone());
878-
let span_data = new_test_export_span_data();
879-
processor.on_end(span_data.clone());
877+
let mut span_data = new_test_export_span_data();
878+
processor.on_end(&mut span_data);
880879
assert_eq!(exporter.get_finished_spans().unwrap()[0], span_data);
881880
let _result = processor.shutdown();
882881
}
@@ -885,7 +884,7 @@ mod tests {
885884
fn simple_span_processor_on_end_skips_export_if_not_sampled() {
886885
let exporter = InMemorySpanExporterBuilder::new().build();
887886
let processor = SimpleSpanProcessor::new(exporter.clone());
888-
let unsampled = SpanData {
887+
let mut unsampled = SpanData {
889888
span_context: SpanContext::empty_context(),
890889
parent_span_id: SpanId::INVALID,
891890
span_kind: SpanKind::Internal,
@@ -899,16 +898,16 @@ mod tests {
899898
status: Status::Unset,
900899
instrumentation_scope: Default::default(),
901900
};
902-
processor.on_end(unsampled);
901+
processor.on_end(&mut unsampled);
903902
assert!(exporter.get_finished_spans().unwrap().is_empty());
904903
}
905904

906905
#[test]
907906
fn simple_span_processor_shutdown_calls_shutdown() {
908907
let exporter = InMemorySpanExporterBuilder::new().build();
909908
let processor = SimpleSpanProcessor::new(exporter.clone());
910-
let span_data = new_test_export_span_data();
911-
processor.on_end(span_data.clone());
909+
let mut span_data = new_test_export_span_data();
910+
processor.on_end(&mut span_data);
912911
assert!(!exporter.get_finished_spans().unwrap().is_empty());
913912
let _result = processor.shutdown();
914913
// Assume shutdown is called by ensuring spans are empty in the exporter
@@ -1109,8 +1108,8 @@ mod tests {
11091108
.build();
11101109
let processor = BatchSpanProcessor::new(exporter, config);
11111110

1112-
let test_span = create_test_span("test_span");
1113-
processor.on_end(test_span.clone());
1111+
let mut test_span = create_test_span("test_span");
1112+
processor.on_end(&mut test_span);
11141113

11151114
// Wait for flush interval to ensure the span is processed
11161115
std::thread::sleep(Duration::from_secs(6));
@@ -1132,8 +1131,8 @@ mod tests {
11321131
let processor = BatchSpanProcessor::new(exporter, config);
11331132

11341133
// Create a test span and send it to the processor
1135-
let test_span = create_test_span("force_flush_span");
1136-
processor.on_end(test_span.clone());
1134+
let mut test_span = create_test_span("force_flush_span");
1135+
processor.on_end(&mut test_span);
11371136

11381137
// Call force_flush to immediately export the spans
11391138
let flush_result = processor.force_flush();
@@ -1161,8 +1160,8 @@ mod tests {
11611160
let processor = BatchSpanProcessor::new(exporter, config);
11621161

11631162
// Create a test span and send it to the processor
1164-
let test_span = create_test_span("shutdown_span");
1165-
processor.on_end(test_span.clone());
1163+
let mut test_span = create_test_span("shutdown_span");
1164+
processor.on_end(&mut test_span);
11661165

11671166
// Call shutdown to flush and export all pending spans
11681167
let shutdown_result = processor.shutdown();
@@ -1196,13 +1195,13 @@ mod tests {
11961195
let processor = BatchSpanProcessor::new(exporter, config);
11971196

11981197
// Create test spans and send them to the processor
1199-
let span1 = create_test_span("span1");
1200-
let span2 = create_test_span("span2");
1201-
let span3 = create_test_span("span3"); // This span should be dropped
1198+
let mut span1 = create_test_span("span1");
1199+
let mut span2 = create_test_span("span2");
1200+
let mut span3 = create_test_span("span3"); // This span should be dropped
12021201

1203-
processor.on_end(span1.clone());
1204-
processor.on_end(span2.clone());
1205-
processor.on_end(span3.clone()); // This span exceeds the queue size
1202+
processor.on_end(&mut span1);
1203+
processor.on_end(&mut span2);
1204+
processor.on_end(&mut span3); // This span exceeds the queue size
12061205

12071206
// Wait for the scheduled delay to expire
12081207
std::thread::sleep(Duration::from_secs(3));
@@ -1242,7 +1241,7 @@ mod tests {
12421241
KeyValue::new("key1", "value1"),
12431242
KeyValue::new("key2", "value2"),
12441243
];
1245-
processor.on_end(span_data.clone());
1244+
processor.on_end(&mut span_data);
12461245

12471246
// Force flush to export the span
12481247
let _ = processor.force_flush();
@@ -1272,8 +1271,8 @@ mod tests {
12721271
processor.set_resource(&resource);
12731272

12741273
// Create a span and send it to the processor
1275-
let test_span = create_test_span("resource_test");
1276-
processor.on_end(test_span.clone());
1274+
let mut test_span = create_test_span("resource_test");
1275+
processor.on_end(&mut test_span);
12771276

12781277
// Force flush to ensure the span is exported
12791278
let _ = processor.force_flush();
@@ -1307,8 +1306,8 @@ mod tests {
13071306
let processor = BatchSpanProcessor::new(exporter, config);
13081307

13091308
for _ in 0..4 {
1310-
let span = new_test_export_span_data();
1311-
processor.on_end(span);
1309+
let mut span = new_test_export_span_data();
1310+
processor.on_end(&mut span);
13121311
}
13131312

13141313
processor.force_flush().unwrap();
@@ -1330,8 +1329,8 @@ mod tests {
13301329
let processor = BatchSpanProcessor::new(exporter, config);
13311330

13321331
for _ in 0..4 {
1333-
let span = new_test_export_span_data();
1334-
processor.on_end(span);
1332+
let mut span = new_test_export_span_data();
1333+
processor.on_end(&mut span);
13351334
}
13361335

13371336
processor.force_flush().unwrap();
@@ -1357,8 +1356,8 @@ mod tests {
13571356
for _ in 0..10 {
13581357
let processor_clone = Arc::clone(&processor);
13591358
let handle = tokio::spawn(async move {
1360-
let span = new_test_export_span_data();
1361-
processor_clone.on_end(span);
1359+
let mut span = new_test_export_span_data();
1360+
processor_clone.on_end(&mut span);
13621361
});
13631362
handles.push(handle);
13641363
}

0 commit comments

Comments
 (0)