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(metrics): Metrics derive macro #592

Merged
merged 17 commits into from
Dec 26, 2022
Merged

Conversation

rkrasiuk
Copy link
Member

@rkrasiuk rkrasiuk commented Dec 23, 2022

This PR introduces Metrics derive macro to reduce code duplication for declaring, registering and deriving metrics.
Addresses #584 (comment)

@rkrasiuk rkrasiuk added C-enhancement New feature or request A-observability Related to tracing, metrics, logs and other observability tools labels Dec 23, 2022
@rkrasiuk rkrasiuk marked this pull request as draft December 23, 2022 13:49
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

I like this a lot.

some nits.

also I'd be great to have some simple tests: ref https://github.com/metrics-rs/metrics/blob/main/metrics-macros/src/tests.rs

re crate name, maybe we'll have additional metrics stuff so I'd suggest we name this metrics-macros?

@gakonst gakonst mentioned this pull request Dec 25, 2022
@codecov-commenter
Copy link

codecov-commenter commented Dec 26, 2022

Codecov Report

Merging #592 (89f053d) into main (ba585cf) will increase coverage by 0.24%.
The diff coverage is 94.73%.

@@            Coverage Diff             @@
##             main     #592      +/-   ##
==========================================
+ Coverage   72.00%   72.25%   +0.24%     
==========================================
  Files         251      255       +4     
  Lines       25478    25737     +259     
==========================================
+ Hits        18345    18595     +250     
- Misses       7133     7142       +9     
Flag Coverage Δ
unit-tests 72.25% <94.73%> (+0.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
bin/reth/src/node/mod.rs 0.00% <0.00%> (ø)
crates/stages/src/lib.rs 100.00% <ø> (ø)
crates/stages/src/stages/headers.rs 97.65% <ø> (ø)
crates/metrics/metrics-derive/src/metric.rs 82.05% <82.05%> (ø)
crates/stages/src/stage_metrics.rs 87.50% <87.50%> (ø)
crates/metrics/metrics-derive/src/expand.rs 99.23% <99.23%> (ø)
crates/metrics/metrics-derive/src/lib.rs 100.00% <100.00%> (ø)
crates/metrics/metrics-derive/src/with_attrs.rs 100.00% <100.00%> (ø)
crates/transaction-pool/src/test_util/mock.rs 53.70% <0.00%> (-6.67%) ⬇️
crates/net/eth-wire/src/ethstream.rs 84.46% <0.00%> (-0.56%) ⬇️
... and 10 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@rkrasiuk rkrasiuk marked this pull request as ready for review December 26, 2022 16:19
@rkrasiuk
Copy link
Member Author

@mattsse @onbjerg RFR now

Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

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

Good from me. Nice work. Thinking we should extract some of these general token parsing utils to a separate crate for use inside of Foundry, ethers and here.

Comment on lines +48 to +50
/// Describe all exposed metrics. Internally calls `describe_*` macros from
/// the metrics crate according to the metric type.
/// Ref: https://docs.rs/metrics/0.20.1/metrics/index.html#macros
Copy link
Member

Choose a reason for hiding this comment

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

@mattsse iirc this does not work and requires #[doc = ...]?

/// }
/// }
/// ```
#[proc_macro_derive(Metrics, attributes(metrics, metric))]
Copy link
Member

Choose a reason for hiding this comment

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

great docs here

@@ -0,0 +1,180 @@
use std::{collections::HashMap, sync::Mutex};
Copy link
Member

Choose a reason for hiding this comment

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

great work in these tests. really clear.

@@ -0,0 +1,5 @@
#[test]
fn compile_test() {
let t = trybuild::TestCases::new();
Copy link
Member

Choose a reason for hiding this comment

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

also really neat - TIL

Copy link
Member

Choose a reason for hiding this comment

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

First, if a test case is being run as compile_fail but a corresponding *.stderr file does not exist, the test runner will save the actual compiler output with the right filename into a directory called wip within the directory containing Cargo.toml. So you can update these files by deleting them, running cargo test, and moving all the files from wip into your testcase directory.

Alternatively, run cargo test with the environment variable TRYBUILD=overwrite to skip the wip directory and write all compiler output directly in place. You'll want to check git diff afterward to be sure the compiler's output is what you had in mind.

https://github.com/dtolnay/trybuild#workflow - really sick work @dtolnay, thanks for the crate

Copy link
Member Author

Choose a reason for hiding this comment

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

i've spent a couple of hours battling compiletest-rs and rustc lib linkage before finding this gem. indeed, great work, really easy to use

@@ -26,9 +26,8 @@ use reth_network::{
use reth_primitives::{Account, Header, H256};
use reth_provider::{db_provider::ProviderImpl, BlockProvider, HeaderProvider};
use reth_stages::{
stage_metrics::HeaderMetrics,
Copy link
Contributor

Choose a reason for hiding this comment

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

a question about the naming convention for the metrics modules, what do you think about following the structure

<crate>::metrics;

instead of, like in this case,

<crate>::<crate>_metrics;

I think it would be slightly nicer and less verbose

@entropidelic
Copy link
Contributor

awesome work! Just a little suggestion about the naming convention of the metrics modules inside each crate

@rkrasiuk
Copy link
Member Author

rkrasiuk commented Dec 26, 2022

@entropidelic sgtm, we can fix that in follow up PRs fixed

@rkrasiuk rkrasiuk merged commit 663efa8 into main Dec 26, 2022
@rkrasiuk rkrasiuk deleted the rkrasiuk/feat-metrics-derive branch December 26, 2022 22:27
@prestwich
Copy link
Collaborator

prestwich commented Dec 26, 2022

quick heads up, metrics scopes of the form x.y are not compatible with prometheus's data model. Metric names are validated as alphanumeric + _ + :. the period . is not an allowed character. As written, I believe this PR will block any usage of prometheus

@gakonst
Copy link
Member

gakonst commented Dec 27, 2022

Should we rename everything to use underscores and validate that in the macro?

@rkrasiuk
Copy link
Member Author

@prestwich you're right, thanks for the heads up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-observability Related to tracing, metrics, logs and other observability tools C-enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants