Add incident timestamp

This commit is contained in:
Sipke Schoorstra 2023-09-14 11:19:05 +02:00
parent 03b608410b
commit 45169d0e71
4 changed files with 18 additions and 5 deletions

View file

@ -25,7 +25,7 @@ internal class ActivityIncidentStateMapper
/// <returns>The mapped <see cref="ActivityIncident"/>.</returns>
public ActivityIncident Map(ProtoActivityIncident source)
{
return new(source.ActivityId, source.ActivityType, source.Message, _exceptionMapper.Map(source.Exception));
return new(source.ActivityId, source.ActivityType, source.Message, _exceptionMapper.Map(source.Exception), DateTimeOffset.Parse(source.Timestamp));
}
/// <summary>
@ -49,6 +49,7 @@ internal class ActivityIncidentStateMapper
Message = source.Message,
ActivityType = source.ActivityType,
ActivityId = source.ActivityId,
Timestamp = source.Timestamp.ToString("O")
};
}

View file

@ -34,6 +34,7 @@ message ActivityIncident {
string ActivityType = 2;
string Message = 3;
optional ExceptionState Exception = 4;
string Timestamp = 5; // ISO 8601
}
message ExceptionState {

View file

@ -1,3 +1,4 @@
using Elsa.Common.Contracts;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Models;
using Elsa.Workflows.Core.Pipelines.ActivityExecution;
@ -24,15 +25,17 @@ public class ExceptionHandlingMiddleware : IActivityExecutionMiddleware
{
private readonly ActivityMiddlewareDelegate _next;
private readonly IIncidentStrategyResolver _incidentStrategyResolver;
private readonly ISystemClock _systemClock;
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
/// <summary>
/// Constructor.
/// </summary>
public ExceptionHandlingMiddleware(ActivityMiddlewareDelegate next, IIncidentStrategyResolver incidentStrategyResolver, ILogger<ExceptionHandlingMiddleware> logger)
public ExceptionHandlingMiddleware(ActivityMiddlewareDelegate next, IIncidentStrategyResolver incidentStrategyResolver, ISystemClock systemClock, ILogger<ExceptionHandlingMiddleware> logger)
{
_next = next;
_incidentStrategyResolver = incidentStrategyResolver;
_systemClock = systemClock;
_logger = logger;
}
@ -51,7 +54,8 @@ public class ExceptionHandlingMiddleware : IActivityExecutionMiddleware
var activity = context.Activity;
var exceptionState = ExceptionState.FromException(e);
var incident = new ActivityIncident(activity.Id, activity.Type, e.Message, exceptionState);
var now = _systemClock.UtcNow;
var incident = new ActivityIncident(activity.Id, activity.Type, e.Message, exceptionState, now);
context.WorkflowExecutionContext.Incidents.Add(incident);
var strategy = await _incidentStrategyResolver.ResolveStrategyAsync(context);

View file

@ -16,7 +16,7 @@ public class ActivityIncident
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ActivityIncident"/> class.
/// </summary>
@ -24,12 +24,14 @@ public class ActivityIncident
/// <param name="activityType">The type of the activity that caused the incident.</param>
/// <param name="message">The message of the incident.</param>
/// <param name="exception">The exception that caused the incident.</param>
public ActivityIncident(string activityId, string activityType, string message, ExceptionState? exception)
/// <param name="timestamp">The timestamp of the incident.</param>
public ActivityIncident(string activityId, string activityType, string message, ExceptionState? exception, DateTimeOffset timestamp)
{
ActivityId = activityId;
ActivityType = activityType;
Message = message;
Exception = exception;
Timestamp = timestamp;
}
/// <summary>The ID of the activity that caused the incident.</summary>
@ -43,4 +45,9 @@ public class ActivityIncident
/// <summary>The exception that caused the incident.</summary>
public ExceptionState? Exception { get; }
/// <summary>
/// The timestamp of the incident.
/// </summary>
public DateTimeOffset Timestamp { get; }
}