Skip to content

Commit a062c9c

Browse files
committed
cont...
1 parent 993dcbc commit a062c9c

File tree

5 files changed

+70
-86
lines changed

5 files changed

+70
-86
lines changed

opentelemetry-sdk/benches/batch_span_processor.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use opentelemetry::trace::{
33
SpanContext, SpanId, SpanKind, Status, TraceFlags, TraceId, TraceState,
44
};
55
use opentelemetry_sdk::export::trace::SpanData;
6-
use opentelemetry_sdk::runtime::Tokio;
76
use opentelemetry_sdk::testing::trace::NoopSpanExporter;
87
use opentelemetry_sdk::trace::{
98
BatchConfigBuilder, BatchSpanProcessor, SpanEvents, SpanLinks, SpanProcessor,
@@ -49,14 +48,13 @@ fn criterion_benchmark(c: &mut Criterion) {
4948
b.iter(|| {
5049
let rt = Runtime::new().unwrap();
5150
rt.block_on(async move {
52-
let span_processor =
53-
BatchSpanProcessor::builder(NoopSpanExporter::new(), Tokio)
54-
.with_batch_config(
55-
BatchConfigBuilder::default()
56-
.with_max_queue_size(10_000)
57-
.build(),
58-
)
59-
.build();
51+
let span_processor = BatchSpanProcessor::builder(NoopSpanExporter::new())
52+
.with_batch_config(
53+
BatchConfigBuilder::default()
54+
.with_max_queue_size(10_000)
55+
.build(),
56+
)
57+
.build();
6058
let mut shared_span_processor = Arc::new(span_processor);
6159
let mut handles = Vec::with_capacity(10);
6260
for _ in 0..task_num {
@@ -70,10 +68,9 @@ fn criterion_benchmark(c: &mut Criterion) {
7068
}));
7169
}
7270
futures_util::future::join_all(handles).await;
73-
let _ =
74-
Arc::<BatchSpanProcessor<Tokio>>::get_mut(&mut shared_span_processor)
75-
.unwrap()
76-
.shutdown();
71+
let _ = Arc::<BatchSpanProcessor>::get_mut(&mut shared_span_processor)
72+
.unwrap()
73+
.shutdown();
7774
});
7875
})
7976
},

opentelemetry-sdk/src/trace/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ mod sampler;
1515
mod span;
1616
mod span_limit;
1717
mod span_processor;
18-
mod span_processor_with_async_runtime;
19-
18+
#[cfg(feature = "experimental_trace_batch_span_processor_with_async_runtime")]
19+
pub mod span_processor_with_async_runtime;
2020
mod tracer;
2121

2222
pub use config::{config, Config};
@@ -33,16 +33,12 @@ pub use span_processor::{
3333
SimpleSpanProcessor, SpanProcessor,
3434
};
3535

36-
/// Re-export asynchronous runtime span processor components.
37-
// TODO: make them available without re-export
38-
#[cfg(feature = "experimental_trace_batch_span_processor_with_async_runtime")]
39-
pub mod span_processor_with_async_runtime;
40-
4136
pub use tracer::Tracer;
4237

4338
#[cfg(feature = "jaeger_remote_sampler")]
4439
pub use sampler::{JaegerRemoteSampler, JaegerRemoteSamplerBuilder};
4540

41+
#[cfg(feature = "experimental_trace_batch_span_processor_with_async_runtime")]
4642
#[cfg(test)]
4743
mod runtime_tests;
4844

opentelemetry-sdk/src/trace/provider.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
/// provider.shutdown();
6363
/// }
6464
/// ```
65+
#[cfg(feature = "experimental_trace_batch_span_processor_with_async_runtime")]
6566
use crate::runtime::RuntimeChannel;
6667
use crate::trace::{
6768
BatchSpanProcessor, Config, RandomIdGenerator, Sampler, SimpleSpanProcessor, SpanLimits, Tracer,
@@ -295,12 +296,20 @@ impl Builder {
295296
Builder { processors, ..self }
296297
}
297298

299+
#[cfg(feature = "experimental_trace_batch_span_processor_with_async_runtime")]
298300
/// The [`SpanExporter`] setup using a default [`BatchSpanProcessor`] that this provider should use.
299301
pub fn with_batch_exporter<T: SpanExporter + 'static, R: RuntimeChannel>(
300302
self,
301303
exporter: T,
302304
runtime: R,
303305
) -> Self {
306+
let batch = BatchSpanProcessor::builder(exporter, runtime).build();
307+
self.with_span_processor(batch)
308+
}
309+
310+
#[cfg(not(feature = "experimental_trace_batch_span_processor_with_async_runtime"))]
311+
/// The [`SpanExporter`] setup using a default [`BatchSpanProcessor`] that this provider should use.
312+
pub fn with_batch_exporter<T: SpanExporter + 'static>(self, exporter: T) -> Self {
304313
let batch = BatchSpanProcessor::builder(exporter).build();
305314
self.with_span_processor(batch)
306315
}

opentelemetry-sdk/src/trace/runtime_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl SpanCountExporter {
4646
}
4747
}
4848

49+
#[cfg(feature = "experimental_trace_batch_span_processor_with_async_runtime")]
4950
#[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))]
5051
fn build_batch_tracer_provider<R: RuntimeChannel>(
5152
exporter: SpanCountExporter,

opentelemetry-sdk/src/trace/span_processor.rs

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ enum BatchMessage {
181181
pub struct BatchSpanProcessor {
182182
message_sender: SyncSender<BatchMessage>,
183183
handle: Mutex<Option<thread::JoinHandle<()>>>,
184+
forceflush_timeout: Duration,
184185
shutdown_timeout: Duration,
185186
is_shutdown: AtomicBool,
186187
dropped_span_count: Arc<AtomicUsize>,
@@ -190,14 +191,15 @@ impl BatchSpanProcessor {
190191
/// Creates a new instance of `BatchSpanProcessor`.
191192
pub fn new<E>(
192193
mut exporter: E,
193-
max_queue_size: usize,
194-
scheduled_delay: Duration,
195-
shutdown_timeout: Duration,
194+
config: BatchConfig,
195+
//ax_queue_size: usize,
196+
//scheduled_delay: Duration,
197+
//shutdown_timeout: Duration,
196198
) -> Self
197199
where
198200
E: SpanExporter + Send + 'static,
199201
{
200-
let (message_sender, message_receiver) = sync_channel(max_queue_size);
202+
let (message_sender, message_receiver) = sync_channel(config.max_queue_size);
201203

202204
let handle = thread::Builder::new()
203205
.name("BatchSpanProcessorDedicatedThread".to_string())
@@ -206,13 +208,15 @@ impl BatchSpanProcessor {
206208
let mut last_export_time = Instant::now();
207209

208210
loop {
209-
let timeout = scheduled_delay.saturating_sub(last_export_time.elapsed());
211+
let timeout = config
212+
.scheduled_delay
213+
.saturating_sub(last_export_time.elapsed());
210214
match message_receiver.recv_timeout(timeout) {
211215
Ok(message) => match message {
212216
BatchMessage::ExportSpan(span) => {
213217
spans.push(span);
214-
if spans.len() >= max_queue_size
215-
|| last_export_time.elapsed() >= scheduled_delay
218+
if spans.len() >= config.max_queue_size
219+
|| last_export_time.elapsed() >= config.scheduled_delay
216220
{
217221
if let Err(err) = block_on(exporter.export(spans.split_off(0)))
218222
{
@@ -232,7 +236,7 @@ impl BatchSpanProcessor {
232236
}
233237
},
234238
Err(RecvTimeoutError::Timeout) => {
235-
if last_export_time.elapsed() >= scheduled_delay {
239+
if last_export_time.elapsed() >= config.scheduled_delay {
236240
if let Err(err) = block_on(exporter.export(spans.split_off(0))) {
237241
eprintln!("Export error: {:?}", err);
238242
}
@@ -251,7 +255,8 @@ impl BatchSpanProcessor {
251255
Self {
252256
message_sender,
253257
handle: Mutex::new(Some(handle)),
254-
shutdown_timeout,
258+
forceflush_timeout: Duration::from_secs(5), // TODO: make this configurable
259+
shutdown_timeout: Duration::from_secs(5), // TODO: make this configurable
255260
is_shutdown: AtomicBool::new(false),
256261
dropped_span_count: Arc::new(AtomicUsize::new(0)),
257262
}
@@ -262,7 +267,10 @@ impl BatchSpanProcessor {
262267
where
263268
E: SpanExporter + Send + 'static,
264269
{
265-
BatchSpanProcessorBuilder::new(exporter)
270+
BatchSpanProcessorBuilder {
271+
exporter,
272+
config: BatchConfig::default(),
273+
}
266274
}
267275
}
268276

@@ -302,8 +310,8 @@ impl SpanProcessor for BatchSpanProcessor {
302310
.map_err(|_| TraceError::Other("Failed to send ForceFlush message".into()))?;
303311

304312
receiver
305-
.recv_timeout(self.shutdown_timeout)
306-
.map_err(|_| TraceError::ExportTimedOut(self.shutdown_timeout))?
313+
.recv_timeout(self.forceflush_timeout)
314+
.map_err(|_| TraceError::ExportTimedOut(self.forceflush_timeout))?
307315
}
308316

309317
/// Shuts down the processor.
@@ -333,51 +341,21 @@ where
333341
E: SpanExporter + Send + 'static,
334342
{
335343
exporter: E,
336-
max_queue_size: usize,
337-
scheduled_delay: Duration,
338-
shutdown_timeout: Duration,
344+
config: BatchConfig,
339345
}
340346

341347
impl<E> BatchSpanProcessorBuilder<E>
342348
where
343349
E: SpanExporter + Send + 'static,
344350
{
345-
/// Creates a new builder with default values.
346-
pub fn new(exporter: E) -> Self {
347-
Self {
348-
exporter,
349-
max_queue_size: 2048,
350-
scheduled_delay: Duration::from_secs(5),
351-
shutdown_timeout: Duration::from_secs(5),
352-
}
351+
/// Set the BatchConfig for [BatchSpanProcessorBuilder]
352+
pub fn with_batch_config(self, config: BatchConfig) -> Self {
353+
BatchSpanProcessorBuilder { config, ..self }
353354
}
354355

355-
/// Sets the maximum queue size for spans.
356-
pub fn with_max_queue_size(mut self, size: usize) -> Self {
357-
self.max_queue_size = size;
358-
self
359-
}
360-
361-
/// Sets the delay between exports.
362-
pub fn with_scheduled_delay(mut self, delay: Duration) -> Self {
363-
self.scheduled_delay = delay;
364-
self
365-
}
366-
367-
/// Sets the timeout for shutdown and flush operations.
368-
pub fn with_shutdown_timeout(mut self, timeout: Duration) -> Self {
369-
self.shutdown_timeout = timeout;
370-
self
371-
}
372-
373-
/// Builds the `BatchSpanProcessorDedicatedThread` instance.
356+
/// Build a new instance of `BatchSpanProcessor`.
374357
pub fn build(self) -> BatchSpanProcessor {
375-
BatchSpanProcessor::new(
376-
self.exporter,
377-
self.max_queue_size,
378-
self.scheduled_delay,
379-
self.shutdown_timeout,
380-
)
358+
BatchSpanProcessor::new(self.exporter, self.config)
381359
}
382360
}
383361

@@ -773,12 +751,13 @@ mod tests {
773751
fn batchspanprocessor_handles_on_end() {
774752
let exporter = MockSpanExporter::new();
775753
let exporter_shared = exporter.exported_spans.clone();
776-
let processor = BatchSpanProcessor::new(
777-
exporter,
778-
10, // max queue size
779-
Duration::from_secs(5),
780-
Duration::from_secs(2),
781-
);
754+
let config = BatchConfigBuilder::default()
755+
.with_max_queue_size(10)
756+
.with_max_export_batch_size(10)
757+
.with_scheduled_delay(Duration::from_secs(5))
758+
.with_max_export_timeout(Duration::from_secs(2))
759+
.build();
760+
let processor = BatchSpanProcessor::new(exporter, config);
782761

783762
let test_span = create_test_span("test_span");
784763
processor.on_end(test_span.clone());
@@ -795,12 +774,13 @@ mod tests {
795774
fn batchspanprocessor_force_flush() {
796775
let exporter = MockSpanExporter::new();
797776
let exporter_shared = exporter.exported_spans.clone(); // Shared access to verify exported spans
798-
let processor = BatchSpanProcessor::new(
799-
exporter,
800-
10, // max queue size
801-
Duration::from_secs(5),
802-
Duration::from_secs(2),
803-
);
777+
let config = BatchConfigBuilder::default()
778+
.with_max_queue_size(10)
779+
.with_max_export_batch_size(10)
780+
.with_scheduled_delay(Duration::from_secs(5))
781+
.with_max_export_timeout(Duration::from_secs(2))
782+
.build();
783+
let processor = BatchSpanProcessor::new(exporter, config);
804784

805785
// Create a test span and send it to the processor
806786
let test_span = create_test_span("force_flush_span");
@@ -824,12 +804,13 @@ mod tests {
824804
fn batchspanprocessor_shutdown() {
825805
let exporter = MockSpanExporter::new();
826806
let exporter_shared = exporter.exported_spans.clone(); // Shared access to verify exported spans
827-
let processor = BatchSpanProcessor::new(
828-
exporter,
829-
10, // max queue size
830-
Duration::from_secs(5),
831-
Duration::from_secs(2),
832-
);
807+
let config = BatchConfigBuilder::default()
808+
.with_max_queue_size(10)
809+
.with_max_export_batch_size(10)
810+
.with_scheduled_delay(Duration::from_secs(5))
811+
.with_max_export_timeout(Duration::from_secs(2))
812+
.build();
813+
let processor = BatchSpanProcessor::new(exporter, config);
833814

834815
// Create a test span and send it to the processor
835816
let test_span = create_test_span("shutdown_span");

0 commit comments

Comments
 (0)