-
-
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.
Update dependencies with vulnerabilities
- update OpenTelemetry - update MassTransit - remove Bugsnag.AspNet.Core, implement natively - fixes #544
- Loading branch information
Showing
14 changed files
with
179 additions
and
16 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using Bugsnag.AspNet.Core; | ||
using Hangfire; | ||
using OpenTelemetry.Trace; | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using Bugsnag.AspNet.Core; | ||
using OpenTelemetry.Trace; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
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
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
62 changes: 62 additions & 0 deletions
62
src/ServiceToolkit/src/SIL.ServiceToolkit/Services/BugsnagMiddleware.cs
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,62 @@ | ||
namespace SIL.ServiceToolkit.Services; | ||
|
||
/// <summary> | ||
/// The Bugsnag AspNetCore middleware. | ||
/// | ||
/// See https://github.com/bugsnag/bugsnag-dotnet for original source. | ||
/// </summary> | ||
public class BugsnagMiddleware(RequestDelegate requestDelegate) | ||
{ | ||
public const string HttpContextItemsKey = "Bugsnag.Client"; | ||
|
||
private readonly RequestDelegate _next = requestDelegate; | ||
|
||
public async Task Invoke(HttpContext context, Bugsnag.IClient client) | ||
{ | ||
if (client.Configuration.AutoCaptureSessions) | ||
client.SessionTracking.CreateSession(); | ||
|
||
// capture the request information now as the http context | ||
// may be changed by other error handlers after an exception | ||
// has occurred | ||
Bugsnag.Payload.Request bugsnagRequestInformation = ToRequest(context); | ||
|
||
client.BeforeNotify(report => | ||
{ | ||
report.Event.Request = bugsnagRequestInformation; | ||
}); | ||
|
||
context.Items[HttpContextItemsKey] = client; | ||
|
||
if (client.Configuration.AutoNotify) | ||
{ | ||
try | ||
{ | ||
await _next(context); | ||
} | ||
catch (Exception exception) | ||
{ | ||
client.Notify(exception, Bugsnag.Payload.HandledState.ForUnhandledException()); | ||
throw; | ||
} | ||
} | ||
else | ||
{ | ||
await _next(context); | ||
} | ||
} | ||
|
||
private static Bugsnag.Payload.Request ToRequest(HttpContext httpContext) | ||
{ | ||
IPAddress? ip = httpContext.Connection.RemoteIpAddress ?? httpContext.Connection.LocalIpAddress; | ||
|
||
return new Bugsnag.Payload.Request | ||
{ | ||
ClientIp = ip?.ToString(), | ||
Headers = httpContext.Request.Headers.ToDictionary(x => x.Key, x => string.Join(",", x.Value!)), | ||
HttpMethod = httpContext.Request.Method, | ||
Url = httpContext.Request.GetDisplayUrl(), | ||
Referer = httpContext.Request.Headers[HeaderNames.Referer], | ||
}; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/ServiceToolkit/src/SIL.ServiceToolkit/Services/BugsnagStartupFilter.cs
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,73 @@ | ||
namespace SIL.ServiceToolkit.Services; | ||
|
||
/// <summary> | ||
/// A startup filter to ensure that the Bugsnag middleware is | ||
/// executed at the start of the middleware stack. | ||
/// | ||
/// See https://github.com/bugsnag/bugsnag-dotnet for original source. | ||
/// </summary> | ||
public class BugsnagStartupFilter : IStartupFilter | ||
{ | ||
static BugsnagStartupFilter() | ||
{ | ||
// populate the env variable that the client expects with the netcore | ||
// provided value unless it has already been specified | ||
if (Environment.GetEnvironmentVariable("BUGSNAG_RELEASE_STAGE") == null) | ||
{ | ||
Environment.SetEnvironmentVariable( | ||
"BUGSNAG_RELEASE_STAGE", | ||
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") | ||
); | ||
} | ||
} | ||
|
||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||
{ | ||
return builder => | ||
{ | ||
builder | ||
.ApplicationServices.GetService<DiagnosticListener>() | ||
?.SubscribeWithAdapter(new DiagnosticSubscriber()); | ||
builder.UseMiddleware<BugsnagMiddleware>(); | ||
next(builder); | ||
}; | ||
} | ||
|
||
private class DiagnosticSubscriber | ||
{ | ||
/// <summary> | ||
/// Handles exceptions that the Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware | ||
/// swallows. | ||
/// </summary> | ||
/// <param name="exception"></param> | ||
/// <param name="httpContext"></param> | ||
[DiagnosticName("Microsoft.AspNetCore.Diagnostics.HandledException")] | ||
public virtual void OnHandledException(Exception exception, HttpContext httpContext) | ||
{ | ||
LogException(exception, httpContext); | ||
} | ||
|
||
/// <summary> | ||
/// Handles exceptions that the Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware | ||
/// swallows. | ||
/// </summary> | ||
/// <param name="exception"></param> | ||
/// <param name="httpContext"></param> | ||
[DiagnosticName("Microsoft.AspNetCore.Diagnostics.UnhandledException")] | ||
public virtual void OnUnhandledException(Exception exception, HttpContext httpContext) | ||
{ | ||
LogException(exception, httpContext); | ||
} | ||
|
||
private static void LogException(Exception exception, HttpContext httpContext) | ||
{ | ||
httpContext.Items.TryGetValue(BugsnagMiddleware.HttpContextItemsKey, out object? clientObject); | ||
|
||
if (clientObject is Bugsnag.IClient client) | ||
{ | ||
if (client.Configuration.AutoNotify) | ||
client.Notify(exception, Bugsnag.Payload.HandledState.ForUnhandledException()); | ||
} | ||
} | ||
} | ||
} |
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