-
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathSentryStartup.cs
More file actions
82 lines (69 loc) · 3.03 KB
/
SentryStartup.cs
File metadata and controls
82 lines (69 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Google.Cloud.Functions.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Sentry;
using Sentry.AspNetCore;
using Sentry.Extensibility;
using Sentry.Reflection;
namespace Google.Cloud.Functions.Framework
{
/// <summary>
/// Starts up the GCP Function integration.
/// </summary>
public class SentryStartup : FunctionsStartup
{
/// <summary>
/// Configure Sentry logging.
/// </summary>
public override void ConfigureLogging(WebHostBuilderContext context, ILoggingBuilder logging)
{
base.ConfigureLogging(context, logging);
logging.AddConfiguration(context.Configuration);
logging.Services.AddSingleton<ISentryEventProcessor, SentryGoogleCloudFunctionEventProcessor>();
// TODO: refactor this with SentryWebHostBuilderExtensions
var section = context.Configuration.GetSection("Sentry");
logging.Services.Configure<SentryAspNetCoreOptions>(section);
logging.Services.Configure<SentryAspNetCoreOptions>(options =>
{
// Make sure all events are flushed out
options.FlushBeforeRequestCompleted = true;
});
logging.Services.AddSingleton<IConfigureOptions<SentryAspNetCoreOptions>, SentryAspNetCoreOptionsSetup>();
logging.Services.AddSingleton<ILoggerProvider, SentryAspNetCoreLoggerProvider>();
logging.AddFilter<SentryAspNetCoreLoggerProvider>(
"Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware",
LogLevel.None);
logging.Services.AddSentry();
}
/// <summary>
/// Configure Sentry services.
/// </summary>
/// <param name="context"></param>
/// <param name="services"></param>
public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
{
base.ConfigureServices(context, services);
services.AddTransient<IStartupFilter, SentryStartupFilter>();
}
private class SentryGoogleCloudFunctionEventProcessor : ISentryEventProcessor
{
private static readonly SdkVersion NameAndVersion
= typeof(SentryStartup).Assembly.GetNameAndVersion();
private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name;
private const string SdkName = "sentry.dotnet.google-cloud-function";
public SentryEvent Process(SentryEvent @event)
{
// Take over the SDK name since this wraps ASP.NET Core
@event.Sdk.Name = SdkName;
@event.Sdk.Version = NameAndVersion.Version;
if (NameAndVersion.Version != null)
{
@event.Sdk.AddPackage(ProtocolPackageName, NameAndVersion.Version);
}
return @event;
}
}
}
}