Skip to content

Commit

Permalink
Added default smtp body and subject template
Browse files Browse the repository at this point in the history
  • Loading branch information
cjoergensen committed Oct 30, 2023
1 parent 0762fb2 commit 1db3425
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Kibana.Alerts/Connectors/SmtpConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class SmtpSettings
public sealed class SmtpConnector(IConfiguration configuration, IHandlebars handlebars) : IConnector
{
private readonly IConfiguration configuration = configuration;
private const string DefaultSubjectTemplate = "⚠ Alert Status Update - {{Name}} has changed state to: {{ExecutionStatus.Status}} ⚠";
private const string DefaultBodyTemplate = "<html><body><p>There has been a change in the status of alert \"<strong>{{Name}}</strong>.\" It is now <strong>{{ExecutionStatus.Status}}</strong>.</p><p>For detailed information regarding this alert's status change, please follow this <a href=\"{{RuleUrl}}\">link</a>.</p></body></html>";


public async Task Send(Alert alert, IConfigurationSection configurationSection, CancellationToken cancellationToken = default)
{
Expand All @@ -23,10 +26,10 @@ public async Task Send(Alert alert, IConfigurationSection configurationSection,
var settings = new SmtpSettings();
configurationSection.Bind(settings);

var bodyTemplate = handlebars.Compile(settings.Body);
var bodyTemplate = handlebars.Compile(settings.Body ?? DefaultBodyTemplate);
var body = bodyTemplate(alert);

var subjectTemplate = handlebars.Compile(settings.Subject);
var subjectTemplate = handlebars.Compile(settings.Subject ?? DefaultSubjectTemplate);
var subject = subjectTemplate(alert);

SmtpClient client = new(host, port);
Expand Down
2 changes: 1 addition & 1 deletion src/Kibana.Alerts/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private async Task SendAlerts(Alert alert, CancellationToken cancellationToken =
foreach (var tag in alert.Tags)
{
var connectors = configuration.GetSection($"Groups:{tag}:Connectors").GetChildren().ToDictionary(x => x.Key, null).Keys.ToList();
if (!connectors.Any())
if (connectors.Count == 0)
{
logger.LogWarning("Tag {tag} on alert {alertName} not mapped to any groups in groups.json", tag, alert.Name);
continue;
Expand Down

0 comments on commit 1db3425

Please sign in to comment.