-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract test of fake-collector to be like demo & doc
- Loading branch information
Showing
6 changed files
with
178 additions
and
137 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,70 @@ | ||
# fake-opentelemetry-collector | ||
|
||
A Fake (basic) opentelemetry collector, useful to test what is collected opentelemetry | ||
A Fake (basic) opentelemetry collector, useful to test what is collected by opentelemetry | ||
|
||
Usage example with [insta](https://crates.io/crates/insta) (snapshot testing) | ||
|
||
```rust | ||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_fake_tracer_and_collector() { | ||
let fake_collector = FakeCollectorServer::start() | ||
.await | ||
.expect("fake collector setup and started"); | ||
let tracer = setup_tracer(&fake_collector).await; | ||
|
||
debug!("Sending span..."); | ||
let mut span = tracer | ||
.span_builder("my-test-span") | ||
.with_kind(SpanKind::Server) | ||
.start(&tracer); | ||
span.add_event("my-test-event", vec![]); | ||
span.end(); | ||
|
||
shutdown_tracer_provider(); | ||
|
||
let otel_spans = fake_collector.exported_spans(); | ||
//insta::assert_debug_snapshot!(otel_spans); | ||
insta::assert_yaml_snapshot!(otel_spans, { | ||
"[].start_time_unix_nano" => "[timestamp]", | ||
"[].end_time_unix_nano" => "[timestamp]", | ||
"[].events[].time_unix_nano" => "[timestamp]", | ||
"[].trace_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(trace_id) = value.as_str()); | ||
format!("[trace_id:lg{}]", trace_id.len()) | ||
}), | ||
"[].span_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(span_id) = value.as_str()); | ||
format!("[span_id:lg{}]", span_id.len()) | ||
}), | ||
"[].links[].trace_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(trace_id) = value.as_str()); | ||
format!("[trace_id:lg{}]", trace_id.len()) | ||
}), | ||
"[].links[].span_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(span_id) = value.as_str()); | ||
format!("[span_id:lg{}]", span_id.len()) | ||
}), | ||
}); | ||
} | ||
use std::time::Duration; | ||
|
||
use fake_opentelemetry_collector::{setup_tracer_provider, FakeCollectorServer}; | ||
use opentelemetry::trace::TracerProvider; | ||
use opentelemetry::trace::{Span, SpanKind, Tracer}; | ||
use tracing::debug; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn demo_fake_tracer_and_collector() { | ||
debug!("Start the fake collector"); | ||
let mut fake_collector = FakeCollectorServer::start() | ||
.await | ||
.expect("fake collector setup and started"); | ||
|
||
debug!("Init the 'application' & tracer provider"); | ||
let tracer_provider = setup_tracer_provider(&fake_collector).await; | ||
let tracer = tracer_provider.tracer("test"); | ||
|
||
debug!("Run the 'application' & sending span..."); | ||
let mut span = tracer | ||
.span_builder("my-test-span") | ||
.with_kind(SpanKind::Server) | ||
.start(&tracer); | ||
span.add_event("my-test-event", vec![]); | ||
span.end(); | ||
|
||
debug!("Shutdown the 'application' & tracer provider and force flush the spans"); | ||
let _ = tracer_provider.force_flush(); | ||
tracer_provider | ||
.shutdown() | ||
.expect("no error during shutdown"); | ||
drop(tracer_provider); | ||
|
||
debug!("Collect & check the spans"); | ||
let otel_spans = fake_collector | ||
.exported_spans(1, Duration::from_secs(20)) | ||
.await; | ||
//insta::assert_debug_snapshot!(otel_spans); | ||
insta::assert_yaml_snapshot!(otel_spans, { | ||
"[].start_time_unix_nano" => "[timestamp]", | ||
"[].end_time_unix_nano" => "[timestamp]", | ||
"[].events[].time_unix_nano" => "[timestamp]", | ||
"[].trace_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(trace_id) = value.as_str()); | ||
format!("[trace_id:lg{}]", trace_id.len()) | ||
}), | ||
"[].span_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(span_id) = value.as_str()); | ||
format!("[span_id:lg{}]", span_id.len()) | ||
}), | ||
"[].links[].trace_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(trace_id) = value.as_str()); | ||
format!("[trace_id:lg{}]", trace_id.len()) | ||
}), | ||
"[].links[].span_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(span_id) = value.as_str()); | ||
format!("[span_id:lg{}]", span_id.len()) | ||
}), | ||
}); | ||
} | ||
``` | ||
|
||
test example at <https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/tree/main/fake-opentelemetry-collector/tests> |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::time::Duration; | ||
|
||
use fake_opentelemetry_collector::{setup_logger_provider, FakeCollectorServer}; | ||
use opentelemetry::logs::{LogRecord, Logger, LoggerProvider, Severity}; | ||
use tracing::debug; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn demo_fake_logger_and_collector() { | ||
debug!("Start the fake collector"); | ||
let mut fake_collector = FakeCollectorServer::start() | ||
.await | ||
.expect("fake collector setup and started"); | ||
|
||
debug!("Init the 'application' & logger provider"); | ||
let logger_provider = setup_logger_provider(&fake_collector).await; | ||
let logger = logger_provider.logger("test"); | ||
|
||
debug!("Run the 'application' & send log ..."); | ||
let mut record = logger.create_log_record(); | ||
record.set_body("This is information".into()); | ||
record.set_severity_number(Severity::Info); | ||
record.set_severity_text("info"); | ||
logger.emit(record); | ||
|
||
debug!("Shutdown the 'application' & logger provider"); | ||
let _ = logger_provider.force_flush(); | ||
logger_provider | ||
.shutdown() | ||
.expect("no error during shutdown"); | ||
drop(logger_provider); | ||
|
||
debug!("Collect & check the logs"); | ||
let otel_logs = fake_collector | ||
.exported_logs(1, Duration::from_millis(500)) | ||
.await; | ||
|
||
insta::assert_yaml_snapshot!(otel_logs, { | ||
"[].trace_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(trace_id) = value.as_str()); | ||
format!("[trace_id:lg{}]", trace_id.len()) | ||
}), | ||
"[].span_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(span_id) = value.as_str()); | ||
format!("[span_id:lg{}]", span_id.len()) | ||
}), | ||
"[].observed_time_unix_nano" => "[timestamp]", | ||
"[].severity_number" => 9, | ||
"[].severity_text" => "info", | ||
"[].body" => "AnyValue { value: Some(StringValue(\"This is information\")) }", | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::time::Duration; | ||
|
||
use fake_opentelemetry_collector::{setup_tracer_provider, FakeCollectorServer}; | ||
use opentelemetry::trace::TracerProvider; | ||
use opentelemetry::trace::{Span, SpanKind, Tracer}; | ||
use tracing::debug; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn demo_fake_tracer_and_collector() { | ||
debug!("Start the fake collector"); | ||
let mut fake_collector = FakeCollectorServer::start() | ||
.await | ||
.expect("fake collector setup and started"); | ||
|
||
debug!("Init the 'application' & tracer provider"); | ||
let tracer_provider = setup_tracer_provider(&fake_collector).await; | ||
let tracer = tracer_provider.tracer("test"); | ||
|
||
debug!("Run the 'application' & sending span..."); | ||
let mut span = tracer | ||
.span_builder("my-test-span") | ||
.with_kind(SpanKind::Server) | ||
.start(&tracer); | ||
span.add_event("my-test-event", vec![]); | ||
span.end(); | ||
|
||
debug!("Shutdown the 'application' & tracer provider and force flush the spans"); | ||
let _ = tracer_provider.force_flush(); | ||
tracer_provider | ||
.shutdown() | ||
.expect("no error during shutdown"); | ||
drop(tracer_provider); | ||
|
||
debug!("Collect & check the spans"); | ||
let otel_spans = fake_collector | ||
.exported_spans(1, Duration::from_secs(20)) | ||
.await; | ||
//insta::assert_debug_snapshot!(otel_spans); | ||
insta::assert_yaml_snapshot!(otel_spans, { | ||
"[].start_time_unix_nano" => "[timestamp]", | ||
"[].end_time_unix_nano" => "[timestamp]", | ||
"[].events[].time_unix_nano" => "[timestamp]", | ||
"[].trace_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(trace_id) = value.as_str()); | ||
format!("[trace_id:lg{}]", trace_id.len()) | ||
}), | ||
"[].span_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(span_id) = value.as_str()); | ||
format!("[span_id:lg{}]", span_id.len()) | ||
}), | ||
"[].links[].trace_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(trace_id) = value.as_str()); | ||
format!("[trace_id:lg{}]", trace_id.len()) | ||
}), | ||
"[].links[].span_id" => insta::dynamic_redaction(|value, _path| { | ||
assert2::let_assert!(Some(span_id) = value.as_str()); | ||
format!("[span_id:lg{}]", span_id.len()) | ||
}), | ||
}); | ||
} |
3 changes: 2 additions & 1 deletion
3
...or__tests__fake_logger_and_collector.snap → ..._log__demo_fake_logger_and_collector.snap
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