Refactor error span handling with prioritization and predicates
Introduce `Order` property and `CanHandle` method in `IErrorSpanHandler` to enable prioritized and conditional processing of error spans. Update handler implementations to utilize these properties and streamline logic. This enhances flexibility and improves the extensibility of error span handling.
This commit is contained in:
parent
e393cca031
commit
b624b2b4af
|
|
@ -5,5 +5,7 @@ namespace Elsa.OpenTelemetry.Abstractions;
|
|||
|
||||
public abstract class ErrorSpanHandlerBase : IErrorSpanHandler
|
||||
{
|
||||
public virtual float Order => 0;
|
||||
public abstract bool CanHandle(ErrorSpanContext context);
|
||||
public abstract void Handle(ErrorSpanContext context);
|
||||
}
|
||||
|
|
@ -1,10 +1,11 @@
|
|||
using Elsa.OpenTelemetry.Abstractions;
|
||||
using Elsa.OpenTelemetry.Models;
|
||||
|
||||
namespace Elsa.OpenTelemetry.Contracts;
|
||||
|
||||
public interface IErrorSpanHandler
|
||||
{
|
||||
float Order { get; }
|
||||
bool CanHandle(ErrorSpanContext context);
|
||||
void Handle(ErrorSpanContext context);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,11 +5,12 @@ namespace Elsa.OpenTelemetry.Handlers;
|
|||
|
||||
public class DefaultErrorSpanHandler : ErrorSpanHandlerBase
|
||||
{
|
||||
public override float Order => 100000;
|
||||
|
||||
public override bool CanHandle(ErrorSpanContext context) => context.Exception != null;
|
||||
|
||||
public override void Handle(ErrorSpanContext context)
|
||||
{
|
||||
if (context.Exception is null)
|
||||
return;
|
||||
|
||||
context.Span.AddException(context.Exception);
|
||||
context.Span.AddException(context.Exception!);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,11 +6,11 @@ namespace Elsa.OpenTelemetry.Handlers;
|
|||
|
||||
public class FaultExceptionErrorSpanHandler : ErrorSpanHandlerBase
|
||||
{
|
||||
public override bool CanHandle(ErrorSpanContext context) => context.Exception is FaultException;
|
||||
|
||||
public override void Handle(ErrorSpanContext context)
|
||||
{
|
||||
if (context.Exception is not FaultException faultException)
|
||||
return;
|
||||
|
||||
var faultException = (FaultException)context.Exception!;
|
||||
var span = context.Span;
|
||||
var tags = new Dictionary<string, object?>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -46,9 +46,11 @@ public class OpenTelemetryTracingActivityExecutionMiddleware(ActivityMiddlewareD
|
|||
span.SetStatus(ActivityStatusCode.Error);
|
||||
span.SetTag("activity.incidents", true);
|
||||
|
||||
var errorSpanHandlers = context.GetServices<IErrorSpanHandler>();
|
||||
var errorSpanHandlerContext = new ErrorSpanContext(span, context.Exception);
|
||||
|
||||
var errorSpanHandlers = context.GetServices<IErrorSpanHandler>()
|
||||
.OrderBy(x => x.Order)
|
||||
.Where(x => x.CanHandle(errorSpanHandlerContext));
|
||||
|
||||
foreach (var handler in errorSpanHandlers)
|
||||
handler.Handle(errorSpanHandlerContext);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue