-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add error metrics tracking for workflow incidents #6622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sfmskywalker
wants to merge
11
commits into
develop/3.5.0
Choose a base branch
from
enh/otel-counters
base: develop/3.5.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6450b78
Refactor OpenTelemetry error handling implementation
sfmskywalker 3a51eb9
Add error metrics tracking for workflow incidents
sfmskywalker c4952c4
Rename handler class and improve error tagging logic
sfmskywalker a1b8a6d
Improve fault exception tagging for Datadog compatibility
sfmskywalker bcffc15
Fix incident selection by using the first instead of the last
sfmskywalker f511a8f
Align .gitignore file with consistent formatting
sfmskywalker 6d11fc8
Fix incorrect selection of activity execution context
sfmskywalker cbc238d
Merge branch 'enh/otel-error' into enh/otel-counters
sfmskywalker c5905de
Exclude docker-compose-datadog.yml from solution file.
sfmskywalker be7713c
Merge branch 'enh/otel-error' into enh/otel-counters
sfmskywalker a611b26
Merge branch 'develop/3.5.0' into enh/otel-counters
sfmskywalker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/modules/Elsa.OpenTelemetry/Abstractions/ActivityErrorSpanHandlerBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using Elsa.OpenTelemetry.Contracts; | ||
| using Elsa.OpenTelemetry.Models; | ||
|
|
||
| namespace Elsa.OpenTelemetry.Abstractions; | ||
|
|
||
| public abstract class ActivityErrorSpanHandlerBase : IActivityErrorSpanHandler | ||
| { | ||
| public virtual float Order => 0; | ||
| public abstract bool CanHandle(ActivityErrorSpanContext context); | ||
| public abstract void Handle(ActivityErrorSpanContext context); | ||
| } |
11 changes: 0 additions & 11 deletions
11
src/modules/Elsa.OpenTelemetry/Abstractions/ErrorSpanHandlerBase.cs
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/modules/Elsa.OpenTelemetry/Abstractions/WorkflowErrorSpanHandlerBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using Elsa.OpenTelemetry.Contracts; | ||
| using Elsa.OpenTelemetry.Models; | ||
|
|
||
| namespace Elsa.OpenTelemetry.Abstractions; | ||
|
|
||
| public abstract class WorkflowErrorSpanHandlerBase : IWorkflowErrorSpanHandler | ||
| { | ||
| public virtual float Order => 0; | ||
| public abstract bool CanHandle(WorkflowErrorSpanContext context); | ||
| public abstract void Handle(WorkflowErrorSpanContext context); | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/modules/Elsa.OpenTelemetry/Contracts/IActivityErrorSpanHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using Elsa.OpenTelemetry.Models; | ||
|
|
||
| namespace Elsa.OpenTelemetry.Contracts; | ||
|
|
||
| public interface IActivityErrorSpanHandler | ||
| { | ||
| float Order { get; } | ||
| bool CanHandle(ActivityErrorSpanContext context); | ||
| void Handle(ActivityErrorSpanContext context); | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/modules/Elsa.OpenTelemetry/Contracts/IErrorMetricHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using Elsa.OpenTelemetry.Models; | ||
|
|
||
| namespace Elsa.OpenTelemetry.Contracts; | ||
|
|
||
| public interface IErrorMetricHandler | ||
| { | ||
| float Order { get; } | ||
| bool CanHandle(ErrorMetricContext context); | ||
| void Handle(ErrorMetricContext context); | ||
| } |
11 changes: 0 additions & 11 deletions
11
src/modules/Elsa.OpenTelemetry/Contracts/IErrorSpanHandler.cs
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
src/modules/Elsa.OpenTelemetry/Contracts/IWorkflowErrorSpanHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using Elsa.OpenTelemetry.Models; | ||
|
|
||
| namespace Elsa.OpenTelemetry.Contracts; | ||
|
|
||
| public interface IWorkflowErrorSpanHandler | ||
| { | ||
| float Order { get; } | ||
| bool CanHandle(WorkflowErrorSpanContext context); | ||
| void Handle(WorkflowErrorSpanContext context); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/modules/Elsa.OpenTelemetry/Handlers/DefaultErrorSpanHandler.cs
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
src/modules/Elsa.OpenTelemetry/Handlers/DefaultExceptionHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using Elsa.OpenTelemetry.Contracts; | ||
| using Elsa.OpenTelemetry.Models; | ||
|
|
||
| namespace Elsa.OpenTelemetry.Handlers; | ||
|
|
||
| public class DefaultExceptionHandler : IActivityErrorSpanHandler, IWorkflowErrorSpanHandler, IErrorMetricHandler | ||
| { | ||
| public float Order => 100000; | ||
| public bool CanHandle(ErrorMetricContext context) => true; | ||
| public bool CanHandle(WorkflowErrorSpanContext context) => true; | ||
| public bool CanHandle(ActivityErrorSpanContext context) => context.Exception != null; | ||
|
|
||
| public void Handle(WorkflowErrorSpanContext context) | ||
| { | ||
| // No-op. | ||
| } | ||
|
|
||
| public void Handle(ActivityErrorSpanContext context) | ||
| { | ||
| context.Span.AddException(context.Exception!); | ||
| } | ||
|
|
||
| public void Handle(ErrorMetricContext context) | ||
| { | ||
| context.Tags["error.exception.type"] = context.Exception.GetType().Name; | ||
| } | ||
| } |
23 changes: 0 additions & 23 deletions
23
src/modules/Elsa.OpenTelemetry/Handlers/FaultExceptionErrorSpanHandler.cs
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
src/modules/Elsa.OpenTelemetry/Handlers/FaultExceptionHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using Elsa.OpenTelemetry.Contracts; | ||
| using Elsa.OpenTelemetry.Models; | ||
| using Elsa.Workflows.Exceptions; | ||
|
|
||
| namespace Elsa.OpenTelemetry.Handlers; | ||
|
|
||
| public class FaultExceptionHandler : IActivityErrorSpanHandler, IWorkflowErrorSpanHandler, IErrorMetricHandler | ||
| { | ||
| public float Order => 0; | ||
|
|
||
| public bool CanHandle(ActivityErrorSpanContext context) => context.Exception is FaultException; | ||
| public bool CanHandle(WorkflowErrorSpanContext context) => context.Exception is FaultException; | ||
| public bool CanHandle(ErrorMetricContext context) => context.Exception is FaultException; | ||
|
|
||
| public void Handle(ActivityErrorSpanContext context) | ||
| { | ||
| var faultException = (FaultException)context.Exception!; | ||
| var span = context.Span; | ||
| var tags = new Dictionary<string, object?>() | ||
| { | ||
| ["exception.code"] = faultException.Code, | ||
| ["exception.category"] = faultException.Category, | ||
| ["exception.type"] = faultException.Type | ||
| }; | ||
| span.AddException(faultException, new(tags.ToArray())); | ||
| } | ||
|
|
||
| public void Handle(WorkflowErrorSpanContext context) | ||
| { | ||
| var faultException = (FaultException)context.Exception!; | ||
| var span = context.Span; | ||
|
|
||
| span.SetTag("error.code", faultException.Code); | ||
| span.SetTag("error.category", faultException.Category); | ||
| span.SetTag("error_details.type", faultException.Type); | ||
| } | ||
|
|
||
| public void Handle(ErrorMetricContext context) | ||
| { | ||
| var faultException = (FaultException)context.Exception!; | ||
sfmskywalker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| context.Tags["error.code"] = faultException.Code; | ||
| context.Tags["error.category"] = faultException.Category; | ||
| context.Tags["error.type"] = faultException.Type; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.Diagnostics.Metrics; | ||
| using Elsa.OpenTelemetry.Contracts; | ||
| using Elsa.OpenTelemetry.Models; | ||
| using Elsa.Workflows; | ||
|
|
||
| namespace Elsa.OpenTelemetry.Metrics; | ||
|
|
||
| /// Tracks and records metrics related to workflow incidents. | ||
| public class ErrorMetrics | ||
| { | ||
| private readonly IEnumerable<IErrorMetricHandler> _errorMetricHandlers; | ||
| private readonly Counter<long> _errorCounter; | ||
|
|
||
| /// Tracks and records metrics related to workflow incidents. | ||
| public ErrorMetrics(IMeterFactory meterFactory, IEnumerable<IErrorMetricHandler> errorMetricHandlers) | ||
| { | ||
| var meter = meterFactory.Create(MeterName); | ||
| _errorMetricHandlers = errorMetricHandlers; | ||
| _errorCounter = meter.CreateCounter<long>( | ||
| name: "workflow_incident_count", | ||
| description: "Counts workflow incidents" | ||
| ); | ||
| } | ||
|
|
||
| public const string MeterName = "Elsa.OpenTelemetry.Incidents"; | ||
|
|
||
| /// Track | ||
| public void TrackError(ActivityExecutionContext context) | ||
| { | ||
| var exception = context.Exception; | ||
|
|
||
| if (exception == null) | ||
| return; | ||
|
|
||
| var workflow = context.WorkflowExecutionContext.Workflow; | ||
| var tags = new Dictionary<string, object?>() | ||
| { | ||
| ["activity.type"] = context.Activity.Type, | ||
| ["workflow.definition.id"] = workflow.Identity.DefinitionId, | ||
| ["workflow.definition.version"] = workflow.Identity.Version | ||
| }; | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(workflow.WorkflowMetadata.Name)) | ||
| tags["workflow.definition.name"] = workflow.WorkflowMetadata.Name; | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(workflow.Identity.TenantId)) | ||
| tags["tenant.id"] = workflow.Identity.TenantId; | ||
|
|
||
| var errorMetricContext = new ErrorMetricContext(_errorCounter, exception, tags); | ||
| var errorMetricHandlers = _errorMetricHandlers | ||
| .OrderBy(x => x.Order) | ||
| .Where(x => x.CanHandle(errorMetricContext)); | ||
|
|
||
| foreach (var handler in errorMetricHandlers) | ||
| handler.Handle(errorMetricContext); | ||
|
|
||
| _errorCounter.Add(1, tags.ToArray()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Include a brief comment explaining the purpose of the ErrorMetrics registration to aid future maintainability.