Skip to content

Commit 768dd0e

Browse files
committed
modify test to cover exporte too
1 parent 60ced1a commit 768dd0e

File tree

2 files changed

+118
-30
lines changed

2 files changed

+118
-30
lines changed

opentelemetry-sdk/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
`LogExporter` trait no longer requires a mutable ref to `self`. If the exporter
6565
needs to mutate state, it should rely on interior mutability.
6666
[2764](https://github.com/open-telemetry/opentelemetry-rust/pull/2764)
67-
- **Breaking** for custom LogProcessor/Exporter authors: Changed `set_resource`
67+
- **Breaking** for custom `LogProcessor` authors: Changed `set_resource`
6868
to require mutable ref.
6969
`fn set_resource(&mut self, _resource: &Resource) {}`
7070

opentelemetry-sdk/src/logs/logger_provider.rs

Lines changed: 117 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl LoggerProviderBuilder {
273273
#[cfg(test)]
274274
mod tests {
275275
use crate::{
276-
logs::{InMemoryLogExporter, SdkLogRecord, TraceContext},
276+
logs::{InMemoryLogExporter, LogBatch, SdkLogRecord, TraceContext},
277277
resource::{
278278
SERVICE_NAME, TELEMETRY_SDK_LANGUAGE, TELEMETRY_SDK_NAME, TELEMETRY_SDK_VERSION,
279279
},
@@ -339,9 +339,40 @@ mod tests {
339339
}
340340
}
341341

342+
#[derive(Debug, Clone)]
343+
struct TestExporterForResource {
344+
resource: Arc<Mutex<Resource>>,
345+
}
346+
impl TestExporterForResource {
347+
fn new() -> Self {
348+
TestExporterForResource {
349+
resource: Arc::new(Mutex::new(Resource::empty())),
350+
}
351+
}
352+
353+
fn resource(&self) -> Resource {
354+
self.resource.lock().unwrap().clone()
355+
}
356+
}
357+
impl LogExporter for TestExporterForResource {
358+
async fn export(&self, _: LogBatch<'_>) -> OTelSdkResult {
359+
Ok(())
360+
}
361+
362+
fn set_resource(&mut self, resource: &Resource) {
363+
let mut res = self.resource.lock().unwrap();
364+
*res = resource.clone();
365+
}
366+
367+
fn shutdown(&self) -> OTelSdkResult {
368+
Ok(())
369+
}
370+
}
371+
342372
#[derive(Debug, Clone)]
343373
struct TestProcessorForResource {
344374
resource: Arc<Mutex<Resource>>,
375+
exporter: TestExporterForResource,
345376
}
346377
impl LogProcessor for TestProcessorForResource {
347378
fn emit(&self, _data: &mut SdkLogRecord, _scope: &InstrumentationScope) {
@@ -359,12 +390,14 @@ mod tests {
359390
fn set_resource(&mut self, resource: &Resource) {
360391
let mut res = self.resource.lock().unwrap();
361392
*res = resource.clone();
393+
self.exporter.set_resource(resource);
362394
}
363395
}
364396
impl TestProcessorForResource {
365-
fn new() -> Self {
397+
fn new(exporter: TestExporterForResource) -> Self {
366398
TestProcessorForResource {
367399
resource: Arc::new(Mutex::new(Resource::empty())),
400+
exporter,
368401
}
369402
}
370403
fn resource(&self) -> Resource {
@@ -373,49 +406,75 @@ mod tests {
373406
}
374407

375408
#[test]
376-
fn test_logger_provider_default_resource() {
377-
let assert_resource = |provider: &TestProcessorForResource,
409+
fn test_resource_handling_provider_processor_exporter() {
410+
let assert_resource = |processor: &TestProcessorForResource,
411+
exporter: &TestExporterForResource,
378412
resource_key: &'static str,
379413
expect: Option<&'static str>| {
380414
assert_eq!(
381-
provider
415+
processor
382416
.resource()
383417
.get(&Key::from_static_str(resource_key))
384418
.map(|v| v.to_string()),
385419
expect.map(|s| s.to_string())
386420
);
387-
};
388-
let assert_telemetry_resource = |provider: &TestProcessorForResource| {
389-
assert_eq!(
390-
provider.resource().get(&TELEMETRY_SDK_LANGUAGE.into()),
391-
Some(Value::from("rust"))
392-
);
393-
assert_eq!(
394-
provider.resource().get(&TELEMETRY_SDK_NAME.into()),
395-
Some(Value::from("opentelemetry"))
396-
);
421+
397422
assert_eq!(
398-
provider.resource().get(&TELEMETRY_SDK_VERSION.into()),
399-
Some(Value::from(env!("CARGO_PKG_VERSION")))
423+
exporter
424+
.resource()
425+
.get(&Key::from_static_str(resource_key))
426+
.map(|v| v.to_string()),
427+
expect.map(|s| s.to_string())
400428
);
401429
};
430+
let assert_telemetry_resource =
431+
|processor: &TestProcessorForResource, exporter: &TestExporterForResource| {
432+
assert_eq!(
433+
processor.resource().get(&TELEMETRY_SDK_LANGUAGE.into()),
434+
Some(Value::from("rust"))
435+
);
436+
assert_eq!(
437+
processor.resource().get(&TELEMETRY_SDK_NAME.into()),
438+
Some(Value::from("opentelemetry"))
439+
);
440+
assert_eq!(
441+
processor.resource().get(&TELEMETRY_SDK_VERSION.into()),
442+
Some(Value::from(env!("CARGO_PKG_VERSION")))
443+
);
444+
assert_eq!(
445+
exporter.resource().get(&TELEMETRY_SDK_LANGUAGE.into()),
446+
Some(Value::from("rust"))
447+
);
448+
assert_eq!(
449+
exporter.resource().get(&TELEMETRY_SDK_NAME.into()),
450+
Some(Value::from("opentelemetry"))
451+
);
452+
assert_eq!(
453+
exporter.resource().get(&TELEMETRY_SDK_VERSION.into()),
454+
Some(Value::from(env!("CARGO_PKG_VERSION")))
455+
);
456+
};
402457

403458
// If users didn't provide a resource and there isn't a env var set. Use default one.
404459
temp_env::with_var_unset("OTEL_RESOURCE_ATTRIBUTES", || {
405-
let processor_with_resource = TestProcessorForResource::new();
460+
let exporter_with_resource = TestExporterForResource::new();
461+
let processor_with_resource =
462+
TestProcessorForResource::new(exporter_with_resource.clone());
406463
let _ = super::SdkLoggerProvider::builder()
407464
.with_log_processor(processor_with_resource.clone())
408465
.build();
409466
assert_resource(
410467
&processor_with_resource,
468+
&exporter_with_resource,
411469
SERVICE_NAME,
412470
Some("unknown_service"),
413471
);
414-
assert_telemetry_resource(&processor_with_resource);
472+
assert_telemetry_resource(&processor_with_resource, &exporter_with_resource);
415473
});
416474

417475
// If user provided a resource, use that.
418-
let processor_with_resource = TestProcessorForResource::new();
476+
let exporter_with_resource = TestExporterForResource::new();
477+
let processor_with_resource = TestProcessorForResource::new(exporter_with_resource.clone());
419478
let _ = super::SdkLoggerProvider::builder()
420479
.with_resource(
421480
Resource::builder_empty()
@@ -424,26 +483,44 @@ mod tests {
424483
)
425484
.with_log_processor(processor_with_resource.clone())
426485
.build();
427-
assert_resource(&processor_with_resource, SERVICE_NAME, Some("test_service"));
486+
assert_resource(
487+
&processor_with_resource,
488+
&exporter_with_resource,
489+
SERVICE_NAME,
490+
Some("test_service"),
491+
);
428492
assert_eq!(processor_with_resource.resource().len(), 1);
429493

430494
// If `OTEL_RESOURCE_ATTRIBUTES` is set, read them automatically
431495
temp_env::with_var(
432496
"OTEL_RESOURCE_ATTRIBUTES",
433497
Some("key1=value1, k2, k3=value2"),
434498
|| {
435-
let processor_with_resource = TestProcessorForResource::new();
499+
let exporter_with_resource = TestExporterForResource::new();
500+
let processor_with_resource =
501+
TestProcessorForResource::new(exporter_with_resource.clone());
436502
let _ = super::SdkLoggerProvider::builder()
437503
.with_log_processor(processor_with_resource.clone())
438504
.build();
439505
assert_resource(
440506
&processor_with_resource,
507+
&exporter_with_resource,
441508
SERVICE_NAME,
442509
Some("unknown_service"),
443510
);
444-
assert_resource(&processor_with_resource, "key1", Some("value1"));
445-
assert_resource(&processor_with_resource, "k3", Some("value2"));
446-
assert_telemetry_resource(&processor_with_resource);
511+
assert_resource(
512+
&processor_with_resource,
513+
&exporter_with_resource,
514+
"key1",
515+
Some("value1"),
516+
);
517+
assert_resource(
518+
&processor_with_resource,
519+
&exporter_with_resource,
520+
"k3",
521+
Some("value2"),
522+
);
523+
assert_telemetry_resource(&processor_with_resource, &exporter_with_resource);
447524
assert_eq!(processor_with_resource.resource().len(), 6);
448525
},
449526
);
@@ -453,7 +530,9 @@ mod tests {
453530
"OTEL_RESOURCE_ATTRIBUTES",
454531
Some("my-custom-key=env-val,k2=value2"),
455532
|| {
456-
let processor_with_resource = TestProcessorForResource::new();
533+
let exporter_with_resource = TestExporterForResource::new();
534+
let processor_with_resource =
535+
TestProcessorForResource::new(exporter_with_resource.clone());
457536
let _ = super::SdkLoggerProvider::builder()
458537
.with_resource(
459538
Resource::builder()
@@ -467,27 +546,36 @@ mod tests {
467546
.build();
468547
assert_resource(
469548
&processor_with_resource,
549+
&exporter_with_resource,
470550
SERVICE_NAME,
471551
Some("unknown_service"),
472552
);
473553
assert_resource(
474554
&processor_with_resource,
555+
&exporter_with_resource,
475556
"my-custom-key",
476557
Some("my-custom-value"),
477558
);
478559
assert_resource(
479560
&processor_with_resource,
561+
&exporter_with_resource,
480562
"my-custom-key2",
481563
Some("my-custom-value2"),
482564
);
483-
assert_resource(&processor_with_resource, "k2", Some("value2"));
484-
assert_telemetry_resource(&processor_with_resource);
565+
assert_resource(
566+
&processor_with_resource,
567+
&exporter_with_resource,
568+
"k2",
569+
Some("value2"),
570+
);
571+
assert_telemetry_resource(&processor_with_resource, &exporter_with_resource);
485572
assert_eq!(processor_with_resource.resource().len(), 7);
486573
},
487574
);
488575

489576
// If user provided a resource, it takes priority during collision.
490-
let processor_with_resource = TestProcessorForResource::new();
577+
let exporter_with_resource = TestExporterForResource::new();
578+
let processor_with_resource = TestProcessorForResource::new(exporter_with_resource);
491579
let _ = super::SdkLoggerProvider::builder()
492580
.with_resource(Resource::empty())
493581
.with_log_processor(processor_with_resource.clone())

0 commit comments

Comments
 (0)