Skip to content

Commit dfd9eb2

Browse files
committed
Creating Axum Example
Want to have a minimal example to test usecase. Relates #1337
1 parent ed97a1b commit dfd9eb2

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ members = [
2828
"examples/metrics-advanced",
2929
"examples/logs-basic",
3030
"examples/traceresponse",
31+
"examples/tracing-axum",
3132
"examples/tracing-grpc",
3233
"stress",
3334
]

Diff for: examples/tracing-axum/Cargo.toml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "tracing-axum"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
axum = "0.6.20"
10+
opentelemetry = { version = "0.21.0", path = "../../opentelemetry" }
11+
opentelemetry-otlp = { version = "0.13.0", path = "../../opentelemetry-otlp" }
12+
opentelemetry-stdout = { version = "0.1.0", path = "../../opentelemetry-stdout", features = ["trace"] }
13+
opentelemetry_sdk = { version = "0.20.0", path = "../../opentelemetry-sdk" }
14+
tokio = { version = "1.33.0", features = ["rt-multi-thread"] }
15+
tower-http = { version = "0.4.4", features = ["trace"] }
16+
tracing = "0.1.40"
17+
tracing-opentelemetry = "0.21.0"
18+
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

Diff for: examples/tracing-axum/src/main.rs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use axum::{
2+
body::Bytes,
3+
extract::MatchedPath,
4+
http::{HeaderMap, Request},
5+
response::{Html, Response},
6+
routing::get,
7+
Router,
8+
};
9+
use opentelemetry::trace::TracerProvider as _;
10+
use std::time::Duration;
11+
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
12+
use tracing::{info_span, Span};
13+
use tracing_subscriber::layer::SubscriberExt;
14+
#[tokio::main]
15+
async fn main() {
16+
let trace_provider = opentelemetry_sdk::trace::Builder::default()
17+
.with_simple_exporter(opentelemetry_stdout::SpanExporter::default())
18+
.build();
19+
let tracer = trace_provider.tracer("Axum Example");
20+
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
21+
tracing_subscriber::registry().with(telemetry);
22+
23+
// build our application with a route
24+
let app = Router::new()
25+
.route("/", get(handler))
26+
// `TraceLayer` is provided by tower-http so you have to add that as a dependency.
27+
// It provides good defaults but is also very customizable.
28+
//
29+
// See https://docs.rs/tower-http/0.1.1/tower_http/trace/index.html for more details.
30+
//
31+
// If you want to customize the behavior using closures here is how.
32+
.layer(
33+
TraceLayer::new_for_http()
34+
.make_span_with(|request: &Request<_>| {
35+
// Log the matched route's path (with placeholders not filled in).
36+
// Use request.uri() or OriginalUri if you want the real path.
37+
let matched_path = request
38+
.extensions()
39+
.get::<MatchedPath>()
40+
.map(MatchedPath::as_str);
41+
42+
info_span!(
43+
"http_request",
44+
method = ?request.method(),
45+
matched_path,
46+
some_other_field = tracing::field::Empty,
47+
)
48+
})
49+
.on_request(|_request: &Request<_>, _span: &Span| {
50+
// You can use `_span.record("some_other_field", value)` in one of these
51+
// closures to attach a value to the initially empty field in the info_span
52+
// created above.
53+
})
54+
.on_response(|_response: &Response, _latency: Duration, _span: &Span| {
55+
// ...
56+
})
57+
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {
58+
// ...
59+
})
60+
.on_eos(
61+
|_trailers: Option<&HeaderMap>, _stream_duration: Duration, _span: &Span| {
62+
// ...
63+
},
64+
)
65+
.on_failure(
66+
|_error: ServerErrorsFailureClass, _latency: Duration, _span: &Span| {
67+
// ...
68+
},
69+
),
70+
);
71+
72+
// Run it
73+
axum::Server::bind(&"127.0.0.1:3000".parse().unwrap())
74+
.serve(app.into_make_service())
75+
.await
76+
.unwrap();
77+
}
78+
79+
async fn handler() -> Html<&'static str> {
80+
Html("<h1>Hello, World!</h1>")
81+
}

0 commit comments

Comments
 (0)