#open-telemetry #mocking #tracing #faker #debugging

fake-opentelemetry-collector

A Fake (basic) opentelemetry collector, useful to test what is collected opentelemetry

18 releases (breaking)

new 0.25.1 Nov 24, 2024
0.24.0 Nov 17, 2024
0.19.0 Jun 30, 2024
0.18.0 Mar 9, 2024
0.12.0 Jul 2, 2023

#327 in Testing

Download history 17/week @ 2024-08-23 154/week @ 2024-08-30 12/week @ 2024-09-06 22/week @ 2024-09-13 334/week @ 2024-09-20 52/week @ 2024-09-27 13/week @ 2024-10-04 5/week @ 2024-10-11 129/week @ 2024-11-01 25/week @ 2024-11-08 106/week @ 2024-11-15 232/week @ 2024-11-22

492 downloads per month

CC0 license

19KB
319 lines

fake-opentelemetry-collector

A Fake (basic) opentelemetry collector, useful to test what is collected by opentelemetry

Usage example with insta (snapshot testing)

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

Dependencies

~8–18MB
~236K SLoC