-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for tracing appender (#1415)
- Loading branch information
1 parent
cd23ec2
commit 8b838fc
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,3 +140,145 @@ const fn severity_of_level(level: &Level) -> Severity { | |
Level::ERROR => Severity::Error, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::layer; | ||
use opentelemetry::logs::Severity; | ||
use opentelemetry::trace::TracerProvider as _; | ||
use opentelemetry::trace::{TraceContextExt, TraceFlags, Tracer}; | ||
use opentelemetry::{logs::AnyValue, Key}; | ||
use opentelemetry_sdk::logs::LoggerProvider; | ||
use opentelemetry_sdk::testing::logs::InMemoryLogsExporter; | ||
use opentelemetry_sdk::trace::{config, Sampler, TracerProvider}; | ||
use tracing::error; | ||
use tracing_subscriber::layer::SubscriberExt; | ||
|
||
// cargo test --features=testing | ||
#[test] | ||
fn tracing_appender_standalone() { | ||
// Arrange | ||
let exporter: InMemoryLogsExporter = InMemoryLogsExporter::default(); | ||
let logger_provider = LoggerProvider::builder() | ||
.with_simple_exporter(exporter.clone()) | ||
.build(); | ||
|
||
let layer = layer::OpenTelemetryTracingBridge::new(&logger_provider); | ||
let subscriber = tracing_subscriber::registry().with(layer); | ||
|
||
// avoiding setting tracing subscriber as global as that does not | ||
// play well with unit tests. | ||
let _guard = tracing::subscriber::set_default(subscriber); | ||
|
||
// Act | ||
error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]"); | ||
logger_provider.force_flush(); | ||
|
||
// Assert TODO: move to helper methods | ||
let exported_logs = exporter | ||
.get_emitted_logs() | ||
.expect("Logs are expected to be exported."); | ||
assert_eq!(exported_logs.len(), 1); | ||
let log = exported_logs | ||
.get(0) | ||
.expect("Atleast one log is expected to be present."); | ||
|
||
// Validate common fields | ||
assert_eq!(log.instrumentation.name, "opentelemetry-appender-tracing"); | ||
assert_eq!(log.record.severity_number, Some(Severity::Error)); | ||
|
||
// Validate trace context is none. | ||
assert!(log.record.trace_context.is_none()); | ||
|
||
// Validate attributes | ||
let attributes: Vec<(Key, AnyValue)> = log | ||
.record | ||
.attributes | ||
.clone() | ||
.expect("Attributes are expected"); | ||
assert_eq!(attributes.len(), 4); | ||
assert!(attributes.contains(&(Key::new("name"), "my-event-name".into()))); | ||
assert!(attributes.contains(&(Key::new("event_id"), 20.into()))); | ||
assert!(attributes.contains(&(Key::new("user_name"), "otel".into()))); | ||
assert!(attributes.contains(&(Key::new("user_email"), "[email protected]".into()))); | ||
} | ||
|
||
#[test] | ||
fn tracing_appender_inside_tracing_context() { | ||
// Arrange | ||
let exporter: InMemoryLogsExporter = InMemoryLogsExporter::default(); | ||
let logger_provider = LoggerProvider::builder() | ||
.with_simple_exporter(exporter.clone()) | ||
.build(); | ||
|
||
let layer = layer::OpenTelemetryTracingBridge::new(&logger_provider); | ||
let subscriber = tracing_subscriber::registry().with(layer); | ||
|
||
// avoiding setting tracing subscriber as global as that does not | ||
// play well with unit tests. | ||
let _guard = tracing::subscriber::set_default(subscriber); | ||
|
||
// setup tracing as well. | ||
let tracer_provider = TracerProvider::builder() | ||
.with_config(config().with_sampler(Sampler::AlwaysOn)) | ||
.build(); | ||
let tracer = tracer_provider.tracer("test-tracer"); | ||
|
||
// Act | ||
let (trace_id_expected, span_id_expected) = tracer.in_span("test-span", |cx| { | ||
let trace_id = cx.span().span_context().trace_id(); | ||
let span_id = cx.span().span_context().span_id(); | ||
|
||
// logging is done inside span context. | ||
error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]"); | ||
(trace_id, span_id) | ||
}); | ||
|
||
logger_provider.force_flush(); | ||
|
||
// Assert TODO: move to helper methods | ||
let exported_logs = exporter | ||
.get_emitted_logs() | ||
.expect("Logs are expected to be exported."); | ||
assert_eq!(exported_logs.len(), 1); | ||
let log = exported_logs | ||
.get(0) | ||
.expect("Atleast one log is expected to be present."); | ||
|
||
// validate common fields. | ||
assert_eq!(log.instrumentation.name, "opentelemetry-appender-tracing"); | ||
assert_eq!(log.record.severity_number, Some(Severity::Error)); | ||
|
||
// validate trace context. | ||
assert!(log.record.trace_context.is_some()); | ||
assert_eq!( | ||
log.record.trace_context.as_ref().unwrap().trace_id, | ||
trace_id_expected | ||
); | ||
assert_eq!( | ||
log.record.trace_context.as_ref().unwrap().span_id, | ||
span_id_expected | ||
); | ||
assert_eq!( | ||
log.record | ||
.trace_context | ||
.as_ref() | ||
.unwrap() | ||
.trace_flags | ||
.unwrap(), | ||
TraceFlags::SAMPLED | ||
); | ||
|
||
// validate attributes. | ||
let attributes: Vec<(Key, AnyValue)> = log | ||
.record | ||
.attributes | ||
.clone() | ||
.expect("Attributes are expected"); | ||
assert_eq!(attributes.len(), 4); | ||
assert!(attributes.contains(&(Key::new("name"), "my-event-name".into()))); | ||
assert!(attributes.contains(&(Key::new("event_id"), 20.into()))); | ||
assert!(attributes.contains(&(Key::new("user_name"), "otel".into()))); | ||
assert!(attributes.contains(&(Key::new("user_email"), "[email protected]".into()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters