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

fake-opentelemetry-collector

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

18 releases (breaking)

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

#309 in Testing

Download history 159/week @ 2024-08-26 24/week @ 2024-09-02 126/week @ 2024-09-16 264/week @ 2024-09-23 23/week @ 2024-09-30 12/week @ 2024-10-07 1/week @ 2024-10-14 150/week @ 2024-11-04 60/week @ 2024-11-11 191/week @ 2024-11-18 127/week @ 2024-11-25 3/week @ 2024-12-02 38/week @ 2024-12-09

414 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