Skip to content

Commit

Permalink
chore(metrics): update metric_name macro to handle a collection of …
Browse files Browse the repository at this point in the history
…names (#1163)

## Summary
The `astria_telemetry::metric_name` macro was changed to `metric_names`
and now defines a collection of const names rather than a single one.

## Background
This will make testing the individual crates' metrics more robust since
we now have a const variable which includes all metric names.

## Changes
- Updated and renamed `astria_telemetry::metric_name` to
`astria_telemetry::metric_names`.

## Testing
Unit tests updated and extended.
  • Loading branch information
Fraser999 authored Jun 10, 2024
1 parent 9cf79a7 commit 53a1ecb
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 71 deletions.
16 changes: 9 additions & 7 deletions crates/astria-bridge-withdrawer/src/metrics_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use metrics::{
describe_histogram,
Unit,
};
use telemetry::metric_name;
use telemetry::metric_names;

/// Registers all metrics used by this crate.
pub fn register() {
Expand Down Expand Up @@ -40,12 +40,14 @@ pub fn register() {
);
}

metric_name!(pub const CURRENT_NONCE);
metric_name!(pub const NONCE_FETCH_COUNT);
metric_name!(pub const NONCE_FETCH_FAILURE_COUNT);
metric_name!(pub const NONCE_FETCH_LATENCY);
metric_name!(pub const SEQUENCER_SUBMISSION_FAILURE_COUNT);
metric_name!(pub const SEQUENCER_SUBMISSION_LATENCY);
metric_names!(pub const METRICS_NAMES:
CURRENT_NONCE,
NONCE_FETCH_COUNT,
NONCE_FETCH_FAILURE_COUNT,
NONCE_FETCH_LATENCY,
SEQUENCER_SUBMISSION_FAILURE_COUNT,
SEQUENCER_SUBMISSION_LATENCY
);

#[cfg(test)]
mod tests {
Expand Down
26 changes: 14 additions & 12 deletions crates/astria-composer/src/metrics_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use metrics::{
describe_histogram,
Unit,
};
use telemetry::metric_name;
use telemetry::metric_names;

/// Labels
pub(crate) const ROLLUP_ID_LABEL: &str = "rollup_id";
Expand Down Expand Up @@ -74,17 +74,19 @@ pub fn register() {
);
}

metric_name!(pub const TRANSACTIONS_RECEIVED);
metric_name!(pub const TRANSACTIONS_DROPPED);
metric_name!(pub const TRANSACTIONS_DROPPED_TOO_LARGE);
metric_name!(pub const NONCE_FETCH_COUNT);
metric_name!(pub const NONCE_FETCH_FAILURE_COUNT);
metric_name!(pub const NONCE_FETCH_LATENCY);
metric_name!(pub const CURRENT_NONCE);
metric_name!(pub const SEQUENCER_SUBMISSION_LATENCY);
metric_name!(pub const SEQUENCER_SUBMISSION_FAILURE_COUNT);
metric_name!(pub const TRANSACTIONS_PER_SUBMISSION);
metric_name!(pub const BYTES_PER_SUBMISSION);
metric_names!(pub const METRICS_NAMES:
TRANSACTIONS_RECEIVED,
TRANSACTIONS_DROPPED,
TRANSACTIONS_DROPPED_TOO_LARGE,
NONCE_FETCH_COUNT,
NONCE_FETCH_FAILURE_COUNT,
NONCE_FETCH_LATENCY,
CURRENT_NONCE,
SEQUENCER_SUBMISSION_LATENCY,
SEQUENCER_SUBMISSION_FAILURE_COUNT,
TRANSACTIONS_PER_SUBMISSION,
BYTES_PER_SUBMISSION
);

#[cfg(test)]
mod tests {
Expand Down
22 changes: 12 additions & 10 deletions crates/astria-conductor/src/metrics_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use metrics::{
describe_histogram,
Unit,
};
use telemetry::metric_name;
use telemetry::metric_names;

pub(crate) const NAMESPACE_TYPE_LABEL: &str = "namespace_type";
pub(crate) const NAMESPACE_TYPE_METADATA: &str = "metadata";
Expand Down Expand Up @@ -59,15 +59,17 @@ pub fn register() {
);
}

metric_name!(pub const BLOBS_PER_CELESTIA_FETCH);
metric_name!(pub const CELESTIA_BLOB_FETCH_ERROR_COUNT);
metric_name!(pub const DECODED_ITEMS_PER_CELESTIA_FETCH);
metric_name!(pub const SEQUENCER_BLOCKS_METADATA_VERIFIED_PER_CELESTIA_FETCH);
metric_name!(pub const SEQUENCER_BLOCK_INFORMATION_RECONSTRUCTED_PER_CELESTIA_FETCH);

metric_name!(pub const EXECUTED_FIRM_BLOCK_NUMBER);
metric_name!(pub const EXECUTED_SOFT_BLOCK_NUMBER);
metric_name!(pub const TRANSACTIONS_PER_EXECUTED_BLOCK);
metric_names!(pub const METRICS_NAMES:
BLOBS_PER_CELESTIA_FETCH,
CELESTIA_BLOB_FETCH_ERROR_COUNT,
DECODED_ITEMS_PER_CELESTIA_FETCH,
SEQUENCER_BLOCKS_METADATA_VERIFIED_PER_CELESTIA_FETCH,
SEQUENCER_BLOCK_INFORMATION_RECONSTRUCTED_PER_CELESTIA_FETCH,

EXECUTED_FIRM_BLOCK_NUMBER,
EXECUTED_SOFT_BLOCK_NUMBER,
TRANSACTIONS_PER_EXECUTED_BLOCK
);

#[cfg(test)]
mod tests {
Expand Down
28 changes: 15 additions & 13 deletions crates/astria-sequencer-relayer/src/metrics_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use metrics::{
describe_histogram,
Unit,
};
use telemetry::metric_name;
use telemetry::metric_names;

/// Registers all metrics used by this crate.
pub fn register() {
Expand Down Expand Up @@ -93,18 +93,20 @@ pub fn register() {
// output, and may need to be updated over time.
pub const HISTOGRAM_BUCKETS: &[f64; 5] = &[0.00001, 0.0001, 0.001, 0.01, 0.1];

metric_name!(pub const CELESTIA_SUBMISSION_HEIGHT);
metric_name!(pub const CELESTIA_SUBMISSION_COUNT);
metric_name!(pub const CELESTIA_SUBMISSION_FAILURE_COUNT);
metric_name!(pub const BLOCKS_PER_CELESTIA_TX);
metric_name!(pub const BLOBS_PER_CELESTIA_TX);
metric_name!(pub const BYTES_PER_CELESTIA_TX);
metric_name!(pub const CELESTIA_PAYLOAD_CREATION_LATENCY);
metric_name!(pub const CELESTIA_SUBMISSION_LATENCY);
metric_name!(pub const SEQUENCER_BLOCK_FETCH_FAILURE_COUNT);
metric_name!(pub const SEQUENCER_HEIGHT_FETCH_FAILURE_COUNT);
metric_name!(pub const SEQUENCER_SUBMISSION_HEIGHT);
metric_name!(pub const COMPRESSION_RATIO_FOR_ASTRIA_BLOCK);
metric_names!(pub const METRICS_NAMES:
CELESTIA_SUBMISSION_HEIGHT,
CELESTIA_SUBMISSION_COUNT,
CELESTIA_SUBMISSION_FAILURE_COUNT,
BLOCKS_PER_CELESTIA_TX,
BLOBS_PER_CELESTIA_TX,
BYTES_PER_CELESTIA_TX,
CELESTIA_PAYLOAD_CREATION_LATENCY,
CELESTIA_SUBMISSION_LATENCY,
SEQUENCER_BLOCK_FETCH_FAILURE_COUNT,
SEQUENCER_HEIGHT_FETCH_FAILURE_COUNT,
SEQUENCER_SUBMISSION_HEIGHT,
COMPRESSION_RATIO_FOR_ASTRIA_BLOCK
);

#[cfg(test)]
mod tests {
Expand Down
28 changes: 15 additions & 13 deletions crates/astria-sequencer/src/metrics_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use metrics::{
describe_histogram,
Unit,
};
use telemetry::metric_name;
use telemetry::metric_names;

/// Registers all metrics used by this crate.
pub fn register() {
Expand Down Expand Up @@ -90,18 +90,20 @@ pub fn register() {
);
}

metric_name!(pub const PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS_DECODE_FAILURE);
metric_name!(pub const PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS_COMETBFT_SPACE);
metric_name!(pub const PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS_SEQUENCER_SPACE);
metric_name!(pub const PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS_FAILED_EXECUTION);
metric_name!(pub const PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS);
metric_name!(pub const PROPOSAL_DEPOSITS);
metric_name!(pub const PROPOSAL_TRANSACTIONS);
metric_name!(pub const PROCESS_PROPOSAL_SKIPPED_PROPOSAL);
metric_name!(pub const CHECK_TX_REMOVED_TOO_LARGE);
metric_name!(pub const CHECK_TX_REMOVED_FAILED_STATELESS);
metric_name!(pub const CHECK_TX_REMOVED_STALE_NONCE);
metric_name!(pub const CHECK_TX_REMOVED_ACCOUNT_BALANCE);
metric_names!(pub const METRICS_NAMES:
PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS_DECODE_FAILURE,
PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS_COMETBFT_SPACE,
PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS_SEQUENCER_SPACE,
PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS_FAILED_EXECUTION,
PREPARE_PROPOSAL_EXCLUDED_TRANSACTIONS,
PROPOSAL_DEPOSITS,
PROPOSAL_TRANSACTIONS,
PROCESS_PROPOSAL_SKIPPED_PROPOSAL,
CHECK_TX_REMOVED_TOO_LARGE,
CHECK_TX_REMOVED_FAILED_STATELESS,
CHECK_TX_REMOVED_STALE_NONCE,
CHECK_TX_REMOVED_ACCOUNT_BALANCE
);

#[cfg(test)]
mod tests {
Expand Down
78 changes: 62 additions & 16 deletions crates/astria-telemetry/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,43 @@ pub use const_format::{
Case as __Case,
};

/// Declare a `const` string slice, using the declaring crate's name as a
/// Declare a collection of `const` string slices, using the declaring crate's name as a
/// prefix and the variable name as a suffix.
///
/// This macro essentially performs this declaration:
/// This macro essentially performs these declarations:
/// ```text
/// METRIC_NAME := ${CARGO_CRATE_NAME}_metric_name;
/// METRIC_NAME_1 := ${CARGO_CRATE_NAME}_metric_name_1;
/// METRIC_NAME_2 := ${CARGO_CRATE_NAME}_metric_name_2;
/// METRIC_NAME_3 := ${CARGO_CRATE_NAME}_metric_name_3;
///
/// METRICS_NAMES := [METRIC_NAME_1, METRIC_NAME_2, METRIC_NAME_3];
/// ```
///
/// The purpose of this macro is to avoid accidental typos, ensuring that the
/// metric name matches the const variable name.
/// metric name matches the const variable name, and to provide a collection of all metric names.
///
/// # Examples
/// ```
/// use astria_telemetry::metric_name;
/// metric_name!(pub const EXAMPLE_COUNTER);
/// // Note that this example has `astria_telemetry` a a prefix because
/// use astria_telemetry::metric_names;
/// metric_names!(pub const ALL_METRICS: EXAMPLE_COUNTER, EXAMPLE_GAUGE);
/// // Note that these examples have `astria_telemetry` as a prefix because
/// // this doctest is part of this crate.
/// // In your case, use your crate's `CARGO_CRATE_NAME` as prefix.
/// // In your case, your own crate's `CARGO_CRATE_NAME` will be the prefix.
/// assert_eq!(EXAMPLE_COUNTER, "astria_telemetry_example_counter");
/// assert_eq!(EXAMPLE_GAUGE, "astria_telemetry_example_gauge");
/// assert_eq!(ALL_METRICS, [EXAMPLE_COUNTER, EXAMPLE_GAUGE]);
/// ```
#[macro_export]
macro_rules! metric_name {
($vis:vis const $($tt:tt)*) => {
$crate::__metric_name_internal!(
$vis [$($tt)*] [::core::stringify!($($tt)*)]
);
macro_rules! metric_names {
($vis:vis const $collection_name:ident: $($name:ident),* $(,)?) => {
$(
$crate::__metric_name_internal!($vis[$name][::core::stringify!($name)]);
)*
$vis const $collection_name: [&str; $crate::__count!($($name)*)] = [
$(
$name,
)*
];
}
}

Expand All @@ -48,11 +59,46 @@ macro_rules! __metric_name_internal {
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __count {
() => (0_usize);
( $x:tt $($xs:tt)* ) => (1_usize + $crate::__count!($($xs)*));
}

#[cfg(test)]
mod tests {
mod inner {
metric_names!(const PRIVATE_METRICS: EXAMPLE_COUNTER, EXAMPLE_GAUGE, EXAMPLE_HISTOGRAM);
metric_names!(pub(super) const PUBLIC_METRICS: PUB_COUNTER, PUB_GAUGE, PUB_HISTOGRAM);

#[test]
fn gives_expected_const_and_value() {
assert_eq!("astria_telemetry_example_counter", EXAMPLE_COUNTER);
assert_eq!("astria_telemetry_example_gauge", EXAMPLE_GAUGE);
assert_eq!("astria_telemetry_example_histogram", EXAMPLE_HISTOGRAM);
assert_eq!(
PRIVATE_METRICS,
[EXAMPLE_COUNTER, EXAMPLE_GAUGE, EXAMPLE_HISTOGRAM]
);
}
}

#[test]
fn should_respect_pub_visibility() {
assert_eq!("astria_telemetry_pub_counter", inner::PUB_COUNTER);
assert_eq!("astria_telemetry_pub_gauge", inner::PUB_GAUGE);
assert_eq!("astria_telemetry_pub_histogram", inner::PUB_HISTOGRAM);
assert_eq!(
inner::PUBLIC_METRICS,
[inner::PUB_COUNTER, inner::PUB_GAUGE, inner::PUB_HISTOGRAM]
);
}

#[test]
fn gives_expected_const_and_value() {
crate::metric_name!(const EXAMPLE_CONST);
assert_eq!("astria_telemetry_example_const", EXAMPLE_CONST);
fn should_allow_trailing_comma() {
metric_names!(const TRAILING_COMMA: A,);
assert_eq!("astria_telemetry_a", A);
assert_eq!(TRAILING_COMMA, [A]);
}
}

0 comments on commit 53a1ecb

Please sign in to comment.