Skip to content

Commit 1f69685

Browse files
authored
chore: add server example (#2)
1 parent 99c57f0 commit 1f69685

File tree

4 files changed

+127
-38
lines changed

4 files changed

+127
-38
lines changed

Cargo.lock

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ fastrace = "0.7"
1717
reqwest = "0.12"
1818

1919
[dev-dependencies]
20+
axum = "0.8"
2021
fastrace = { version = "0.7", features = ["enable"] }
22+
fastrace-axum = { git = "https://github.com/fast/fastrace-axum" }
2123
tokio = { version = "1.44", features = ["full"] }

examples/client.rs

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,22 @@
1-
//! Example demonstrating how to use fastrace-reqwest to propagate trace context in HTTP client
2-
//! requests.
3-
//!
4-
//! This example shows:
5-
//! 1. Setting up a fastrace reporter.
6-
//! 2. Creating a root span.
7-
//! 3. Sending an HTTP request with trace context propagation.
8-
//!
9-
//! To run this example:
10-
//! First start the server example: `cargo run --example server`
11-
//! Then run this client: `cargo run --example client`
12-
131
use fastrace::collector::Config;
142
use fastrace::collector::ConsoleReporter;
15-
use fastrace::prelude::*;
16-
use fastrace_reqwest::traceparent_headers;
17-
use reqwest::Client;
183

194
#[tokio::main]
205
async fn main() {
21-
// Initialize fastrace with the console reporter.
6+
// Configurate fastrace reporter.
227
fastrace::set_reporter(ConsoleReporter, Config::default());
238

24-
{
25-
// Create a root span for the client operation.
26-
let root = Span::root("client".to_string(), SpanContext::random());
27-
let _g = root.set_local_parent();
28-
29-
send_request().await;
30-
}
9+
let app = axum::Router::new()
10+
.route("/ping", axum::routing::get(ping))
11+
// Add a the FastraceLayer to routes.
12+
// The layer extracts trace context from incoming requests.
13+
.layer(fastrace_axum::FastraceLayer);
3114

32-
// Flush any remaining traces before the program exits.
33-
fastrace::flush();
15+
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
16+
axum::serve(listener, app).await.unwrap();
3417
}
3518

36-
/// Send an HTTP request to the server with trace context propagation.
37-
/// The traceparent_headers() function adds the trace context to the request headers.
38-
#[fastrace::trace]
39-
async fn send_request() {
40-
let client = Client::new();
41-
let response = client
42-
.get("http://localhost:8080/ping")
43-
.headers(traceparent_headers())
44-
.send()
45-
.await
46-
.unwrap();
47-
assert!(response.status().is_success());
48-
println!("{:?}", response.text().await.unwrap());
19+
#[fastrace::trace] // Trace individual handlers.
20+
async fn ping() -> &'static str {
21+
"pong"
4922
}

examples/server.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use fastrace::collector::Config;
2+
use fastrace::collector::ConsoleReporter;
3+
4+
#[tokio::main]
5+
async fn main() {
6+
// Initialize fastrace with the console reporter.
7+
fastrace::set_reporter(ConsoleReporter, Config::default());
8+
9+
let app = axum::Router::new()
10+
.route("/ping", axum::routing::get(ping))
11+
// Add a the FastraceLayer to routes.
12+
// The layer extracts trace context from incoming requests.
13+
.layer(fastrace_axum::FastraceLayer);
14+
15+
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
16+
axum::serve(listener, app).await.unwrap();
17+
}
18+
19+
#[fastrace::trace]
20+
async fn ping() -> &'static str {
21+
"pong"
22+
}

0 commit comments

Comments
 (0)