Skip to content

Commit bb409d3

Browse files
Restore semantic tracing for HTTP requests (#1550)
Co-authored-by: Louis-Marie Givel <louis-marie.givel@accenture.com>
1 parent 2cd42e2 commit bb409d3

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

josh-proxy/src/bin/josh-proxy.rs

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,12 +1548,14 @@ async fn prepare_namespace(
15481548
}
15491549

15501550
fn trace_http_response_code(trace_span: Span, http_status: StatusCode) {
1551+
use opentelemetry_semantic_conventions::trace::HTTP_RESPONSE_STATUS_CODE;
1552+
15511553
macro_rules! trace {
15521554
($level:expr) => {{
15531555
tracing::event!(
15541556
parent: trace_span,
15551557
$level,
1556-
http_status = http_status.as_u16()
1558+
{ HTTP_RESPONSE_STATUS_CODE } = http_status.as_u16()
15571559
);
15581560
}};
15591561
}
@@ -1591,6 +1593,35 @@ fn make_upstream(remotes: &Vec<cli::Remote>) -> josh::JoshResult<JoshProxyUpstre
15911593
}
15921594
}
15931595

1596+
#[tracing::instrument(skip_all, fields(url.path, http.request.method))]
1597+
async fn handle_http_request(
1598+
proxy_service: Arc<JoshProxyService>,
1599+
req: Request<Incoming>,
1600+
) -> Result<JoshResponse, hyper::http::Error> {
1601+
use opentelemetry_semantic_conventions::trace::{HTTP_REQUEST_METHOD, URL_PATH};
1602+
1603+
let span = tracing::Span::current();
1604+
span.record(URL_PATH, req.uri().path());
1605+
span.record(HTTP_REQUEST_METHOD, req.method().to_string());
1606+
1607+
let r = if let Ok(req_auth) = josh_proxy::auth::strip_auth(req) {
1608+
match call_service(proxy_service, req_auth)
1609+
.instrument(span.clone())
1610+
.await
1611+
{
1612+
Ok(r) => r,
1613+
Err(e) => make_response(&e.0, hyper::StatusCode::INTERNAL_SERVER_ERROR),
1614+
}
1615+
} else {
1616+
make_response(
1617+
"JoshError( strip_auth)",
1618+
hyper::StatusCode::INTERNAL_SERVER_ERROR,
1619+
)
1620+
};
1621+
trace_http_response_code(span.clone(), r.status());
1622+
Ok(r)
1623+
}
1624+
15941625
async fn run_proxy() -> josh::JoshResult<i32> {
15951626
let addr: SocketAddr = format!("[::]:{}", ARGS.port).parse()?;
15961627
let upstream = make_upstream(&ARGS.remote).inspect_err(|e| {
@@ -1646,41 +1677,9 @@ async fn run_proxy() -> josh::JoshResult<i32> {
16461677
.timer(TokioTimer::new())
16471678
.serve_connection(
16481679
io,
1649-
service_fn(move |_req| {
1680+
service_fn(|req| {
16501681
let proxy_service = proxy_service.clone();
1651-
1652-
let _s = tracing::span!(
1653-
tracing::Level::TRACE,
1654-
"http_request",
1655-
path = _req.uri().path().to_string()
1656-
);
1657-
let s = _s;
1658-
1659-
async move {
1660-
let r = if let Ok(req_auth) = josh_proxy::auth::strip_auth(_req) {
1661-
match call_service(proxy_service, req_auth)
1662-
.instrument(s.clone())
1663-
.await
1664-
{
1665-
Ok(r) => r,
1666-
Err(e) => make_response(
1667-
match e {
1668-
JoshError(s) => s,
1669-
}
1670-
.as_str(),
1671-
hyper::StatusCode::INTERNAL_SERVER_ERROR,
1672-
),
1673-
}
1674-
} else {
1675-
make_response(
1676-
"JoshError( strip_auth)",
1677-
hyper::StatusCode::INTERNAL_SERVER_ERROR,
1678-
)
1679-
};
1680-
let _e = s.enter();
1681-
trace_http_response_code(s.clone(), r.status());
1682-
Ok::<_, hyper::http::Error>(r)
1683-
}
1682+
async { handle_http_request(proxy_service, req).await }
16841683
}),
16851684
);
16861685
pin!(conn);

0 commit comments

Comments
 (0)