private Task <IIndexResponse> CreateSystemEvent(EventRequestModel model, CancellationToken cancellationToken) { if (model.Tags == null) { model.Tags = new List <string>(); } model.Tags.Add(model.Level.ToString()); model.Tags.Add(model.Category); model.Tags.Add(model.TargetKey); // Remove duplicates var hashset = model.Tags.ToHashSet(); var eventTimestamp = _timeStampFactory.GetTimestamp(); var systemEvent = new SystemEventElasticsearchDocument { Category = model.Category, Level = model.Level.ToString(), TargetKey = model.TargetKey, Message = $"{model.Message} by {model.Sender}", Tags = hashset.ToArray(), Sender = model.Sender, Timestamp = eventTimestamp, Endtime = eventTimestamp }; return(_esClient.IndexAsync(systemEvent, cancellationToken: cancellationToken)); }
private Task SendSnsNotification(string notificationMessage, CategorySubscription categorySubscription, SystemEventElasticsearchDocument document, CancellationToken cancellationToken) { var message = JsonConvert.SerializeObject(new { Event = document, notificationMessage }); return(_amazonSimpleNotificationService.PublishAsync( categorySubscription.TopicArn, message, cancellationToken)); }
private Task <string> SendSlackNotification(string notificationMessage, CategorySubscription categorySubscription, SystemEventElasticsearchDocument document, CancellationToken cancellationToken) { var message = new Message(notificationMessage) .SetUserWithEmoji(_SystemName, _SystemIcon); message.AddAttachment(new Attachment() .AddField("Event Id", document.Id, true) .AddField("Message", document.Message, true) .AddField("Target", document.TargetKey, true) .AddField("Sender", document.Sender, true) .AddField("Start Time", document.Timestamp, true) .AddField("End Time", document.Endtime, true) .AddField("Level", document.Level, true) .SetColor((document.Level == "critical")? "#eb4034": "#349ceb") ); return(_slackService.SendAsync(message, categorySubscription.WebhookUrl)); }
public async Task <IIndexResponse> IndexAsync(SystemEventElasticsearchDocument document, CancellationToken cancellationToken) { string indexName = null; var latency = PreInvoke(_clientName, _indexMethodName, document); try { indexName = _indexFactory.GetIndexName(); var result = await _esClient.IndexAsync(new IndexRequest <SystemEventElasticsearchDocument>( document, indexName), cancellationToken : cancellationToken); PostInvokeSuccess(latency, _clientName, _indexMethodName, document, indexName); return(result); } catch (Exception ex) { PostInvokeFailure(_clientName, _indexMethodName, ex, document, indexName); throw; } }
private async Task SendNotification(string notificationMessage, string category, SystemEventElasticsearchDocument document, CancellationToken cancellationToken) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (string.IsNullOrWhiteSpace(category)) { throw new ArgumentException($"The {nameof(category)} can not be null or whitespace"); } _logger.LogInformation($"Sending notification for category {category}"); var categorySubscriptions = new List <CategorySubscription>(); // If there are subscriptions to * then // send this notification to those channels. if (_configurationToNotificationChannelMap.ContainsKey(_allCategoriesKey)) { categorySubscriptions.AddRange(_configurationToNotificationChannelMap[_allCategoriesKey]); } if (_configurationToNotificationChannelMap.ContainsKey(category)) { categorySubscriptions.AddRange(_configurationToNotificationChannelMap[category]); } _logger.LogInformation($"Number of category subs {categorySubscriptions.Count}"); foreach (var categorySubscription in categorySubscriptions) { _logger.LogInformation($"categorySubscription.Type {categorySubscription.Type}"); try { switch (categorySubscription.Type) { case NotificationChannelType.Slack: await SendSlackNotification(notificationMessage, categorySubscription, document, cancellationToken); break; case NotificationChannelType.Sns: await SendSnsNotification(notificationMessage, categorySubscription, document, cancellationToken); break; default: _logger.LogInformation( $"The provided notification channel {categorySubscription.Type} is not supported"); break; } } catch { // Ignored so a failure in one channel does not // stops the rest of the notifications from been sent. } } }
public Task OnEventFinished(string category, SystemEventElasticsearchDocument document, CancellationToken cancellationToken) { return(SendNotification( _onEventFinishedNotificationMessage, category, document, cancellationToken)); }