Expand description
§fastrace-opentelemetry
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 upThen, run the synchronous example:
cargo run --example synchronousJaeger 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()`.
}Structs§
- Open
Telemetry Reporter - OpenTelemetry reporter for
fastrace.
Constants§
Functions§
- current_
opentelemetry_ context - Returns the OpenTelemetry
SpanContextof the current fastrace local parent span.