#open-telemetry #mocking #tracing #faker

fake-opentelemetry-collector

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

16 releases (breaking)

new 0.24.0 Nov 17, 2024
0.21.1 Sep 23, 2024
0.19.0 Jun 30, 2024
0.18.0 Mar 9, 2024
0.12.0 Jul 2, 2023

#299 in Testing

Download history 20/week @ 2024-07-29 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

210 downloads per month

CC0 license

21KB
395 lines

fake-opentelemetry-collector

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

Usage example with insta (snapshot testing)

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

Dependencies

~8–18MB
~233K SLoC