Updating Metric Values
After registering a metric, the metric does not yet have a value.
The value of a metric can be updated at any time and as frequently as needed by the plugin.
If metrics are obtained from a sensor or an external API, determining the update frequency often requires balancing the
need for up-to-date values with the performance cost of frequent sensor or API access.
It’s a good practice to expose these update frequencies as plugin settings, allowing users to configure them according
to their preferences.
Updating Values
Similar to registering a metric, metric values are passed to the
IMoBroService
.
The service provides multiple overloads of the UpdateMetricValue
function, allowing you to pass:
- Simple literal values.
- Multiple MetricValues at once.
Metrics must be registered before their values can be updated.
Any attempts to update unregistered or unknown metrics will be ignored by MoBro.
Examples
Below are some examples of updating metric values using the UpdateMetricValue
function:
// Update the metric 'cpu_usage' with a new percentage value
_mobro.UpdateMetricValue("cpu_usage", 42.69);
// Update the metric 'cpu_usage' and include the DateTime when the value was measured
_mobro.UpdateMetricValue("cpu_usage", 42.69, DateTime.UtcNow);
// Update the metric 'cpu_usage' using 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 values must comply with the restrictions defined by the
metric's MetricValueType (as specified in
the MetricType).
Refer to the Reference: MetricType documentation for a
detailed list
of restrictions.
Passing values of an invalid type will cause the plugin to terminate unexpectedly.
Example: DateTime
MetricValueType
For metrics with a MetricValueType
of DateTime
, the value passed to the UpdateMetricValue
function must either:
- Be a valid date-time string that conforms to the ISO 8601 standard, or
- Be a C#
DateTime
orDateTimeOffset
object.
Numerical Values
For numerical MetricValues, the value must be provided in the base unit as defined by the metric's MetricType. This ensures that unit conversions are handled correctly.