fastrace-opentelemetry

Crates.iofastrace-opentelemetry
lib.rsfastrace-opentelemetry
version0.15.1
created_at2024-07-16 10:10:51.779811+00
updated_at2025-12-18 13:39:48.807594+00
descriptionOpentelemetry reporter for fastrace
homepage
repositoryhttps://github.com/fast/fastrace
max_upload_size
id1304906
size32,731
tison (tisonkun)

documentation

https://docs.rs/fastrace-opentelemetry

README

fastrace-opentelemetry

Documentation Crates.io LICENSE

OpenTelemetry reporter for fastrace.

Dependencies

[dependencies]
fastrace = { version = "0.7", features = ["enable"] }
fastrace-opentelemetry = "0.15"

Setup OpenTelemetry Collector

Start OpenTelemetry Collector with Jaeger and Zipkin receivers:

docker compose -f dev/docker-compose.yaml up

Then, run the synchronous example:

cargo run --example synchronous

Jaeger UI is available on http://127.0.0.1:16686/

Zipkin UI is available on http://127.0.0.1:9411/

Report to OpenTelemetry Collector

use std::borrow::Cow;

use fastrace::collector::Config;
use fastrace::prelude::*;
use fastrace_opentelemetry::OpenTelemetryReporter;
use opentelemetry::InstrumentationScope;
use opentelemetry::KeyValue;
use opentelemetry_otlp::SpanExporter;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::Resource;

// Initialize reporter
let reporter = OpenTelemetryReporter::new(
    SpanExporter::builder()
        .with_tonic()
        .with_endpoint("http://127.0.0.1:4317".to_string())
        .with_protocol(opentelemetry_otlp::Protocol::Grpc)
        .with_timeout(opentelemetry_otlp::OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT)
        .build()
        .expect("initialize otlp exporter"),
    Cow::Owned(
        Resource::builder()
            .with_attributes([KeyValue::new("service.name", "asynchronous")])
            .build()
    ),
    InstrumentationScope::builder("example-crate").with_version(env!("CARGO_PKG_VERSION")).build(),
);
fastrace::set_reporter(reporter, Config::default());

{
    // Start tracing
    let root = Span::root("root", SpanContext::random());
}

fastrace::flush();

Activate OpenTelemetry Trace Context

If you use fastrace spans but also depend on libraries that expect an OpenTelemetry parent Context, you can bridge the current fastrace local parent into an OpenTelemetry context.

This requires a local parent to be set for the current thread (e.g. via Span::set_local_parent).

use fastrace::prelude::*;
use fastrace_opentelemetry::current_opentelemetry_context;
use opentelemetry::trace::TraceContextExt;
use opentelemetry::Context;

fn main() {
    let span = Span::root("root", SpanContext::random());
    let _guard = span.set_local_parent();

    let _otel_guard = current_opentelemetry_context()
        .map(|cx| Context::current().with_remote_span_context(cx).attach());

    // Call library code that uses `Context::current()`.
}
Commit count: 256

cargo fmt