Skip to content

Commit

Permalink
refactor: extract test of fake-collector to be like demo & doc
Browse files Browse the repository at this point in the history
  • Loading branch information
davidB committed Nov 24, 2024
1 parent c028ae6 commit 7a6f339
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 137 deletions.
105 changes: 63 additions & 42 deletions fake-opentelemetry-collector/README.md
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>
92 changes: 0 additions & 92 deletions fake-opentelemetry-collector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,95 +126,3 @@ pub async fn setup_logger_provider(
)
.build()
}

#[cfg(test)]
mod tests {
use super::*;

use opentelemetry::logs::{LogRecord, Logger, LoggerProvider, Severity};
use opentelemetry::trace::TracerProvider;
use opentelemetry::trace::{Span, SpanKind, Tracer};

#[tokio::test(flavor = "multi_thread")]
async fn test_fake_tracer_and_collector() {
let mut fake_collector = FakeCollectorServer::start()
.await
.expect("fake collector setup and started");
let tracer_provider = setup_tracer_provider(&fake_collector).await;
let tracer = tracer_provider.tracer("test");

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();

let _ = tracer_provider.force_flush();
tracer_provider
.shutdown()
.expect("no error during shutdown");
drop(tracer_provider);

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())
}),
});
}

#[tokio::test(flavor = "multi_thread")]
async fn test_fake_logger_and_collector() {
let mut fake_collector = FakeCollectorServer::start()
.await
.expect("fake collector setup and started");

let logger_provider = setup_logger_provider(&fake_collector).await;
let logger = logger_provider.logger("test");
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);

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\")) }",
});
}
}
51 changes: 51 additions & 0 deletions fake-opentelemetry-collector/tests/demo_log.rs
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\")) }",
});
}
60 changes: 60 additions & 0 deletions fake-opentelemetry-collector/tests/demo_trace.rs
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())
}),
});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: fake-opentelemetry-collector/src/lib.rs
source: fake-opentelemetry-collector/tests/demo_log.rs
expression: otel_logs
snapshot_kind: text
---
- trace_id: "[trace_id:lg0]"
span_id: "[span_id:lg0]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: fake-opentelemetry-collector/src/lib.rs
source: fake-opentelemetry-collector/tests/demo_trace.rs
expression: otel_spans
snapshot_kind: text
---
- trace_id: "[trace_id:lg32]"
span_id: "[span_id:lg16]"
Expand All @@ -23,4 +24,3 @@ expression: otel_spans
status:
message: ""
code: STATUS_CODE_UNSET

0 comments on commit 7a6f339

Please sign in to comment.