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

feat(logging): add a tracing span per JSON-RPC call #722

Merged
merged 19 commits into from
Jun 21, 2022

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Apr 4, 2022

Essentially this PR downgrades to only print request and response bodies when trace! is enabled.

In addition I have added a tracing span per call so it's possible to just trace specific method calls which could be quite useful I reckon.

For batches it's possible to just log/trace by the target batch because the span is called batch so not possible to trace the individual calls inside it.

The main motivation for tracing spans is for the servers where this can be super useful.

Example WS

2022-05-27T16:02:00.688793Z DEBUG jsonrpsee_client_transport::ws: Connecting to target: Target { sockaddrs: [127.0.0.1:35045], host: "127.0.0.1", host_header: "127.0.0.1:35045", _mode: Plain, path_and_query: "/" }
2022-05-27T16:02:00.689003Z  INFO jsonrpsee_ws_server::server: Accepting new connection 1/100
2022-05-27T16:02:00.689562Z  INFO jsonrpsee_client_transport::ws: Connection established to target: Target { sockaddrs: [], host: "127.0.0.1", host_header: "127.0.0.1:35045", _mode: Plain, path_and_query: "/" }
2022-05-27T16:02:00.689759Z TRACE method_call{method=say_hello}: jsonrpsee_core::tracing: send="{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"say_hello\"}"
2022-05-27T16:02:00.689826Z TRACE jsonrpsee_client_transport::ws: send: {"jsonrpc":"2.0","id":0,"method":"say_hello"}
2022-05-27T16:02:00.690105Z TRACE method_call{method=say_hello}: jsonrpsee_core::tracing: recv="{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"say_hello\",\"params\":null}"
2022-05-27T16:02:00.690160Z TRACE method_call{method=say_hello}: jsonrpsee_core::tracing: send="{\"jsonrpc\":\"2.0\",\"result\":\"lo\",\"id\":0}"
2022-05-27T16:02:00.690394Z TRACE method_call{method=say_hello}: jsonrpsee_core::tracing: recv="{\"jsonrpc\":\"2.0\",\"result\":\"lo\",\"id\":0}"

Example HTTP

2022-05-27T15:30:09.114372Z TRACE method_call{method=say_hello}: jsonrpsee_core::tracing: send="{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"say_hello\",\"params\":[1,2,3]}"
2022-05-27T15:30:09.114518Z TRACE method_call{method=say_hello}: hyper::client::pool: checkout waiting for idle connection: ("http", 127.0.0.1:45057)
2022-05-27T15:30:09.114600Z TRACE method_call{method=say_hello}: hyper::client::connect::http: Http::connect; scheme=Some("http"), host=Some("127.0.0.1"), port=Some(Port(45057))
2022-05-27T15:30:09.114653Z DEBUG method_call{method=say_hello}: hyper::client::connect::http: connecting to 127.0.0.1:45057
2022-05-27T15:30:09.114761Z TRACE method_call{method=say_hello}: mio::poll: registering event source with poller: token=Token(2), interests=READABLE | WRITABLE
2022-05-27T15:30:09.114968Z DEBUG method_call{method=say_hello}: hyper::client::connect::http: connected to 127.0.0.1:45057
2022-05-27T15:30:09.115012Z TRACE method_call{method=say_hello}: hyper::client::conn: client handshake Http1
2022-05-27T15:30:09.115048Z TRACE method_call{method=say_hello}: hyper::client::client: handshake complete, spawning background dispatcher task
2022-05-27T15:30:09.115162Z TRACE method_call{method=say_hello}: want: poll_want: taker wants!
2022-05-27T15:30:09.115199Z TRACE method_call{method=say_hello}: hyper::client::pool: checkout dropped for ("http", 127.0.0.1:45057)
2022-05-27T15:30:09.115715Z TRACE method_call{method=say_hello}: jsonrpsee_core::tracing: recv="{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"say_hello\",\"params\":[1,2,3]}"
2022-05-27T15:30:09.115776Z TRACE method_call{method=say_hello}: jsonrpsee_core::tracing: send="{\"jsonrpc\":\"2.0\",\"result\":\"lo\",\"id\":0}"
2022-05-27T15:30:09.116148Z TRACE method_call{method=say_hello}: jsonrpsee_core::tracing: recv="{\"id\":0,\"jsonrpc\":\"2.0\",\"result\":\"lo\"}"

@niklasad1 niklasad1 requested a review from a team as a code owner April 4, 2022 08:52
@niklasad1 niklasad1 changed the title make logging less verbose + tracing span per call WIP: make logging less verbose + tracing span per call Apr 4, 2022
@niklasad1 niklasad1 force-pushed the na-make-logging-less-verbose branch from 27391f2 to 5fbdcbf Compare May 27, 2022 15:27
@niklasad1 niklasad1 changed the title WIP: make logging less verbose + tracing span per call make logging less verbose + tracing span per call May 31, 2022
@niklasad1 niklasad1 self-assigned this May 31, 2022

/// Find the next char boundary to truncate at.
fn truncate_at_char_boundary(s: &str, max: u32) -> &str {
match s.char_indices().nth(max as usize) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite an expensive thing to do relatively speaking, but I guess we only do it for TRACE level logs which are only used in exceptional cases!

A quick optimisation might be to check s.len() < max first, and if that is true, there is def no need to truncate?


/// Find the next char boundary to truncate at.
fn truncate_at_char_boundary(s: &str, max: u32) -> &str {
match s.char_indices().nth(max as usize) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite an expensive thing to do relatively speaking, but I guess we only do it for TRACE level logs which are only used in exceptional cases!

A quick optimisation might be to check s.len() < max first, and if that is true, there is def no need to truncate?

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming you still want to get this merged, looks good to me, and the tracing stuff sounds very useful!

core/Cargo.toml Outdated Show resolved Hide resolved
serde_json::from_slice::<serde_json::Value>(&raw)
);
return Err(Error::Custom("Unparsable response".into()));
let json = serde_json::from_slice::<serde_json::Value>(&raw);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the connection is killed here, we can propagate the entire response in the error then.

core/src/lib.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 merged commit 00c2ce6 into master Jun 21, 2022
@niklasad1 niklasad1 deleted the na-make-logging-less-verbose branch June 21, 2022 06:10
@niklasad1 niklasad1 changed the title make logging less verbose + tracing span per call feat(logging): add one tracing span per JSON-RPC call Jun 21, 2022
@niklasad1 niklasad1 changed the title feat(logging): add one tracing span per JSON-RPC call feat(logging): add a tracing span per JSON-RPC call Jun 21, 2022
niklasad1 added a commit that referenced this pull request Jun 21, 2022
To include the bugfix for `tracing::enabled!` when `log` is enabled.
Follow up on #722
niklasad1 added a commit that referenced this pull request Jun 21, 2022
To include the bugfix for `tracing::enabled!` when `log` is enabled.
Follow up on #722
///
/// To enable this you need to call `RpcTracing::method_call("some_method").span().enable()`.
pub fn method_call(method: &str) -> Self {
Self(tracing::span!(tracing::Level::DEBUG, "method_call", %method))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants