### Install cargo-insta Source: https://github.com/fast/fastrace/blob/main/CONTRIBUTING.md Command to install the cargo-insta tool for snapshot testing. ```bash cargo install cargo-insta ``` -------------------------------- ### Setup OpenTelemetry Collector Source: https://github.com/fast/fastrace/blob/main/crates/fastrace-opentelemetry/README.md Start the OpenTelemetry Collector using Docker Compose to receive traces from Jaeger and Zipkin. ```shell docker compose -f dev/docker-compose.yaml up ``` -------------------------------- ### Run Synchronous Example Source: https://github.com/fast/fastrace/blob/main/crates/fastrace-opentelemetry/README.md Execute the synchronous example provided by Fastrace to test trace reporting. ```shell cargo run --example synchronous ``` -------------------------------- ### Install Dependencies and Benchmark Fastrace Source: https://github.com/fast/fastrace/blob/main/benches/results/README.md Installs necessary system packages, Rust, and the cargo-criterion tool. Clones the Fastrace repository and runs the comparison benchmark, saving the output to a file. ```shell sudo apt update sudo apt install build-essential libssl-dev pkg-config -y curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh source $HOME/.cargo/env cargo install --version=1.0.0-alpha3 cargo-criterion git clone https://github.com/fast/fastrace.git cd fastrace cargo criterion compare --message-format=json | grep "benchmark-complete" > compare-xxx.txt ``` -------------------------------- ### Create a Root Span and Local Context Source: https://github.com/fast/fastrace/blob/main/README.md Use `Span::root` to start a new trace with a randomly generated context and `Span::set_local_parent` to establish a local tracing context for the current thread. The `func_path!()` macro automatically captures the current function's name for the root span. ```rust use fastrace::prelude::*; pub fn send_request(req: HttpRequest) -> Result<(), Error> { let root = Span::root(func_path!(), SpanContext::random()); let _guard = root.set_local_parent(); // ... } ``` -------------------------------- ### Initialize OpenTelemetry Reporter Source: https://github.com/fast/fastrace/blob/main/crates/fastrace-opentelemetry/README.md Configure and initialize the OpenTelemetryReporter with an OTLP exporter endpoint and resource attributes. This reporter is then set as the global reporter for Fastrace. ```rust 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(); ``` -------------------------------- ### Add Fastrace and OpenTelemetry Dependencies Source: https://github.com/fast/fastrace/blob/main/crates/fastrace-opentelemetry/README.md Include the necessary dependencies in your Cargo.toml file to use Fastrace and its OpenTelemetry reporter. ```toml [dependencies] fastrace = { version = "0.7", features = ["enable"] } fastrace-opentelemetry = "0.18" ``` -------------------------------- ### Configure Async OpenTelemetry Exporter with Tokio Source: https://github.com/fast/fastrace/blob/main/crates/fastrace-opentelemetry/README.md Adapt the OpenTelemetryReporter to use an async exporter, such as OTLP HTTP with reqwest, by providing a Tokio runtime handle for blocking operations. ```rust let handle = tokio::runtime::Handle::current(); let reporter = OpenTelemetryReporter::new(exporter, resource, instrumentation_scope) .with_block_on(move |future| handle.block_on(future)); ``` -------------------------------- ### Common Cargo Commands Source: https://github.com/fast/fastrace/blob/main/CONTRIBUTING.md Standard commands for analyzing, building, linting, and testing the Rust project. ```bash cargo check ``` ```bash cargo build ``` ```bash cargo clippy ``` ```bash cargo test ``` ```bash cargo bench ``` -------------------------------- ### Initialize Reporter and Collect Spans Source: https://github.com/fast/fastrace/blob/main/README.md Initialize a ConsoleReporter early in your application and ensure flush() is called before termination. Root spans are recommended for short tasks to ensure traces are reported. ```rust use fastrace::collector::Config; use fastrace::collector::ConsoleReporter; use fastrace::prelude::*; fn main() { fastrace::set_reporter(ConsoleReporter, Config::default()); loop { let root = Span::root("worker-loop", SpanContext::random()); let _guard = root.set_local_parent(); handle_request(); } fastrace::flush(); } ``` -------------------------------- ### Add Fastrace Dependency Source: https://github.com/fast/fastrace/blob/main/README.md Include fastrace as a dependency in your Cargo.toml file with the 'enable' feature. ```toml [dependencies] fastrace = { version = "0.7", features = ["enable"] } ``` -------------------------------- ### Add Fastrace Dependency to Cargo.toml Source: https://github.com/fast/fastrace/blob/main/README.md Include `fastrace` as a dependency in your library's `Cargo.toml` file. No extra features are required for basic integration. ```toml [dependencies] fastrace = "0.7" ``` -------------------------------- ### Run All Targets/Workspace Source: https://github.com/fast/fastrace/blob/main/CONTRIBUTING.md Execute cargo commands across all targets and the entire workspace. ```bash cargo --all-targets --workspace ``` -------------------------------- ### Migrate from tokio-tracing to Fastrace Source: https://github.com/fast/fastrace/blob/main/README.md Use this snippet to capture spans from libraries instrumented with tokio-tracing when migrating to fastrace. It requires the fastrace-tracing crate. ```rust let subscriber = tracing_subscriber::Registry::default().with(fastrace_tracing::FastraceCompatLayer::new()); tracing::subscriber::set_global_default(subscriber).unwrap(); ``` -------------------------------- ### Bridge Fastrace Span to OpenTelemetry Context Source: https://github.com/fast/fastrace/blob/main/crates/fastrace-opentelemetry/README.md Activate the current Fastrace local parent span as an OpenTelemetry context. This is useful when integrating with libraries that rely on OpenTelemetry's `Context::current()`. ```rust 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()`. } ``` -------------------------------- ### Trace a Function with #[fastrace::trace] Source: https://github.com/fast/fastrace/blob/main/README.md Apply the `#[fastrace::trace]` attribute to a function to automatically collect `SpanRecord` data when the function is called within an active tracing context. ```rust #[fastrace::trace] pub fn send_request(req: HttpRequest) -> Result<(), Error> { // ... } ``` -------------------------------- ### Test Specific Function Source: https://github.com/fast/fastrace/blob/main/CONTRIBUTING.md Run cargo tests targeting a specific function or test name. ```bash cargo test multiple_local_parent ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.