Skip to content

Commit d6b9713

Browse files
authored
Merge pull request #2459 from calebschoepp/improve-outbound-tracing
ref(outbound-http): Add a small hack to improve the tracing of outbound http requests through spin core
2 parents 5a449eb + 92bfd89 commit d6b9713

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

crates/core/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{path::PathBuf, time::Duration};
1919

2020
use anyhow::Result;
2121
use crossbeam_channel::Sender;
22-
use tracing::instrument;
22+
use tracing::{field::Empty, instrument};
2323
use wasmtime::{InstanceAllocationStrategy, PoolingAllocationConfig};
2424
use wasmtime_wasi::preview2::ResourceTable;
2525
use wasmtime_wasi_http::types::{default_send_request, WasiHttpCtx, WasiHttpView};
@@ -195,7 +195,7 @@ impl<T: Send + OutboundWasiHttpHandler> WasiHttpView for Data<T> {
195195
&mut self.table
196196
}
197197

198-
#[instrument(name = "start_outbound_http_request", skip_all, fields(otel.kind = "client"))]
198+
#[instrument(name = "spin_core.send_request", skip_all, fields(otel.kind = "client", url.full = %request.request.uri(), http.request.method = %request.request.method(), otel.name = %request.request.method(), http.response.status_code = Empty, server.address = Empty, server.port = Empty))]
199199
fn send_request(
200200
&mut self,
201201
mut request: wasmtime_wasi_http::types::OutgoingRequest,

crates/trigger-http/src/lib.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use tokio::{
4747
use tracing::{field::Empty, log, Instrument};
4848
use wasmtime_wasi_http::{
4949
body::{HyperIncomingBody as Body, HyperOutgoingBody},
50+
types::HostFutureIncomingResponse,
5051
WasiHttpView,
5152
};
5253

@@ -569,7 +570,6 @@ impl HttpRuntimeData {
569570
) -> wasmtime::Result<
570571
wasmtime::component::Resource<wasmtime_wasi_http::types::HostFutureIncomingResponse>,
571572
> {
572-
use wasmtime_wasi_http::types::HostFutureIncomingResponse;
573573
use wasmtime_wasi_http::types::IncomingResponseInternal;
574574

575575
let this = data.as_ref();
@@ -706,7 +706,44 @@ impl OutboundWasiHttpHandler for HttpRuntimeData {
706706
return Self::chain_request(data, request, component_id);
707707
}
708708

709-
wasmtime_wasi_http::types::default_send_request(data, request)
709+
let current_span = tracing::Span::current();
710+
let uri = request.request.uri();
711+
if let Some(authority) = uri.authority() {
712+
current_span.record("server.address", authority.host());
713+
if let Some(port) = authority.port() {
714+
current_span.record("server.port", port.as_u16());
715+
}
716+
}
717+
718+
// TODO: This is a temporary workaround to make sure that outbound task is instrumented.
719+
// Once Wasmtime gives us the ability to do the spawn ourselves we can just call .instrument
720+
// and won't have to do this workaround.
721+
let response_handle = wasmtime_wasi_http::types::default_send_request(data, request)?;
722+
let response = data.table().get_mut(&response_handle)?;
723+
*response = match std::mem::replace(response, HostFutureIncomingResponse::Consumed) {
724+
HostFutureIncomingResponse::Pending(handle) => {
725+
HostFutureIncomingResponse::Pending(wasmtime_wasi::preview2::spawn(
726+
async move {
727+
let res: Result<
728+
Result<
729+
wasmtime_wasi_http::types::IncomingResponseInternal,
730+
wasmtime_wasi_http::bindings::http::types::ErrorCode,
731+
>,
732+
anyhow::Error,
733+
> = handle.await;
734+
if let Ok(Ok(res)) = &res {
735+
tracing::Span::current()
736+
.record("http.response.status_code", res.resp.status().as_u16());
737+
}
738+
res
739+
}
740+
.in_current_span(),
741+
))
742+
}
743+
other => other,
744+
};
745+
746+
Ok(response_handle)
710747
}
711748
}
712749

0 commit comments

Comments
 (0)