Skip to content

Commit 52ab6f4

Browse files
Tagging Exception for jaegar ui and opentelemetry added
1 parent 7169067 commit 52ab6f4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

SharedLibrary/Middlewares/ExceptionHandlingMiddleware.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.Extensions.Logging;
3+
using System.Diagnostics;
4+
using OpenTelemetry.Trace; // <- this brings in Activity.RecordException(...)
35

46
namespace SharedLibrary.Middlewares
57
{
@@ -29,6 +31,25 @@ public async Task Invoke(HttpContext context)
2931
}
3032
catch (Exception ex)
3133
{
34+
35+
var activity = Activity.Current;
36+
if (activity is not null)
37+
{
38+
var tags = new ActivityTagsCollection
39+
{
40+
["exception.type"] = ex.GetType().FullName,
41+
["exception.message"] = ex.Message,
42+
["exception.stacktrace"] = ex.ToString(),
43+
};
44+
45+
// add the OpenTelemetry "exception" event to the current span
46+
activity.AddEvent(new ActivityEvent("exception", tags: tags));
47+
48+
// mark the span as error so it shows red in Jaeger
49+
activity.SetStatus(ActivityStatusCode.Error, ex.Message);
50+
}
51+
52+
3253
// Log using Serilog (through ILogger)
3354
_logger.LogError(ex, "Unhandled exception occurred while processing request {Path}", context.Request.Path);
3455

SharedLibrary/SharedLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<ItemGroup>
1010
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.9" />
12+
<PackageReference Include="OpenTelemetry" Version="1.13.1" />
1213
</ItemGroup>
1314

1415
</Project>

0 commit comments

Comments
 (0)