-
Notifications
You must be signed in to change notification settings - Fork 196
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 minimal tracing to server framework #1314
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ class ServerOperationHandlerGenerator( | |
"SmithyHttpServer" to ServerCargoDependency.SmithyHttpServer(runtimeConfig).asType(), | ||
"Phantom" to ServerRuntimeType.Phantom, | ||
"ServerOperationHandler" to ServerRuntimeType.serverOperationHandler(runtimeConfig), | ||
"Tracing" to ServerCargoDependency.Tracing.asType(), | ||
"http" to RuntimeType.http, | ||
) | ||
|
||
|
@@ -78,41 +79,62 @@ class ServerOperationHandlerGenerator( | |
""".trimIndent(), | ||
*codegenScope | ||
) { | ||
// Instrument operation handler at callsite. | ||
val operationHandlerInvoke = if (state) { | ||
"self(input_inner, state)" | ||
} else { | ||
"self(input_inner)" | ||
} | ||
val operationHandlerCall = | ||
""" | ||
let input_inner = input_wrapper.into(); | ||
#{Tracing}::debug!(input = ?input_inner, "calling operation handler"); | ||
let output_inner = $operationHandlerInvoke | ||
.instrument(#{Tracing}::debug_span!("${operationName}_handler")) | ||
.await; | ||
#{Tracing}::debug!(output = ?output_inner, "operation handler returned"); | ||
""" | ||
val callImpl = if (state) { | ||
""" | ||
let state = match $serverCrate::extension::extract_extension(&mut req).await { | ||
Ok(v) => v, | ||
Err(extension_not_found_rejection) => { | ||
#{Tracing}::error!(?extension_not_found_rejection, "unable to extract extension from request; maybe you forgot to register it with `AddExtensionLayer`?"); | ||
let extension = $serverCrate::extension::RuntimeErrorExtension::new(extension_not_found_rejection.to_string()); | ||
let runtime_error = $serverCrate::runtime_error::RuntimeError { | ||
protocol: #{SmithyHttpServer}::protocols::Protocol::${protocol.name.toPascalCase()}, | ||
kind: extension_not_found_rejection.into(), | ||
}; | ||
let mut response = runtime_error.into_response(); | ||
response.extensions_mut().insert(extension); | ||
return response.map($serverCrate::body::boxed); | ||
let response = response.map($serverCrate::body::boxed); | ||
#{Tracing}::debug!(?response, "returning HTTP response"); | ||
return response; | ||
} | ||
}; | ||
let input_inner = input_wrapper.into(); | ||
let output_inner = self(input_inner, state).await; | ||
""".trimIndent() | ||
} else { | ||
$operationHandlerCall | ||
""" | ||
let input_inner = input_wrapper.into(); | ||
let output_inner = self(input_inner).await; | ||
""".trimIndent() | ||
} else { | ||
operationHandlerCall | ||
} | ||
rustTemplate( | ||
""" | ||
type Sealed = #{ServerOperationHandler}::sealed::Hidden; | ||
|
||
##[#{Tracing}::instrument(level = "debug", skip_all, name = "${operationName}_service_call")] | ||
async fn call(self, req: #{http}::Request<B>) -> #{http}::Response<#{SmithyHttpServer}::body::BoxBody> { | ||
use #{Tracing}::Instrument; | ||
|
||
let mut req = #{AxumCore}::extract::RequestParts::new(req); | ||
use #{AxumCore}::extract::FromRequest; | ||
use #{AxumCore}::response::IntoResponse; | ||
let input_wrapper = match $inputWrapperName::from_request(&mut req).await { | ||
Ok(v) => v, | ||
Err(runtime_error) => { | ||
return runtime_error.into_response().map($serverCrate::body::boxed); | ||
#{Tracing}::debug!(?runtime_error, "unable to extract operation input from request"); | ||
let response = runtime_error.into_response().map($serverCrate::body::boxed); | ||
#{Tracing}::debug!(?response, "returning HTTP response"); | ||
return response; | ||
} | ||
}; | ||
$callImpl | ||
|
@@ -121,7 +143,9 @@ class ServerOperationHandlerGenerator( | |
response.extensions_mut().insert( | ||
#{SmithyHttpServer}::extension::OperationExtension::new("${operation.id.namespace}", "$operationName") | ||
); | ||
response.map(#{SmithyHttpServer}::body::boxed) | ||
response = response.map(#{SmithyHttpServer}::body::boxed); | ||
#{Tracing}::debug!(?response, "returning HTTP response"); | ||
response | ||
} | ||
""", | ||
*codegenScope | ||
|
@@ -157,6 +181,7 @@ class ServerOperationHandlerGenerator( | |
Fut: std::future::Future<Output = $outputType> + Send, | ||
B: $serverCrate::body::HttpBody + Send + 'static, $streamingBodyTraitBounds | ||
B::Data: Send, | ||
B: std::fmt::Debug, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like adding debug bounds like this since sometimes it can be painful. I think we should avoid maybe logging bodies? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless its an error then I would try to convert the body into a string to print that maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking of logging them at |
||
$serverCrate::rejection::RequestRejection: From<<B as $serverCrate::body::HttpBody>::Error> | ||
""".trimIndent() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Response bodies should probably only be logged at
TRACE
level, since they can be very big.Same for request bodies, which I'm not logging anywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most boxed types don't have a real debug impl https://docs.rs/http-body/latest/src/http_body/combinators/box_body.rs.html#32-36 so is this actually useful to print the body?