IMoBroService
The IMoBroService
allows plugins to interact with MoBro. It facilitates registering items, updating metric values, and
more.
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
Name | Type | Description |
---|---|---|
items | IEnumerable<IMoBroItem> | The items to register |
Example
IEnumerable<IMoBroItem> metrics = CreateMetrics();
_mobro.Register(metrics);
Register(IMoBroItem)
Registers a single item with the service.
Parameters
Name | Type | Description |
---|---|---|
item | IMoBroItem | The 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
Name | Type | Description |
---|---|---|
ids | IEnumerable<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
Name | Type | Description |
---|---|---|
id | string | The 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 implementIMoBroItem
.
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
Name | Type | Description |
---|---|---|
id | string | The ID of the item to retrieve. |
item | T (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
Name | Type | Description |
---|---|---|
values | IEnumerable<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
Name | Type | Description |
---|---|---|
id | string | The ID of the metric. |
value | object? | The new value of the metric. |
timestamp | DateTime | The 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
Name | Type | Description |
---|---|---|
id | string | The ID of the metric. |
value | object? | 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
Name | Type | Description |
---|---|---|
value | MetricValue | The 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
Name | Type | Description |
---|---|---|
id | string | The 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
Name | Type | Description |
---|---|---|
message | string | The 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
Name | Type | Description |
---|---|---|
exception | Exception | The occurred exception. |
Example
try
{
// some logic
}
catch (Exception e)
{
_logger.Error(e, "Plugin error");
_mobro.Error(e);
}