diff --git a/src/Application/Masa.Alert.Application/AlarmRules/EventHandler/SendAlarmNotificationEventHandler.cs b/src/Application/Masa.Alert.Application/AlarmRules/EventHandler/SendAlarmNotificationEventHandler.cs index c6666a4..1852ceb 100644 --- a/src/Application/Masa.Alert.Application/AlarmRules/EventHandler/SendAlarmNotificationEventHandler.cs +++ b/src/Application/Masa.Alert.Application/AlarmRules/EventHandler/SendAlarmNotificationEventHandler.cs @@ -8,14 +8,20 @@ public class SendAlarmNotificationEventHandler private readonly INotificationSender _notificationSender; private readonly IAlarmHistoryRepository _repository; private readonly IAlarmRuleRepository _alarmRuleRepository; + private readonly ITscClient _tscClient; + private readonly AlarmRuleDomainService _domainService; public SendAlarmNotificationEventHandler(INotificationSender notificationSender , IAlarmHistoryRepository repository - , IAlarmRuleRepository alarmRuleRepository) + , IAlarmRuleRepository alarmRuleRepository + , ITscClient tscClient + , AlarmRuleDomainService domainService) { _notificationSender = notificationSender; _repository = repository; _alarmRuleRepository = alarmRuleRepository; + _tscClient = tscClient; + _domainService = domainService; } [EventHandler] @@ -35,6 +41,11 @@ public async Task HandleEventAsync(SendAlarmNotificationEvent eto) if (alarmRule != null) { variables.TryAdd(AlertConsts.ALARM_RULE_NAME_NOTIFICATION_TEMPLATE_VAR_NAME, alarmRule.DisplayName); + + if (alarmRule.Type == AlarmRuleTypes.Log) + { + await AddLogVariablesAsync(alarmRule, variables); + } } await _notificationSender.SendAsync(notificationConfig, variables); @@ -43,4 +54,44 @@ public async Task HandleEventAsync(SendAlarmNotificationEvent eto) alarm.Notification(); await _repository.UpdateAsync(alarm); } + + private async Task AddLogVariablesAsync(AlarmRule alarmRule, Dictionary variables) + { + var checkTime = DateTimeOffset.Now; + var latest = await _domainService.GetLatest(alarmRule.Id); + var startTime = alarmRule.GetStartCheckTime(checkTime, latest); + if (startTime == null) + return; + + var request = new LogLatestRequest + { + Query = alarmRule.WhereExpression, + Start = startTime.Value.UtcDateTime, + End = checkTime.UtcDateTime, + IsDesc = true + }; + + var log = await _tscClient.LogService.GetLatestAsync(request); + if (log == null) + return; + + var prefix = "Log"; + variables.TryAdd($"{prefix}.{nameof(log.Timestamp)}", log.Timestamp); + variables.TryAdd($"{prefix}.{nameof(log.TraceId)}", log.TraceId); + variables.TryAdd($"{prefix}.{nameof(log.SpanId)}", log.SpanId); + variables.TryAdd($"{prefix}.{nameof(log.TraceFlags)}", log.TraceFlags); + variables.TryAdd($"{prefix}.{nameof(log.SeverityText)}", log.SeverityText); + variables.TryAdd($"{prefix}.{nameof(log.SeverityNumber)}", log.SeverityNumber); + variables.TryAdd($"{prefix}.{nameof(log.Body)}", log.Body); + + foreach (var resource in log.Resource) + { + variables.TryAdd($"{prefix}.{nameof(log.Resource)}.{nameof(resource.Key)}", resource.Value); + } + + foreach (var attribute in log.Attributes) + { + variables.TryAdd($"{prefix}.{nameof(log.Attributes)}.{nameof(attribute.Key)}", attribute.Value); + } + } } diff --git a/src/Application/Masa.Alert.Application/_Imports.cs b/src/Application/Masa.Alert.Application/_Imports.cs index 041f581..774de17 100644 --- a/src/Application/Masa.Alert.Application/_Imports.cs +++ b/src/Application/Masa.Alert.Application/_Imports.cs @@ -56,4 +56,5 @@ global using Masa.BuildingBlocks.Caching; global using Masa.Alert.Domain.Shared.Consts; global using Masa.BuildingBlocks.Extensions.BackgroundJobs; -global using Masa.Alert.Application.AlarmRules.Commands; \ No newline at end of file +global using Masa.Alert.Application.AlarmRules.Commands; +global using Masa.BuildingBlocks.StackSdks.Tsc.Contracts.Log; \ No newline at end of file diff --git a/src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertDbContext.cs b/src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertDbContext.cs index 3c02b10..969f60e 100644 --- a/src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertDbContext.cs +++ b/src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertDbContext.cs @@ -17,7 +17,7 @@ protected override void OnModelCreatingExecuting(ModelBuilder builder) protected override void OnConfiguring(MasaDbContextOptionsBuilder optionsBuilder) { optionsBuilder.DbContextOptionsBuilder - .LogTo(Console.WriteLine, LogLevel.Information) + .LogTo(Console.WriteLine, LogLevel.Error) .EnableSensitiveDataLogging() .EnableDetailedErrors(); }