pub trait MetricNormalize {
// Required method
fn normalize(
&mut self,
state: &mut MetricSet,
metric: Metric,
) -> Option<Metric>;
}
Expand description
Normalizes metrics according to a set of rules.
Depending on the system in which they are being sent to, metrics may have to be modified in order to fit the data model or constraints placed on that system. Typically, this boils down to whether or not the system can accept absolute metrics or incremental metrics: the latest value of a metric, or the delta between the last time the metric was observed and now, respective. Other rules may need to be applied, such as dropping metrics of a specific type that the system does not support.
The trait provides a simple interface to apply this logic uniformly, given a reference to a simple state container
that allows tracking the necessary information of a given metric over time. As well, given the optional return, it
composes nicely with iterators (i.e. using filter_map
) in order to filter metrics within existing
iterator/stream-based approaches.
Required Methods§
sourcefn normalize(&mut self, state: &mut MetricSet, metric: Metric) -> Option<Metric>
fn normalize(&mut self, state: &mut MetricSet, metric: Metric) -> Option<Metric>
Normalizes the metric against the given state.
If the metric was normalized successfully, Some(metric)
will be returned. Otherwise, None
is returned.
In some cases, a metric may be successfully added/tracked within the given state, but due to the normalization logic, it cannot yet be emitted. An example of this is normalizing all metrics to be incremental.
In this example, if an incoming metric is already incremental, it can be passed through unchanged. If the
incoming metric is absolute, however, we need to see it at least twice in order to calculate the incremental
delta necessary to emit an incremental version. This means that the first time an absolute metric is seen,
normalize
would return None
, and the subsequent calls would return Some(metric)
.
However, a metric may simply not be supported by a normalization implementation, and so None
may or may not be
a common return value. This behavior is, thus, implementation defined.