Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request/response traces #397

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions rust-runtime/smithy-http-tower/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@
*/

use crate::SendOperationError;
use pin_project::pin_project;
use smithy_http::body::SdkBody;
use smithy_http::operation;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tower::{BoxError, Layer, Service};

#[pin_project]
pub struct DispatchFuture<F> {
#[pin]
f: F,
}
use tracing::trace;

/// Connects Operation driven middleware to an HTTP implementation.
///
Expand All @@ -27,29 +21,17 @@ pub struct DispatchService<S> {
inner: S,
}

impl<F, T, E> Future for DispatchFuture<F>
where
F: Future<Output = Result<T, E>>,
E: Into<BoxError>,
{
type Output = Result<T, SendOperationError>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
this.f
.poll(cx)
.map_err(|e| SendOperationError::RequestDispatchError(e.into()))
}
}
type BoxedResultFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;

impl<S> Service<operation::Request> for DispatchService<S>
where
S: Service<http::Request<SdkBody>>,
S: Service<http::Request<SdkBody>> + Clone + Send + 'static,
S::Error: Into<BoxError>,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = SendOperationError;
type Future = DispatchFuture<S::Future>;
type Future = BoxedResultFuture<Self::Response, Self::Error>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner
Expand All @@ -59,9 +41,15 @@ where

fn call(&mut self, req: operation::Request) -> Self::Future {
let (req, _property_bag) = req.into_parts();
DispatchFuture {
f: self.inner.call(req),
}
let mut inner = self.inner.clone();
let future = async move {
trace!(request = ?req);
inner
.call(req)
.await
.map_err(|e| SendOperationError::RequestDispatchError(e.into()))
};
Box::pin(future)
}
}

Expand Down
1 change: 1 addition & 0 deletions rust-runtime/smithy-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ http = "0.2.3"
thiserror = "1"
pin-project = "1"
percent-encoding = "2.1.0"
tracing = "0.1.24"

# We are using hyper for our streaming body implementation, but this is an internal detail.
hyper = "0.14.5"
Expand Down
5 changes: 4 additions & 1 deletion rust-runtime/smithy-http/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use bytes::{Buf, Bytes};
use http::Response;
use http_body::Body;
use std::error::Error;
use tracing::trace;

type BoxError = Box<dyn Error + Send + Sync>;

Expand Down Expand Up @@ -78,8 +79,10 @@ where
if let Some(parsed_response) = handler.parse_unloaded(&mut response) {
return sdk_result(parsed_response, response);
}
let (parts, body) = response.into_parts();

trace!(response = ?response);

let (parts, body) = response.into_parts();
let body = match read_body(body).await {
Ok(body) => body,
Err(err) => {
Expand Down