-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from tvorova-tvornica/add-tracing-decorator
Implement tracing job decorator
- Loading branch information
Showing
9 changed files
with
112 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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,6 @@ | ||
namespace AnagramSolver.BackgroundJobs; | ||
|
||
public interface IJob<T> | ||
{ | ||
Task ExecuteAsync(T jobData); | ||
} |
This file contains 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,57 @@ | ||
|
||
using System.Runtime.ExceptionServices; | ||
using Sentry; | ||
|
||
namespace AnagramSolver.BackgroundJobs; | ||
|
||
public class TracingJobDecorator<T> : IJob<T> | ||
{ | ||
private readonly IJob<T> _decorated; | ||
private readonly Func<IHub> _getHub; | ||
|
||
public TracingJobDecorator( | ||
IJob<T> decorated, | ||
Func<IHub> getHub) | ||
{ | ||
_decorated = decorated; | ||
_getHub = getHub; | ||
} | ||
|
||
public async Task ExecuteAsync(T jobData) | ||
{ | ||
var hub = _getHub(); | ||
|
||
if (!hub.IsEnabled) | ||
{ | ||
await _decorated.ExecuteAsync(jobData).ConfigureAwait(false); | ||
return; | ||
} | ||
|
||
var transaction = _getHub().StartTransaction("BackgroundJob", $"{_decorated.GetType().Name}"); | ||
|
||
hub.ConfigureScope(scope => { | ||
scope.Transaction = transaction; | ||
}); | ||
|
||
Exception? exception = null; | ||
try | ||
{ | ||
await _decorated.ExecuteAsync(jobData).ConfigureAwait(false); | ||
} | ||
catch(Exception e) | ||
{ | ||
exception = e; | ||
} | ||
finally | ||
{ | ||
if (exception is null) | ||
{ | ||
transaction.Finish(SpanStatus.Ok); | ||
} | ||
else | ||
{ | ||
transaction.Finish(exception); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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