Skip to main content

Metrics

Derive Macro Metrics 

Source
#[derive(Metrics)]
{
    // Attributes available to this derive:
    #[new]
    #[crate_path]
    #[config]
    #[raw_end_fn]
    #[calculate]
    #[hidden]
}
Expand description

Derive macros for Metrics trait.

Using this macro will emit implementation of Metrics and Default traits, and some helper assertions.

By default each metric field is initialized using Default::default() implementation. But one can customize constructor by using #[new(...)] attribute, where they can provide arguments for new method in the metric type.

In example below, cycles field will be initialized with PerfEventMetric::new(perf_event::events::Hardware::CPU_CYCLES).

§Example:

#[derive(Metrics)]
pub struct MetricsProvider {
   /// Without `#[new]` attribute, the metric will be initialized with `Default::default()`.
   /// wall_time can be gathered from Instant or from perf_event(CPU_CLOCK), result is similar,
   /// but Instant is more portable.
   pub wall_time: crate::InstantProvider,
   /// CPU cycles spent in the span.
   /// The first metric in the list will be used as the primary metric and adds report of %parent in the report.
   #[new(perf_event::events::Hardware::CPU_CYCLES)]
   pub cycles: crate::PerfEventMetric,
}

Macro is only applies to structs where each member implements the SingleMetric trait.

Note: Metrics and SingleMetric are different traits.

#[config] attribute allows to customize display options for each metric, see [MetricReportInfo] for more details.

§Example:

#[derive(Metrics)]
pub struct MetricsProvider {
    #[new(perf_event::events::Hardware::CPU_CYCLES)]
    #[config(show_spread = false)]
    pub cycles: crate::PerfEventMetric,
}