Skip to main content

imetric

Macro imetric 

Source
macro_rules! imetric {
    (target: $target:expr, $name:expr, $unit:expr, $($rest:tt)+) => { ... };
    ($name:expr, $unit:expr, $($rest:tt)+) => { ... };
}
Expand description

Records a integer metric. Use imetric_dyn! when the name or unit is built at run time.

§Examples

use micromegas_tracing::imetric;

imetric!("Frame Time", "ticks", 1000);

Properties go between the unit and the value, as key = value pairs. A key is an identifier or a string literal. Literal values are interned once per callsite, unless a ..set is spread without a static value, in which case the literals are copied into the block (the spread set’s own values are not); any other value is attached to the measure and copied into the current block. A bare value must implement PropertyValue (strings do); use %value (Display) or ?value (Debug) for the rest, at the cost of one String allocation.

use micromegas_tracing::prelude::*;

let correlation_id = String::from("c0ffee");
let rows = 3;
imetric!("rows_fetched", "count", correlation_id = correlation_id, region = "eu", rows = %rows, 3);
imetric!("rows_fetched", "count", "http.method" = "GET", 3);

In a call with properties, a {key} placeholder whose name is a property key refers to that property’s value (evaluated once), not to a local of the same name; every other placeholder keeps its usual meaning. When every placeholder is a plain {key} over a %, bare, static, or string, char, bool or integer literal property, or a {key:?} over a ? property, and the call has no other format arguments, the process doesn’t format the message at all: it stores the format string with the properties, and analytics renders it. That is the cheapest way to put a property in the message, and it is smaller in the block. A placeholder with a spec ({key:>8}), a {key} over a ? property, a float or negated literal, a positional argument, or a name that isn’t a property is formatted in the process as before. Property names are case-insensitive, so a placeholder matching two keys is a compile error, and a placeholder only binds a key spelled exactly like it.

use micromegas_tracing::prelude::*;

let (user, rows) = ("alice", 3);
info!(user = user, rows = %rows, "{user} fetched {rows} rows");

A placeholder that matches two keys is ambiguous, so it doesn’t compile:

ⓘ
use micromegas_tracing::prelude::*;

let (a, b) = ("1", "2");
info!(user = %a, User = %b, "{user}");

A literal or static property is recorded once per callsite and never formatted per call, so it can’t take a format spec or {key:?}; bind the value to a differently named local and format that instead:

ⓘ
use micromegas_tracing::prelude::*;

let x: &'static str = "x";
info!(l = static x, "{l:?}");
ⓘ
use micromegas_tracing::prelude::*;

info!(n = 1, "{n:>8}");

A precomputed &'static PropertySet is spread with ..set. It must come last in the property list, and key = value pairs override its entries:

use micromegas_tracing::prelude::*;
use micromegas_tracing::property_set::{Property, PropertySet};

let set = PropertySet::find_or_create(vec![Property::new("region", "eu")]);
imetric!("rows_fetched", "count", ..set, 3);
imetric!("rows_fetched", "count", correlation_id = "c0ffee", ..set, 3);

target: "name" replaces the module path as the target of the metric:

use micromegas_tracing::prelude::*;

imetric!(target: "my_crate::cache", "hits", "count", kind = "disk", 1);

key = static expr takes a &'static str chosen at runtime, from a small closed set (a label or an enum variant name, for example). Each distinct tuple of static values is interned once per callsite and then looked up, a few nanoseconds more than a ..set spread and far cheaper than a dynamic value. Every distinct value is kept forever, so the cardinality contract of PropertySet applies: never use it for ids. It mixes freely with literals, dynamic values and a trailing ..set. Among literals and static values the last one wins (case-insensitive), a dynamic value beats both, and any pair overrides a ..set entry. With ..set and a static value in the same call, literals and static values are interned over the set, so dynamic values beat them whatever the order.

use micromegas_tracing::prelude::*;

let label: &'static str = "alpha";
imetric!("rows_fetched", "count", prefix = static label, tier = "ram", 3);
fmetric!("fetch_ms", "ms", prefix = static label, 1.5);

The value must be 'static; a borrowed string is a compile error:

ⓘ
use micromegas_tracing::prelude::*;

let s = String::from("alpha");
imetric!("rows_fetched", "count", prefix = static s.as_str(), 3);

static doesn’t combine with % or ?:

ⓘ
use micromegas_tracing::prelude::*;

imetric!("rows_fetched", "count", prefix = static %"x", 3);

target, name and unit are reserved keys, because analytics treats them as column overrides. They can still be set through a ..set spread:

ⓘ
use micromegas_tracing::prelude::*;

imetric!("rows_fetched", "count", unit = "rows", 3);

A literal value goes through concat!, so 1_000 is recorded as “1000” and 0x10 as “16”, while a float keeps its spelling (0.50 stays “0.50”); %value uses Display instead.

A bare integer needs %:

ⓘ
use micromegas_tracing::prelude::*;

let n = 3_u32;
imetric!("rows_fetched", "count", rows = n, 3);

A bare negated expression has no PropertyValue impl either:

ⓘ
use micromegas_tracing::prelude::*;

let n = 3_i32;
imetric!("rows_fetched", "count", delta = -n, 3);

..set can’t be followed by other entries, and appears at most once:

ⓘ
use micromegas_tracing::prelude::*;
use micromegas_tracing::property_set::PropertySet;

let set = PropertySet::empty();
imetric!("rows_fetched", "count", ..set, kind = "a", 3);
ⓘ
use micromegas_tracing::prelude::*;
use micromegas_tracing::property_set::PropertySet;

let set = PropertySet::empty();
imetric!("rows_fetched", "count", ..set, ..set, 3);