Skip to content

Commit

Permalink
Merge pull request #2459 from calebschoepp/improve-outbound-tracing
Browse files Browse the repository at this point in the history
ref(outbound-http): Add a small hack to improve the tracing of outbound http requests through spin core
  • Loading branch information
calebschoepp authored Apr 17, 2024
2 parents 5a449eb + 92bfd89 commit d6b9713
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{path::PathBuf, time::Duration};

use anyhow::Result;
use crossbeam_channel::Sender;
use tracing::instrument;
use tracing::{field::Empty, instrument};
use wasmtime::{InstanceAllocationStrategy, PoolingAllocationConfig};
use wasmtime_wasi::preview2::ResourceTable;
use wasmtime_wasi_http::types::{default_send_request, WasiHttpCtx, WasiHttpView};
Expand Down Expand Up @@ -195,7 +195,7 @@ impl<T: Send + OutboundWasiHttpHandler> WasiHttpView for Data<T> {
&mut self.table
}

#[instrument(name = "start_outbound_http_request", skip_all, fields(otel.kind = "client"))]
#[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))]
fn send_request(
&mut self,
mut request: wasmtime_wasi_http::types::OutgoingRequest,
Expand Down
41 changes: 39 additions & 2 deletions crates/trigger-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use tokio::{
use tracing::{field::Empty, log, Instrument};
use wasmtime_wasi_http::{
body::{HyperIncomingBody as Body, HyperOutgoingBody},
types::HostFutureIncomingResponse,
WasiHttpView,
};

Expand Down Expand Up @@ -569,7 +570,6 @@ impl HttpRuntimeData {
) -> wasmtime::Result<
wasmtime::component::Resource<wasmtime_wasi_http::types::HostFutureIncomingResponse>,
> {
use wasmtime_wasi_http::types::HostFutureIncomingResponse;
use wasmtime_wasi_http::types::IncomingResponseInternal;

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

wasmtime_wasi_http::types::default_send_request(data, request)
let current_span = tracing::Span::current();
let uri = request.request.uri();
if let Some(authority) = uri.authority() {
current_span.record("server.address", authority.host());
if let Some(port) = authority.port() {
current_span.record("server.port", port.as_u16());
}
}

// TODO: This is a temporary workaround to make sure that outbound task is instrumented.
// Once Wasmtime gives us the ability to do the spawn ourselves we can just call .instrument
// and won't have to do this workaround.
let response_handle = wasmtime_wasi_http::types::default_send_request(data, request)?;
let response = data.table().get_mut(&response_handle)?;
*response = match std::mem::replace(response, HostFutureIncomingResponse::Consumed) {
HostFutureIncomingResponse::Pending(handle) => {
HostFutureIncomingResponse::Pending(wasmtime_wasi::preview2::spawn(
async move {
let res: Result<
Result<
wasmtime_wasi_http::types::IncomingResponseInternal,
wasmtime_wasi_http::bindings::http::types::ErrorCode,
>,
anyhow::Error,
> = handle.await;
if let Ok(Ok(res)) = &res {
tracing::Span::current()
.record("http.response.status_code", res.resp.status().as_u16());
}
res
}
.in_current_span(),
))
}
other => other,
};

Ok(response_handle)
}
}

Expand Down

0 comments on commit d6b9713

Please sign in to comment.