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

File system persistence for sessions #1105

Merged
merged 20 commits into from
Jul 14, 2021
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- ASP.NET Core: fix handled not being set for Handled exceptions ([#1111](https://github.com/getsentry/sentry-dotnet/pull/1111))

### Features

- File system persistence for sessions ([#1105](https://github.com/getsentry/sentry-dotnet/pull/1105))

## 3.7.0

### Features
Expand Down
130 changes: 122 additions & 8 deletions samples/Sentry.Samples.Google.Cloud.Functions/Function.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,134 @@
using System;
#nullable enable
using System;
using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using Google.Cloud.Functions.Hosting;
using Microsoft.Extensions.Logging;
using Sentry;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Sentry.AspNetCore;
using Sentry.Extensibility;
using Sentry.Reflection;

[assembly: FunctionsStartup(typeof(SentryStartup))]
[assembly: FunctionsStartup(typeof(SimpleHttpFunction.SentryStartup))]

public class Function : IHttpFunction
namespace SimpleHttpFunction
{
private readonly ILogger<Function> _logger;
public Function(ILogger<Function> logger) => _logger = logger;
public class Function : IHttpFunction
{
private readonly ILogger<Function> _logger;
public Function(ILogger<Function> logger) => _logger = logger;

public Task HandleAsync(HttpContext context)
{
_logger.LogInformation("Useful info that is added to the breadcrumb list.");
_logger.LogError("Is Sentry enabled? " + SentrySdk.IsEnabled);
SentrySdk.CaptureMessage("hello from GCP Functions");
throw new Exception("Bad function");
}
}

/// <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.FlushOnCompletedRequest = true;
options.DiagnosticLogger = new TestConsoleDiagnosticLogger(options.DiagnosticLevel);
});

logging.Services.AddSingleton<IConfigureOptions<SentryAspNetCoreOptions>, SentryAspNetCoreOptionsSetup>();
logging.Services.AddSingleton<ILoggerProvider, SentryAspNetCoreLoggerProvider>();

logging.AddFilter<SentryAspNetCoreLoggerProvider>(
"Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware",
LogLevel.None);
logging.AddFilter<SentryAspNetCoreLoggerProvider>(
"Sentry*",
LogLevel.Debug);
logging.Services.AddSentry();
}

public Task HandleAsync(HttpContext context)
/// <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;
}
}
}
public class TestConsoleDiagnosticLogger : IDiagnosticLogger
{
_logger.LogInformation("Useful info that is added to the breadcrumb list.");
throw new Exception("Bad function");
private readonly SentryLevel _minimalLevel;

/// <summary>
/// Creates a new instance of <see cref="ConsoleDiagnosticLogger"/>.
/// </summary>
public TestConsoleDiagnosticLogger(SentryLevel minimalLevel) {
_minimalLevel = minimalLevel;
}

/// <summary>
/// Whether the logger is enabled to the defined level.
/// </summary>
public bool IsEnabled(SentryLevel level) {
return level >= _minimalLevel;
}

/// <summary>
/// Log message with level, exception and parameters.
/// </summary>
public void Log(SentryLevel logLevel, string message, Exception? exception = null, params object?[] args)
{
Console.Write($@"{logLevel,7}: {string.Format(message, args)}
{exception}");
}

}
}
Loading