Skip to content

rpc-extractor: add uptime and getnettotals support - #302

Merged
0xB10C merged 2 commits into
peer-observer:masterfrom
GuiSchet:add-simple-rpcs
Dec 11, 2025
Merged

0xB10C merged 2 commits into
peer-observer:masterfrom
GuiSchet:add-simple-rpcs

Conversation

@GuiSchet

@GuiSchet GuiSchet commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

Implements two new RPC queries to the rpc-extractor:

  • uptime: Returns node uptime in seconds
  • getnettotals: Returns network traffic statistics (bytes sent/received, upload target)

Both RPCs follow the established pattern with:

  • Protobuf message definitions in rpc.proto
  • From trait implementations for type conversions
  • CLI flags --disable-uptime and --disable-getnettotals
  • Async functions that publish events to NATS

The uptime RPC is particularly lightweight (4-5ms response time) and
provides a simple metric for node health monitoring. The getnettotals
RPC exposes network traffic statistics useful for bandwidth analysis.

Partially addresses #199.

@GuiSchet

GuiSchet commented Dec 9, 2025

Copy link
Copy Markdown
Contributor Author

i'll make other PR addingPrometheus metrics support for the new uptime and getnettotals RPCs.

@0xB10C

0xB10C commented Dec 10, 2025

Copy link
Copy Markdown
Collaborator

Awesome, thanks. Did a first pass and code looks good. I want to play around with this a bit.

i'll make other PR addingPrometheus metrics support for the new uptime and getnettotals RPCs.

You might need to add a stub, otherwise the compilation and tests will fail:

error[E0004]: non-exhaustive patterns: `&shared::protobuf::rpc::rpc_event::Event::Uptime(_)` and `&shared::protobuf::rpc::rpc_event::Event::NetTotals(_)` not covered
   --> tools/metrics/src/lib.rs:164:11
    |
164 |     match e {
    |           ^ patterns `&shared::protobuf::rpc::rpc_event::Event::Uptime(_)` and `&shared::protobuf::rpc::rpc_event::Event::NetTotals(_)` not covered
    |
note: `shared::protobuf::rpc::rpc_event::Event` defined here
   --> /home/runner/work/peer-observer/peer-observer/target/debug/build/shared-5e63b5618d299ac8/out/rpc.rs:12:5
    |
 12 |     pub enum Event {
    |     ^^^^^^^^^^^^^^
...
 18 |         Uptime(u32),
    |         ------ not covered
 19 |         #[prost(message, tag = "4")]
 20 |         NetTotals(super::NetTotals),
    |         --------- not covered
    = note: the matched value is of type `&shared::protobuf::rpc::rpc_event::Event`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
    |
534 ~         },
535 +         &shared::protobuf::rpc::rpc_event::Event::Uptime(_) | &shared::protobuf::rpc::rpc_event::Event::NetTotals(_) => todo!()
    |

Comment thread protobuf/rpc.proto Resolved
@0xB10C

0xB10C commented Dec 10, 2025 •

Copy link
Copy Markdown
Collaborator

feel free to just add a match arm similar to:

 &shared::protobuf::rpc::rpc_event::Event::Uptime(_) | &shared::protobuf::rpc::rpc_event::Event::NetTotals(_) => {},

in metrics for now.

As for metrics, uptime should probably just be a prometheus IntGauge. For total_bytes_received and total_bytes_sent, am IntGauge works too.

Comment thread extractors/rpc/src/lib.rs Resolved
@0xB10C

0xB10C commented Dec 10, 2025

Copy link
Copy Markdown
Collaborator

You probably also want to add the new command line flag in the tests. See CI failure :) You can run the build and tests locally with cargo build and cargo test --all-features.

Additionally, it would be good to have tests for these events similar to the other RPC tests. See e.g.

async fn test_integration_rpc_getpeerinfo() {
println!("test that we receive getpeerinfo RPC events");
check(false, true, |event| {
match event {
Event::Rpc(r) => {
if let Some(ref e) = r.event {
match e {
PeerInfos(p) => {
// we expect 1 peer to be connected
assert_eq!(p.infos.len(), 1);
let peer = p.infos.first().expect("we have expactly one peer here");
assert_eq!(peer.connection_type, "inbound");
return;
}
_ => panic!("unexpected RPC data {:?}", r.event),
}
}
}
_ => panic!("unexpected event {:?}", event),
}
})
.await;
}

@GuiSchet

Copy link
Copy Markdown
Contributor Author

Hi @0xB10C,

Apologies for the CI spam! I've now addressed all the issues:

Changes Made:

  1. Prometheus Metrics Added:

    • rpc_uptime (IntGauge) - Node uptime in seconds
    • rpc_nettotals_total_bytes_received (IntGauge) - Total bytes received
    • rpc_nettotals_total_bytes_sent (IntGauge) - Total bytes sent
  2. Integration Tests Added:

    • test_integration_rpc_uptime() - Verifies uptime RPC events
    • test_integration_rpc_getnettotals() - Verifies getnettotals RPC events
    • Updated existing test helpers to support the new --disable-uptime and --disable-getnettotals flags
  3. Fixed:

    • Non-exhaustive pattern match error in metrics tool
    • All integration tests now pass locally (verified with cargo test --features nats_integration_tests --features node_integration_tests)

Test Results:

✅ test_integration_rpc_getmempoolinfo ... ok
✅ test_integration_rpc_getnettotals ... ok
✅ test_integration_rpc_getpeerinfo ... ok
✅ test_integration_rpc_uptime ... ok

All 40 metrics integration tests also pass.

Ready for another look when you have time. Thanks for the feedback!

Comment thread extractors/rpc/tests/integration.rs Outdated
Comment on lines +220 to +221
// total_bytes_received and total_bytes_sent are u64, so they're always >= 0
// Just verify the event was received correctly

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think that's true. u64 can be 0!

@0xB10C

0xB10C commented Dec 11, 2025

Copy link
Copy Markdown
Collaborator

Thanks! I see a few minor things to fixup. Will do them myself and push them here to save on the extra review-roundtrip. Afterwards, this should be ready for merge.

@0xB10C

0xB10C commented Dec 11, 2025

Copy link
Copy Markdown
Collaborator

I split this up into two clean commits, ran cargo fmt, addressed #302 (comment) and added test_integration_metrics_rpc_getnettotals() and test_integration_metrics_rpc_uptime() see:

https://github.com/peer-observer/peer-observer/compare/a18b21f81dab7f37debcf875398357e9758fc9c2..6638cb3f13272eb7fe433b5ddd76791a3adf4cfd

@0xB10C

0xB10C commented Dec 11, 2025

Copy link
Copy Markdown
Collaborator

Thank you @GuiSchet! Feel free to pick up other RPCs to implement, but note that I'm currently refactoring some of the protobuf definitions in #305, so this will probably cause collisions. Might be better to wait a few days until that's done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants