Skip to main content

Updating metric values

After registering a metric, the metric does not yet have a value.
The value of a metric can be updated anytime and as frequently as needed by the plugin.

tip

If metrics are obtained from a sensor or an external API, defining the update frequency generally involves balancing more up-to-date metric values against the possible performance hit of frequently accessing the sensors.
It generally makes sense to expose these update frequencies as plugin settings, allowing the user to decide according to his preferences.

Updating values

Similar to registering metric, metric values are passed to the IMoBroService.
The service offers multiple overloads of the UpdateMetricValue function allowing to pass simple literal values as well as multiple MetricValues at once.

note

A metric needs to be registered before its value is updated.
Metric values of unknown (=unregistered) metrics will be ignored by MoBro.

// update the value of metric 'cpu_usage' by passing a new percentage value 
_mobro.UpdateMetricValue("cpu_usage", 42.69);

// update the value of metric 'cpu_usage' by passing a new percentage value
// alsonside the DateTime the value was measuered at
_mobro.UpdateMetricValue("cpu_usage", 42.69, DateTime.UtcNow);

// update the value of metric 'cpu_usage' by passing a MetricValue struct
_mobro.UpdateMetricValue(new MetricValue("cpu_usage", DateTime.UtcNow, 42.69));

// pass multiple metric values at once
IEnumerable<MetricValue> metricValues = GetCurrentValues();
_mobro.UpdateMetricValues(metricValues);

Value restrictions

When updating metric values, the actual values must conform to certain restrictions imposed by the metrics MetricValueType (as defined by the metrics MetricType).
See Reference: MetricType for a list of all value restrictions.

caution

Passing values of invalid type causes the plugin to be terminated erroneously

For example:
When updating the value of a metric of MetricValueType DateTime, the value passed to the UpdateMetricValue function must either be a data + time string conforming to ISO 8601 or a C# DateTime/DateTimeOffset object.

Numerical values

When updating a numerical MetricValue, the value must be provided in the base unit as defined by the metrics MetricType to ensure correct unit conversions