forked from lavina/lavina
1
0
Fork 0

continue propagated traces in http request handlers

This commit is contained in:
Nikita Vilunov 2024-05-04 01:59:43 +02:00
parent 84eb901042
commit 1bb818a8e5
1 changed files with 25 additions and 0 deletions

View File

@ -74,12 +74,14 @@ async fn main_loop(
Ok(())
}
#[tracing::instrument(skip_all)]
async fn route(
registry: &MetricsRegistry,
core: &LavinaCore,
storage: &Storage,
request: Request<hyper::body::Incoming>,
) -> HttpResult<Response<Full<Bytes>>> {
propagade_span_from_headers(&request);
let res = match (request.method(), request.uri().path()) {
(&Method::GET, "/metrics") => endpoint_metrics(registry),
(&Method::GET, "/rooms") => endpoint_rooms(&core.rooms).await,
@ -161,6 +163,7 @@ async fn endpoint_set_password(
Ok(empty_204_request())
}
#[tracing::instrument(skip_all)]
async fn endpoint_send_room_message(
request: Request<hyper::body::Incoming>,
core: &LavinaCore,
@ -183,6 +186,7 @@ async fn endpoint_send_room_message(
}
}
#[tracing::instrument(skip_all)]
async fn endpoint_set_room_topic(
request: Request<hyper::body::Incoming>,
core: &LavinaCore,
@ -282,3 +286,24 @@ where
Full::new(Bytes::from(buffer))
}
}
fn propagade_span_from_headers<T>(req: &Request<T>) {
use opentelemetry::propagation::Extractor;
use tracing::Span;
use tracing_opentelemetry::OpenTelemetrySpanExt;
struct HttpReqExtractor<'a, T> {
req: &'a Request<T>,
}
impl<'a, T> Extractor for HttpReqExtractor<'a, T> {
fn get(&self, key: &str) -> Option<&str> {
self.req.headers().get(key).and_then(|v| v.to_str().ok())
}
fn keys(&self) -> Vec<&str> {
self.req.headers().keys().map(|k| k.as_str()).collect()
}
}
let ctx = opentelemetry::global::get_text_map_propagator(|pp| pp.extract(&HttpReqExtractor { req }));
Span::current().set_parent(ctx);
}