Skip to content
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
30 changes: 15 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion volo-http/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ volo-http/src/
├── response.rs # Response type alias
├── context/ # RPC contexts
│ ├── client.rs # ClientContext (target, stats, timeout)
│ └── server.rs # ServerContext (RpcInfo, path params, extensions)
│ ├── server.rs # ServerContext (RpcInfo, path params, extensions)
│ └── stat.rs # CommonStats
├── error/
│ ├── client.rs # ClientError
│ └── server.rs # ExtractBodyError
Expand Down
4 changes: 2 additions & 2 deletions volo-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "volo-http"
version = "0.5.4"
version = "0.5.5"
edition.workspace = true
homepage.workspace = true
repository.workspace = true
Expand Down Expand Up @@ -117,7 +117,7 @@ client = [
] # client core
server = [
"hyper-util/server",
"dep:ipnet", "dep:matchit", "dep:memchr", "dep:scopeguard", "dep:mime_guess",
"dep:ipnet", "dep:matchit", "dep:memchr", "dep:scopeguard", "dep:mime_guess", "dep:chrono",
] # server core

__serde = ["dep:serde"] # a private feature for enabling `serde` by `serde_xxx`
Expand Down
3 changes: 3 additions & 0 deletions volo-http/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pub mod server;

#[cfg(feature = "server")]
pub use self::server::ServerContext;

#[cfg(all(feature = "client", feature = "server"))]
pub mod stat;
31 changes: 30 additions & 1 deletion volo-http/src/context/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Context and its utilities of server

use chrono::{DateTime, Local};
use volo::{
context::{Context, Reusable, Role, RpcCx, RpcInfo},
net::Address,
Expand All @@ -8,7 +9,7 @@ use volo::{

use crate::{
server::param::PathParamsVec,
utils::macros::{impl_deref_and_deref_mut, impl_getter},
utils::macros::{impl_deref_and_deref_mut, impl_getter, stat_impl},
};

/// RPC context of http server
Expand Down Expand Up @@ -44,10 +45,38 @@ pub struct ServerCxInner {
/// [`PathParamsMap`]: crate::server::param::PathParamsMap
/// [`PathParams`]: crate::server::param::PathParams
pub params: PathParamsVec,

/// Statistics of the request
pub stats: ServerStats,
}

impl ServerCxInner {
impl_getter!(params, PathParamsVec);
impl_getter!(stats, ServerStats);
}

/// Statistics of server
#[derive(Debug, Default, Clone)]
pub struct ServerStats {
read_header_start: Option<DateTime<Local>>,
read_header_finish: Option<DateTime<Local>>,
read_body_start: Option<DateTime<Local>>,
read_body_finish: Option<DateTime<Local>>,
handle_start: Option<DateTime<Local>>,
handle_finish: Option<DateTime<Local>>,
write_start: Option<DateTime<Local>>,
write_finish: Option<DateTime<Local>>,
}

impl ServerStats {
stat_impl!(read_header_start);
stat_impl!(read_header_finish);
stat_impl!(read_body_start);
stat_impl!(read_body_finish);
stat_impl!(handle_start);
stat_impl!(handle_finish);
stat_impl!(write_start);
stat_impl!(write_finish);
}

/// Configuration of the request
Expand Down
34 changes: 34 additions & 0 deletions volo-http/src/context/stat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! HTTP request and response statistics shared across client and server contexts.

use chrono::{DateTime, Local};
use http::{method::Method, status::StatusCode, uri::Uri};

/// Shared HTTP statistics captured for every request on both client and server sides
#[derive(Debug, Default, Clone)]
pub struct CommonStats {
/// The time at which request processing began
pub process_start_time: DateTime<Local>,

/// The time at which request processing completed
pub process_end_time: DateTime<Local>,

/// The HTTP method of the request (e.g. `GET`, `POST`)
pub method: Method,

/// The full URI of the request
pub uri: Uri,

/// The HTTP status code of the response.
///
/// Status code may be None if the service failed
pub status_code: Option<StatusCode>,

/// Size of the request body in bytes
pub req_size: i64,

/// Size of the response body in bytes
pub resp_size: i64,

/// Whether the request resulted in an error
pub is_error: bool,
}
5 changes: 4 additions & 1 deletion volo-http/src/server/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ macro_rules! impl_handler {
Ok(value) => value,
Err(rejection) => return rejection.into_response(),
};
self($($ty,)* $last).await.into_response()
cx.stats.record_handle_start();
let result = self($($ty,)* $last).await;
cx.stats.record_handle_finish();
result.into_response()
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion volo-http/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ where
let mut cx = ServerContext::new(service.peer);
cx.rpc_info_mut().set_config(service.config);
let span = service.span_provider.on_serve(&cx);
let resp = service
let resp: http::Response<Body> = service
.inner
.call(&mut cx, req.map(Body::from_incoming))
.instrument(span)
Expand Down
4 changes: 2 additions & 2 deletions volo-http/src/utils/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ macro_rules! impl_getter {
#[cfg(feature = "server")]
pub(crate) use impl_getter;

#[cfg(feature = "client")]
#[cfg(any(feature = "client", feature = "server"))]
macro_rules! stat_impl {
($t: ident) => {
paste::paste! {
Expand All @@ -119,5 +119,5 @@ macro_rules! stat_impl {
}
};
}
#[cfg(feature = "client")]
#[cfg(any(feature = "client", feature = "server"))]
pub(crate) use stat_impl;
4 changes: 2 additions & 2 deletions volo-thrift/src/codec/default/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ mod tests {
}
}

#[cfg(feature = "shmipc")]
#[cfg(all(feature = "shmipc", target_os = "linux"))]
#[tokio::test]
async fn test_decode_unexpected_eof_returns_none_when_shmipc_available() {
let (_env, stream) = ShmipcTestEnv::new().await;
Expand All @@ -562,7 +562,7 @@ mod tests {
assert!(result.unwrap().is_none());
}

#[cfg(feature = "shmipc")]
#[cfg(all(feature = "shmipc", target_os = "linux"))]
#[tokio::test]
async fn test_decode_other_error_returns_error_when_shmipc_available() {
let (_env, stream) = ShmipcTestEnv::new().await;
Expand Down
Loading