Skip to content

Commit

Permalink
Merge pull request #60 from nenoNaninu/f/custom_exception_handler
Browse files Browse the repository at this point in the history
custom exception handling option
  • Loading branch information
nenoNaninu authored Dec 18, 2024
2 parents 7aeea4a + ce2b769 commit 2811d24
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This is an [Instrumentation Library](https://github.com/open-telemetry/opentelem

- [Install](#install)
- [Usage](#usage)
- [Exception Handling](#exception-handling)
- [Example](#example)
- [Related Work](#related-work)

Expand Down Expand Up @@ -42,6 +43,27 @@ builder.Services.AddOpenTelemetry()
});
```

### Exception Handling

By setting the `OnException` option, you can override the attributes that this library writes by default.
For example, when an exception occurs inside your SignalR hub method, this library sets the `otel.status_code` attribute to `ERROR`.
However, there are cases where you do not want a specific exception to be `ERROR`.
In that case, you can override the default attribute by setting it as follows.

```cs
builder.Services.AddSignalR()
.AddHubInstrumentation(options =>
{
options.OnException = static (activity, exception) =>
{
if (exception is HubException)
{
activity.SetTag("otel.status_code", "OK");
}
};
});
```

## Example

The example code architecture is as follows.
Expand Down
23 changes: 22 additions & 1 deletion src/AspNetCore.SignalR.OpenTelemetry/HubInstrumentationFilter.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using AspNetCore.SignalR.OpenTelemetry.Internal;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.Http.Connections.Features;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace AspNetCore.SignalR.OpenTelemetry;

public sealed class HubInstrumentationFilter : IHubFilter
{
private readonly ILogger _logger;
private readonly HubInstrumentationOptions _options;

public HubInstrumentationFilter(ILoggerFactory loggerFactory)
public HubInstrumentationFilter(ILoggerFactory loggerFactory, IOptions<HubInstrumentationOptions> options)
{
_logger = loggerFactory.CreateLogger("AspNetCore.SignalR.Logging.HubLoggingFilter");
_options = options.Value;
}

public async ValueTask<object?> InvokeMethodAsync(
Expand Down Expand Up @@ -46,6 +50,9 @@ public HubInstrumentationFilter(ILoggerFactory loggerFactory)
catch (Exception exception)
{
HubActivitySource.StopInvocationActivityError(activity, exception);

InvokeOptionExceptionHandler(activity, exception);

throw;
}
}
Expand Down Expand Up @@ -75,6 +82,9 @@ public async Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeC
catch (Exception exception)
{
HubActivitySource.StopInvocationActivityError(activity, exception);

InvokeOptionExceptionHandler(activity, exception);

throw;
}
}
Expand Down Expand Up @@ -113,7 +123,18 @@ public async Task OnDisconnectedAsync(
catch (Exception ex)
{
HubActivitySource.StopInvocationActivityError(activity, ex);

InvokeOptionExceptionHandler(activity, ex);

throw;
}
}

private void InvokeOptionExceptionHandler(Activity? activity, Exception exception)
{
if (_options.OnException is not null && activity is not null && activity.IsAllDataRequested)
{
_options.OnException(activity, exception);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Diagnostics;

namespace AspNetCore.SignalR.OpenTelemetry;

public sealed class HubInstrumentationOptions
{
public Action<Activity, Exception>? OnException { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand All @@ -7,6 +8,11 @@ namespace AspNetCore.SignalR.OpenTelemetry;
public static class SignalRServerBuilderExtensions
{
public static ISignalRServerBuilder AddHubInstrumentation(this ISignalRServerBuilder builder)
{
return builder.AddHubInstrumentation(_ => { });
}

public static ISignalRServerBuilder AddHubInstrumentation(this ISignalRServerBuilder builder, Action<HubInstrumentationOptions> configure)
{
builder.Services.TryAddSingleton<HubInstrumentationFilter>();

Expand All @@ -15,6 +21,8 @@ public static ISignalRServerBuilder AddHubInstrumentation(this ISignalRServerBui
options.AddFilter<HubInstrumentationFilter>();
});

builder.Services.Configure(configure);

return builder;
}
}

0 comments on commit 2811d24

Please sign in to comment.