Crates.io | fastrace-poem |
lib.rs | fastrace-poem |
version | 0.1.0 |
created_at | 2025-03-18 11:51:38.434706+00 |
updated_at | 2025-03-18 11:51:38.434706+00 |
description | A Poem middleware for propagating trace context for fastrace |
homepage | |
repository | https://github.com/fast/fastrace-poem |
max_upload_size | |
id | 1596527 |
size | 80,387 |
Distributed tracing integration for Poem web framework with fastrace.
fastrace-poem
provides middleware for the Poem web framework to enable distributed tracing with automatic context propagation. This helps you track requests as they flow through your microservice architecture, giving you valuable insights for debugging, performance analysis, and system understanding.
Distributed tracing works by passing trace context (trace IDs, span IDs, etc.) between services. This allows individual service traces to be connected into a complete picture of a request's journey through your system.
Context propagation refers to the act of passing this trace context between services. In HTTP-based systems, this is typically done via HTTP headers like traceparent
following the W3C Trace Context specification.
Add the following to your Cargo.toml
:
[dependencies]
fastrace = "0.7"
fastrace-poem = "0.1"
use fastrace::collector::{Config, ConsoleReporter};
use fastrace_poem::FastraceMiddleware;
use poem::{get, handler, EndpointExt, Request, Response, Route, Server};
use poem::listener::TcpListener;
#[handler]
#[fastrace::trace] // Trace individual handlers.
fn ping() -> Response {
Response::builder().body("pong")
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
// Configure fastrace reporter.
fastrace::set_reporter(ConsoleReporter, Config::default());
// Add the FastraceMiddleware to your routes.
let app = Route::new()
.at("/ping", get(ping))
.with(FastraceMiddleware);
Server::new(TcpListener::bind("0.0.0.0:8080"))
.run(app)
.await?;
fastrace::flush();
Ok(())
}
To propagate trace context from clients to your Poem service:
use fastrace::prelude::*;
use fastrace_reqwest::traceparent_headers;
use reqwest::Client;
#[fastrace::trace]
async fn send_request() {
let client = Client::new();
let response = client
.get("http://your-poem-service/endpoint")
.headers(traceparent_headers()) // Adds traceparent header.
.send()
.await
.unwrap();
// Process response...
}
traceparent
header.Check out the examples directory for complete working examples showing:
client.rs
- How to send requests with trace contextserver.rs
- How to receive and process trace context using fastrace-poem
To run the examples:
# First start the server
cargo run --example server
# Then in another terminal, run the client
cargo run --example client
This project is licensed under the Apache-2.0 license.