Skip to content
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

custom exception handling option #60

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading