forked from open-telemetry/opentelemetry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Want to have a minimal example to test usecase. Relates #1337
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>") | ||
} |