Skip to content

Commit

Permalink
Creating Axum Example
Browse files Browse the repository at this point in the history
Want to have a minimal example to test usecase.

Relates #1337
  • Loading branch information
hdost committed Nov 2, 2023
1 parent ed97a1b commit dfd9eb2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ members = [
"examples/metrics-advanced",
"examples/logs-basic",
"examples/traceresponse",
"examples/tracing-axum",
"examples/tracing-grpc",
"stress",
]
Expand Down
18 changes: 18 additions & 0 deletions examples/tracing-axum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "tracing-axum"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.6.20"
opentelemetry = { version = "0.21.0", path = "../../opentelemetry" }
opentelemetry-otlp = { version = "0.13.0", path = "../../opentelemetry-otlp" }
opentelemetry-stdout = { version = "0.1.0", path = "../../opentelemetry-stdout", features = ["trace"] }
opentelemetry_sdk = { version = "0.20.0", path = "../../opentelemetry-sdk" }
tokio = { version = "1.33.0", features = ["rt-multi-thread"] }
tower-http = { version = "0.4.4", features = ["trace"] }
tracing = "0.1.40"
tracing-opentelemetry = "0.21.0"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
81 changes: 81 additions & 0 deletions examples/tracing-axum/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use axum::{
body::Bytes,
extract::MatchedPath,
http::{HeaderMap, Request},
response::{Html, Response},
routing::get,
Router,
};
use opentelemetry::trace::TracerProvider as _;
use std::time::Duration;
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
use tracing::{info_span, Span};
use tracing_subscriber::layer::SubscriberExt;
#[tokio::main]
async fn main() {
let trace_provider = opentelemetry_sdk::trace::Builder::default()
.with_simple_exporter(opentelemetry_stdout::SpanExporter::default())
.build();
let tracer = trace_provider.tracer("Axum Example");
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
tracing_subscriber::registry().with(telemetry);

// build our application with a route
let app = Router::new()
.route("/", get(handler))
// `TraceLayer` is provided by tower-http so you have to add that as a dependency.
// It provides good defaults but is also very customizable.
//
// See https://docs.rs/tower-http/0.1.1/tower_http/trace/index.html for more details.
//
// If you want to customize the behavior using closures here is how.
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &Request<_>| {
// Log the matched route's path (with placeholders not filled in).
// Use request.uri() or OriginalUri if you want the real path.
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);

info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
})
.on_request(|_request: &Request<_>, _span: &Span| {
// You can use `_span.record("some_other_field", value)` in one of these
// closures to attach a value to the initially empty field in the info_span
// created above.
})
.on_response(|_response: &Response, _latency: Duration, _span: &Span| {
// ...
})
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {
// ...
})
.on_eos(
|_trailers: Option<&HeaderMap>, _stream_duration: Duration, _span: &Span| {
// ...
},
)
.on_failure(
|_error: ServerErrorsFailureClass, _latency: Duration, _span: &Span| {
// ...
},
),
);

// Run it
axum::Server::bind(&"127.0.0.1:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}

async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}

0 comments on commit dfd9eb2

Please sign in to comment.