Skip to content

Commit 0d97896

Browse files
committed
fix integration test, and enable batch in otlp-http example
1 parent 3feaddc commit 0d97896

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

opentelemetry-otlp/examples/basic-otlp-http/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ fn init_traces() -> Result<sdktrace::TracerProvider, TraceError> {
4949
.build()?;
5050

5151
Ok(TracerProvider::builder()
52-
// TODO: Enable BatchExporter after
53-
// https://github.com/open-telemetry/opentelemetry-rust/pull/2456
54-
.with_simple_exporter(exporter)
52+
.with_batch_exporter(exporter)
5553
.with_resource(RESOURCE.clone())
5654
.build())
5755
}
@@ -73,7 +71,6 @@ fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, Metric
7371

7472
// #[tokio::main]
7573
// TODO: Re-enable tokio::main, if needed, after
76-
// https://github.com/open-telemetry/opentelemetry-rust/pull/2456
7774
fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
7875
let logger_provider = init_logs()?;
7976

opentelemetry-otlp/tests/integration_test/tests/traces.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ pub fn span_batch_non_tokio_main() -> Result<()> {
181181
std::thread::sleep(Duration::from_secs(2));
182182

183183
// Validate results
184-
// TODO: Fix this test
185-
// assert_traces_results(test_utils::TRACES_FILE, "./expected/traces.json")?;
184+
assert_traces_results(test_utils::TRACES_FILE, "./expected/traces.json")?;
186185
Ok(())
187186
}
188187

opentelemetry-sdk/src/trace/span_processor.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ enum BatchMessage {
229229
ExportSpan(SpanData),
230230
ForceFlush(SyncSender<TraceResult<()>>),
231231
Shutdown(SyncSender<TraceResult<()>>),
232+
SetResource(Arc<Resource>),
232233
}
233234

234235
/// A batch span processor with a dedicated background thread.
@@ -292,6 +293,9 @@ impl BatchSpanProcessor {
292293
let _ = sender.send(result);
293294
break;
294295
}
296+
BatchMessage::SetResource(resource) => {
297+
exporter.set_resource(&resource);
298+
}
295299
},
296300
Err(RecvTimeoutError::Timeout) => {
297301
if last_export_time.elapsed() >= config.scheduled_delay {
@@ -396,6 +400,14 @@ impl SpanProcessor for BatchSpanProcessor {
396400
}
397401
result
398402
}
403+
404+
/// Set the resource for the processor.
405+
fn set_resource(&mut self, resource: &Resource) {
406+
let resource = Arc::new(resource.clone());
407+
let _ = self
408+
.message_sender
409+
.try_send(BatchMessage::SetResource(resource));
410+
}
399411
}
400412

401413
/// Builder for `BatchSpanProcessorDedicatedThread`.
@@ -782,20 +794,24 @@ mod tests {
782794
}
783795
}
784796

797+
use crate::Resource;
785798
use futures_util::future::BoxFuture;
786799
use futures_util::FutureExt;
800+
use opentelemetry::{Key, KeyValue, Value};
787801
use std::sync::{atomic::Ordering, Arc, Mutex};
788802

789803
// Mock exporter to test functionality
790804
#[derive(Debug)]
791805
struct MockSpanExporter {
792806
exported_spans: Arc<Mutex<Vec<SpanData>>>,
807+
exported_resource: Arc<Mutex<Option<Resource>>>,
793808
}
794809

795810
impl MockSpanExporter {
796811
fn new() -> Self {
797812
Self {
798813
exported_spans: Arc::new(Mutex::new(Vec::new())),
814+
exported_resource: Arc::new(Mutex::new(None)),
799815
}
800816
}
801817
}
@@ -811,6 +827,10 @@ mod tests {
811827
}
812828

813829
fn shutdown(&mut self) {}
830+
fn set_resource(&mut self, resource: &Resource) {
831+
let mut exported_resource = self.exported_resource.lock().unwrap();
832+
*exported_resource = Some(resource.clone());
833+
}
814834
}
815835

816836
#[test]
@@ -947,4 +967,69 @@ mod tests {
947967
let dropped_count = processor.dropped_span_count.load(Ordering::Relaxed);
948968
assert_eq!(dropped_count, 1, "Unexpected number of dropped spans");
949969
}
970+
971+
#[test]
972+
fn validate_span_attributes_exported_correctly() {
973+
let exporter = MockSpanExporter::new();
974+
let exporter_shared = exporter.exported_spans.clone();
975+
let config = BatchConfigBuilder::default().build();
976+
let processor = BatchSpanProcessor::new(exporter, config);
977+
978+
// Create a span with attributes
979+
let mut span_data = create_test_span("attribute_validation");
980+
span_data.attributes = vec![
981+
KeyValue::new("key1", "value1"),
982+
KeyValue::new("key2", "value2"),
983+
];
984+
processor.on_end(span_data.clone());
985+
986+
// Force flush to export the span
987+
let _ = processor.force_flush();
988+
989+
// Validate the exported attributes
990+
let exported_spans = exporter_shared.lock().unwrap();
991+
assert_eq!(exported_spans.len(), 1);
992+
let exported_span = &exported_spans[0];
993+
assert!(exported_span
994+
.attributes
995+
.contains(&KeyValue::new("key1", "value1")));
996+
assert!(exported_span
997+
.attributes
998+
.contains(&KeyValue::new("key2", "value2")));
999+
}
1000+
1001+
#[test]
1002+
fn batchspanprocessor_sets_and_exports_with_resource() {
1003+
let exporter = MockSpanExporter::new();
1004+
let exporter_shared = exporter.exported_spans.clone();
1005+
let resource_shared = exporter.exported_resource.clone();
1006+
let config = BatchConfigBuilder::default().build();
1007+
let mut processor = BatchSpanProcessor::new(exporter, config);
1008+
1009+
// Set a resource for the processor
1010+
let resource = Resource::new(vec![KeyValue::new("service.name", "test_service")]);
1011+
processor.set_resource(&resource);
1012+
1013+
// Create a span and send it to the processor
1014+
let test_span = create_test_span("resource_test");
1015+
processor.on_end(test_span.clone());
1016+
1017+
// Force flush to ensure the span is exported
1018+
let _ = processor.force_flush();
1019+
1020+
// Validate spans are exported
1021+
let exported_spans = exporter_shared.lock().unwrap();
1022+
assert_eq!(exported_spans.len(), 1);
1023+
1024+
// Validate the resource is correctly set in the exporter
1025+
let exported_resource = resource_shared.lock().unwrap();
1026+
assert!(exported_resource.is_some());
1027+
assert_eq!(
1028+
exported_resource
1029+
.as_ref()
1030+
.unwrap()
1031+
.get(Key::new("service.name")),
1032+
Some(Value::from("test_service"))
1033+
);
1034+
}
9501035
}

0 commit comments

Comments
 (0)