Sending an Alert
Once the configuration parameters are set for your alerting provider, you associate the alert provider with your model at the time of deployment. You can then start sending alerts from instances of your real-time digital twin model, whether it is a code-based model or a rules engine model.
Note
if no alert provider was selected at the time of deployment of the model, no alerts will be sent when the API is called. An error message will be displayed in the system log.
.NET and Java Models
In C#, the API you can use to send alerts programmatically from your model’s implementation is defined as:
public abstract class ProcessingContext<TDigitalTwin>
{
// [...]
public abstract Task SendAlertAsync(AlertMessage alertMessage);
}
alertMessage (AlertMessage): represents the content of the alert you want to send
In order to send an alert, you need to build an AlertMessage object with at least a title, message and severity. You also can provide optional custom properties as key/value pairs. The AlertMessage class is defined as follows:
/// <summary>
/// The AlertMessage objects contain all the data to send alerts to external services.
/// This includes properties such as title, message and severity. Finally, alerts can include snapshots of
/// properties along with the message.
/// </summary>
public class AlertMessage
{
/// <summary>
/// Title of the alert
/// </summary>
public string Title { get; set; }
/// <summary>
/// Severity of the alert. Stored as a string since different providers use different severity names
/// </summary>
public string Severity { get; set; }
/// <summary>
/// A more descriptive message about the alert
/// </summary>
public string Message { get; set; }
/// <summary>
/// Alerts can include additional custom properties
/// </summary>
public Dictionary<string, string> OptionalTwinInstanceProperties { get; set; } = new Dictionary<string, string>();
}
Note
Severity is expressed as a string because each provider uses different conventions. Make sure you refer to the provider’s specification for severity strings. For example, Splunk On-Call supports any strings but will not trigger an incident if the severity is not at least set to “CRITICAL”.
Example:
The following C# example shows a simple real-time digital twin model that records temperatures samples received in messages and sends an alert if at least 2 samples are over 100.
class TestAlertingProcessor : MessageProcessor<TestAlertingModel>
{
public override Task<ProcessingResult> ProcessMessageAsync(ProcessingContext<TestAlertingModel> context, TestAlertingModel digitalTwin, byte[] msgBytes)
{
TestAlertingMessage? message = System.Text.Json.JsonSerializer.Deserialize<TestAlertingMessage>(msgBytes);
if (message is null)
{
_logger.LogWarning("Message deserialization failed.");
return Task.FromResult(ProcessingResult.NoUpdate);
}
digitalTwin.RecordedTemperatures.Add(message.Temp);
if (digitalTwin.RecordedTemperatures.Where(t => t > 100).Count() > 2)
{
AlertMessage alertMessage = new AlertMessage();
alertMessage.Message = "Multiple high temperature readings observed";
alertMessage.Severity = "CRITICAL";
alertMessage.Title = "High temperature alert";
alertMessage.OptionalTwinInstanceProperties = new Dictionary<string, string>();
await context.SendAlertAsync(alertMessage);
}
return ProcessingResult.DoUpdate;
}
}
Rules Engine Models
In a rules engine model, you can trigger an alert by calling one of two built-in functions:
SEND_ALERT(severity, title, message)
SEND_ALERT_WITH_DATA(severity, title, message, dataCollection)
See SEND_ALERT and SEND_ALERT_WITH_DATA for more details.
Both of these functions take the following parameters:
severity (String): The severity of the alert
title (String): The title of the alert
message (String): The description of the alert
The only difference between the two functions is that SEND_ALERT_WITH_DATA also allows the caller to provide a data collection name, so that the alert also includes a snapshot of instance properties defined in the data collection.
Example:
IF Condition THEN SEND_ALERT("CRITICAL", "Condition was triggered", "Here are some details")