Skip to content

Commit

Permalink
feat: extract trace context from incoming request
Browse files Browse the repository at this point in the history
  • Loading branch information
tronghn committed Nov 14, 2024
1 parent aa76b2d commit 7ca8f76
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ utoipa-swagger-ui = { version = "8.0.3", features = ["axum"] }
tracing = { version = "0.1.40", features = [] }
tracing-opentelemetry = "0.27.0"
opentelemetry = { version = "0.26.0", features = ["metrics", "trace"] }
opentelemetry-http = "0.26.0"
opentelemetry_sdk = { version = "0.26.0", features = ["trace", "rt-tokio"] }
opentelemetry-otlp = { version = "0.26.0", features = ["metrics", "trace"] }
opentelemetry-semantic-conventions = { version = "0.27.0", features = ["semconv_experimental"] }
Expand Down
27 changes: 19 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ use crate::handlers::__path_token_exchange;
use crate::handlers::{introspect, token, token_exchange, HandlerState};
use axum::Router;
use log::info;
use opentelemetry::propagation::TextMapPropagator;
use opentelemetry_http::HeaderExtractor;
use opentelemetry_sdk::propagation::TraceContextPropagator;
use tokio::net::TcpListener;
use tower_http::classify::ServerErrorsFailureClass;
use tower_http::trace::TraceLayer;
use tracing::{info_span, Span};
use tracing_opentelemetry::OpenTelemetrySpanExt;
use utoipa::OpenApi;
use utoipa_axum::router::OpenApiRouter;
use utoipa_axum::routes;
Expand Down Expand Up @@ -66,21 +70,28 @@ impl App {
.get::<MatchedPath>()
.map(MatchedPath::as_str);

info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
// get tracing context from request
let propagator = TraceContextPropagator::new();
let parent_context = propagator.extract(&HeaderExtractor(request.headers()));

let root_span = info_span!(
"http_request",
method = ?request.method(),
matched_path,
);
root_span.set_parent(parent_context.clone());
root_span
})
.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| {
tracing::info!(histogram.http_response_secs = latency.as_secs_f64(), code = %response.status().as_u16());
tracing::info!(monotonic_counter.response_code = 1, response_code = response.status().as_u16());
tracing::info!(
histogram.http_response_secs = latency.as_secs_f64(),
status_code = response.status().as_u16(),
);
})
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {
// ...
Expand Down

0 comments on commit 7ca8f76

Please sign in to comment.