Skip to main content

IMoBroService

The IMoBroService allows plugins to interact with MoBro. It facilitates registering items, updating metric values, and more.

info

All item registrations (e.g., metrics, categories, etc.) as well as metric value updates are accumulated and then pushed to the MoBro data service in bulk, within a maximum of 100 ms.

Functions

Register(IEnumerable<IMoBroItem>)

Registers multiple items with the service.

Parameters

NameTypeDescription
itemsIEnumerable<IMoBroItem>The items to register

Example

IEnumerable<IMoBroItem> metrics = CreateMetrics();
_mobro.Register(metrics);

Register(IMoBroItem)

Registers a single item with the service.

Parameters

NameTypeDescription
itemIMoBroItemThe item to register

Example

var metric = MoBroItem
.CreateMetric()
.WithId("os_name")
...
.Build();
_mobro.Register(metric);

Unregister(IEnumerable<string>)

Unregisters multiple items from the service.

Parameters

NameTypeDescription
idsIEnumerable<string>The IDs of the items to unregister

Example

_mobro.Unregister(new string[] { "os_name", "cpu_usage" });

Unregister(string)

Unregisters a single item from the service.

Parameters

NameTypeDescription
idstringThe ID of the item to unregister

Example

_mobro.Unregister("os_name");

GetAll(): IEnumerable<IMoBroItem>

Retrieves all registered items.

Returns

An IEnumerable<IMoBroItem> containing all registered items.

Example

var allItems = _mobro.GetAll();
foreach (var item in allItems)
{
Console.WriteLine($"Item: {item.Id}");
}

GetAll<T>(): IEnumerable<T>

Retrieves all registered items of a specified type.

Type Parameters

  • T: The type of the items to retrieve. T must implement IMoBroItem.

Returns

An IEnumerable<T> containing all registered items of the specified type.

Example

var metrics = _mobro.GetAll<Metric>();
foreach (var metric in metrics)
{
Console.WriteLine($"Metric: {metric.Id} - {metric.Label}");
}

TryGet<T>(string, out T): bool

Retrieves the registered item associated with a specified ID.

Type Parameters

  • T: The type of the item.

Parameters

NameTypeDescription
idstringThe ID of the item to retrieve.
itemT (out)When the method returns, contains the registered item associated with the specified ID; otherwise, null.

Returns

true if an item with the given ID and type is registered; otherwise false.

Example

if (_mobro.TryGet<Metric>("os_name", out var metric))
{
Console.WriteLine($"Metric: {metric.Label}");
}
else
{
// metric with ID 'os_name' not registered
}

ClearRegistration

Clears (unregisters) all currently registered items.

Example

_mobro.ClearRegistration();

UpdateMetricValues(IEnumerable<MetricValue>)

Pushes new values for one or more registered metrics.

Parameters

NameTypeDescription
valuesIEnumerable<MetricValue>The metric values

Example

IEnumerable<MetricValue> metricValues = GetCurrentValues();
_mobro.UpdateMetric(metricValues);

UpdateMetricValue(string, object?, DateTime)

Pushes a new value for a registered metric.

Parameters

NameTypeDescription
idstringThe ID of the metric.
valueobject?The new value of the metric.
timestampDateTimeThe timestamp when the value was recorded or measured.

Example

_mobro.UpdateMetric("os_name", "Windows 11", DateTime.UtcNow);

UpdateMetricValue(string, object?)

Pushes a new value for a registered metric, with the timestamp automatically set to DateTime.UtcNow.

Parameters

NameTypeDescription
idstringThe ID of the metric.
valueobject?The new value of the metric

Example

_mobro.UpdateMetric("os_name", "Windows 11");

UpdateMetricValue(MetricValue)

Pushes a single new value for a registered metric.

Parameters

NameTypeDescription
valueMetricValueThe metric value.

Example

_mobro.UpdateMetric(new MetricValue(
"os_name",
DateTime.UtcNow,
"Windows 11"
));

GetMetricValues(): IEnumerable<MetricValue>

Gets all current metric values.

Returns

An IEnumerable<MetricValue> containing all current metric values.

Example

var metricValues = _mobro.GetMetricValues();
foreach (var metricValue in metricValues)
{
Console.WriteLine($"{metricValue.Id}: {metricValue.Value}");
}

GetMetricValue(string): MetricValue

Retrieves the current value assigned to a metric with the specified ID.

Parameters

NameTypeDescription
idstringThe ID of the metric.

Returns

The MetricValue currently assigned to the metric.

Example

var metricValue = _mobro.GetMetricValue("os_name");
Console.WriteLine($"Current OS: {metricValue.Value}");

Error(string)

Notifies the service of an unrecoverable error, causing the service to terminate the plugin.

Parameters

NameTypeDescription
messagestringThe error message.

Example

_mobro.Error("Plugin error");

Error(Exception)

Notifies the service of an unrecoverable error due to an exception, causing the service to terminate the plugin.

Parameters

NameTypeDescription
exceptionExceptionThe occurred exception.

Example

try 
{
// some logic
}
catch (Exception e)
{
_logger.Error(e, "Plugin error");
_mobro.Error(e);
}