Skip to content

Commit 753b0d9

Browse files
committed
ref(outbound-http): Add a small hack to improve the tracing of outbound http requests through spin core
Signed-off-by: Caleb Schoepp <[email protected]>
1 parent 2efad6a commit 753b0d9

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

crates/core/src/lib.rs

Lines changed: 11 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().to_string(), http.request.method = request.request.method().to_string(), otel.name = request.request.method().to_string(), 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,
@@ -205,6 +205,15 @@ impl<T: Send + OutboundWasiHttpHandler> WasiHttpView for Data<T> {
205205
where
206206
Self: Sized,
207207
{
208+
let current_span = tracing::Span::current();
209+
let uri = request.request.uri();
210+
if let Some(authority) = uri.authority() {
211+
current_span.record("server.address", authority.host());
212+
if let Some(port) = authority.port() {
213+
current_span.record("server.port", port.as_u16());
214+
}
215+
}
216+
208217
spin_telemetry::inject_trace_context(&mut request.request);
209218
T::send_request(self, request)
210219
}

crates/trigger-http/src/lib.rs

Lines changed: 30 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,35 @@ 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+
// TODO: This is a temporary workaround to make sure that outbound task is instrumented.
710+
// Once Wasmtime gives us the ability to do the spawn ourselves we can just call .instrument
711+
// and won't have to do this workaround.
712+
let response_handle = wasmtime_wasi_http::types::default_send_request(data, request)?;
713+
let response = data.table().get_mut(&response_handle)?;
714+
*response = match std::mem::replace(response, HostFutureIncomingResponse::Consumed) {
715+
HostFutureIncomingResponse::Pending(handle) => {
716+
HostFutureIncomingResponse::Pending(wasmtime_wasi::preview2::spawn(
717+
async move {
718+
let res: std::prelude::v1::Result<
719+
std::prelude::v1::Result<
720+
wasmtime_wasi_http::types::IncomingResponseInternal,
721+
wasmtime_wasi_http::bindings::http::types::ErrorCode,
722+
>,
723+
anyhow::Error,
724+
> = handle.await;
725+
if let Ok(Ok(res)) = &res {
726+
tracing::Span::current()
727+
.record("http.response.status_code", res.resp.status().as_u16());
728+
}
729+
res
730+
}
731+
.instrument(tracing::Span::current()),
732+
))
733+
}
734+
other => other,
735+
};
736+
737+
Ok(response_handle)
710738
}
711739
}
712740

0 commit comments

Comments
 (0)