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

Google Cloud Function support #1085

Merged
merged 9 commits into from
Jul 10, 2021
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## Unreleased

### Fetures
### Features

- Integration for Google Cloud Functions ([#1085](https://github.com/getsentry/sentry-dotnet/pull/1085))
- Add ClearAttachments to Scope ([#1104](https://github.com/getsentry/sentry-dotnet/pull/1104))

## 3.6.1
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Sentry SDK for .NET
| **Sentry.Extensions.Logging** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.Extensions.Logging.svg)](https://www.nuget.org/packages/Sentry.Extensions.Logging) | [![NuGet](https://img.shields.io/nuget/v/Sentry.Extensions.Logging.svg)](https://www.nuget.org/packages/Sentry.Extensions.Logging) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.Extensions.Logging.svg)](https://www.nuget.org/packages/Sentry.Extensions.Logging) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/) |
| **Sentry.AspNetCore** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.AspNetCore.svg)](https://www.nuget.org/packages/Sentry.AspNetCore) | [![NuGet](https://img.shields.io/nuget/v/Sentry.AspNetCore.svg)](https://www.nuget.org/packages/Sentry.AspNetCore) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.AspNetCore.svg)](https://www.nuget.org/packages/Sentry.AspNetCore) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/) |
| **Sentry.AspNetCore.Grpc** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.AspNetCore.Grpc.svg)](https://www.nuget.org/packages/Sentry.AspNetCore.Grpc) | [![NuGet](https://img.shields.io/nuget/v/Sentry.AspNetCore.Grpc.svg)](https://www.nuget.org/packages/Sentry.AspNetCore.Grpc) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.AspNetCore.Grpc.svg)](https://www.nuget.org/packages/Sentry.AspNetCore.Grpc) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/) |
| **Sentry.Google.Cloud.Functions** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.Google.Cloud.Functions.svg)](https://www.nuget.org/packages/Sentry.Google.Cloud.Functions) | [![NuGet](https://img.shields.io/nuget/v/Sentry.Google.Cloud.Functions.svg)](https://www.nuget.org/packages/Sentry.Google.Cloud.Functions) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.Google.Cloud.Functions.svg)](https://www.nuget.org/packages/Sentry.Google.Cloud.Functions) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/gcp-functions/) |
| **Sentry.AspNet** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.AspNet.svg)](https://www.nuget.org/packages/Sentry.AspNet) | [![NuGet](https://img.shields.io/nuget/v/Sentry.AspNet.svg)](https://www.nuget.org/packages/Sentry.AspNet) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.AspNet.svg)](https://www.nuget.org/packages/Sentry.AspNet) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/aspnet) |
| **Sentry.EntityFramework** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.EntityFramework.svg)](https://www.nuget.org/packages/Sentry.EntityFramework) | [![NuGet](https://img.shields.io/nuget/v/Sentry.EntityFramework.svg)](https://www.nuget.org/packages/Sentry.EntityFramework) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.EntityFramework.svg)](https://www.nuget.org/packages/Sentry.EntityFramework) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/entityframework) |
| **Sentry.Serilog** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.Serilog.svg)](https://www.nuget.org/packages/Serilog) | [![NuGet](https://img.shields.io/nuget/v/Sentry.Serilog.svg)](https://www.nuget.org/packages/Sentry.Serilog) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.Serilog.svg)](https://www.nuget.org/packages/Sentry.Serilog) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/serilog) |
Expand Down
21 changes: 21 additions & 0 deletions Sentry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.EntityFramework.Test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.EntityFramework", "samples\Sentry.Samples.EntityFramework\Sentry.Samples.EntityFramework.csproj", "{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Google.Cloud.Functions", "samples\Sentry.Samples.Google.Cloud.Functions\Sentry.Samples.Google.Cloud.Functions.csproj", "{88269A52-A0BA-41B2-8DF3-505B66B17243}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Google.Cloud.Functions", "src\Sentry.Google.Cloud.Functions\Sentry.Google.Cloud.Functions.csproj", "{D1DB7B31-EC6B-430B-B6B0-2849BAE41AC1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Google.Cloud.Functions.Tests", "test\Sentry.Google.Cloud.Functions.Tests\Sentry.Google.Cloud.Functions.Tests.csproj", "{066522A4-8380-4D29-8DD0-973B1EDF0B39}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -271,6 +277,18 @@ Global
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}.Release|Any CPU.Build.0 = Release|Any CPU
{88269A52-A0BA-41B2-8DF3-505B66B17243}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{88269A52-A0BA-41B2-8DF3-505B66B17243}.Debug|Any CPU.Build.0 = Debug|Any CPU
{88269A52-A0BA-41B2-8DF3-505B66B17243}.Release|Any CPU.ActiveCfg = Release|Any CPU
{88269A52-A0BA-41B2-8DF3-505B66B17243}.Release|Any CPU.Build.0 = Release|Any CPU
{D1DB7B31-EC6B-430B-B6B0-2849BAE41AC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1DB7B31-EC6B-430B-B6B0-2849BAE41AC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1DB7B31-EC6B-430B-B6B0-2849BAE41AC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1DB7B31-EC6B-430B-B6B0-2849BAE41AC1}.Release|Any CPU.Build.0 = Release|Any CPU
{066522A4-8380-4D29-8DD0-973B1EDF0B39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{066522A4-8380-4D29-8DD0-973B1EDF0B39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{066522A4-8380-4D29-8DD0-973B1EDF0B39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{066522A4-8380-4D29-8DD0-973B1EDF0B39}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -314,6 +332,9 @@ Global
{8B38F62E-0DD5-486F-96F5-2025AFB9B491} = {AF6AF4C7-8AA2-4D59-8064-2D79560904EB}
{840B220E-68EC-4ECB-AEA1-67B0151F17FC} = {83263231-1A2A-4733-B759-EEFF14E8C5D5}
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA} = {77454495-55EE-4B40-A089-71B9E8F82E89}
{88269A52-A0BA-41B2-8DF3-505B66B17243} = {77454495-55EE-4B40-A089-71B9E8F82E89}
{D1DB7B31-EC6B-430B-B6B0-2849BAE41AC1} = {AF6AF4C7-8AA2-4D59-8064-2D79560904EB}
{066522A4-8380-4D29-8DD0-973B1EDF0B39} = {83263231-1A2A-4733-B759-EEFF14E8C5D5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0C652B1A-DF72-4EE5-A98B-194FE2C054F6}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions samples/Sentry.Samples.Google.Cloud.Functions/Function.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using Google.Cloud.Functions.Hosting;
using Microsoft.Extensions.Logging;

[assembly: FunctionsStartup(typeof(SentryStartup))]

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.");
throw new Exception("Bad function");
}
}
15 changes: 15 additions & 0 deletions samples/Sentry.Samples.Google.Cloud.Functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Sample integration with Google Cloud Function

Edit the `appsettings.json` file and add your own `DSN`.
You can get one at [sentry.io](sentry.io).

Run this sample:

```sh
dotnet run
```

Browse the URL: `http://127.0.0.1:8080/`.
An event will be sent to the DSN configured on `appsettings.json`.

![Sample event in Sentry](.assets/gcp_sample.png)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<!-- Must add the dependency directly here for AutoGenerateEntryPoint to work -->
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" />
<ProjectReference Include="../../src/Sentry.Google.Cloud.Functions/Sentry.Google.Cloud.Functions.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Sentry": {
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537",
"IncludeRequestPayload": true,
"SendDefaultPii": true
}
}
1 change: 0 additions & 1 deletion src/Sentry.AspNetCore.Grpc/SentryBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static ISentryBuilder AddGrpc(this ISentryBuilder builder)
return builder;
}


private class SentryGrpcEventProcessor : ISentryEventProcessor
{
private static readonly SdkVersion NameAndVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// Extension methods for <see cref="IServiceCollection"/>
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class ServiceCollectionExtensions
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds Sentry's services to the <see cref="IServiceCollection"/>
Expand Down
1 change: 1 addition & 0 deletions src/Sentry.AspNetCore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

[assembly: InternalsVisibleTo("Sentry.AspNetCore.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")]
[assembly: InternalsVisibleTo("Sentry.AspNetCore.Grpc, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")]
[assembly: InternalsVisibleTo("Sentry.Google.Cloud.Functions.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
8 changes: 7 additions & 1 deletion src/Sentry.AspNetCore/SentryAspNetCoreLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

namespace Sentry.AspNetCore
{
/// <summary>
/// Logger provider for Sentry.
/// </summary>
[ProviderAlias("Sentry")]
internal class SentryAspNetCoreLoggerProvider : SentryLoggerProvider
public class SentryAspNetCoreLoggerProvider : SentryLoggerProvider
{
/// <summary>
/// Creates a new instance of <see cref="SentryAspNetCoreLoggerProvider"/>
/// </summary>
public SentryAspNetCoreLoggerProvider(IOptions<SentryAspNetCoreOptions> options, IHub hub)
: base(options, hub)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@

namespace Sentry.AspNetCore
{
internal class SentryAspNetCoreOptionsSetup : ConfigureFromConfigurationOptions<SentryAspNetCoreOptions>
/// <summary>
/// Sets up ASP.NET Core option for Sentry.
/// </summary>
public class SentryAspNetCoreOptionsSetup : ConfigureFromConfigurationOptions<SentryAspNetCoreOptions>
{
private readonly IHostingEnvironment _hostingEnvironment;

/// <summary>
/// Creates a new instance of <see cref="SentryAspNetCoreOptionsSetup"/>.
/// </summary>
public SentryAspNetCoreOptionsSetup(
ILoggerProviderConfiguration<SentryAspNetCoreLoggerProvider> providerConfiguration,
IHostingEnvironment hostingEnvironment)
: base(providerConfiguration.Configuration)
=> _hostingEnvironment = hostingEnvironment;

/// <summary>
/// Configures the <see cref="SentryAspNetCoreOptions"/>.
/// </summary>
public override void Configure(SentryAspNetCoreOptions options)
{
base.Configure(options);
Expand Down
9 changes: 7 additions & 2 deletions src/Sentry.AspNetCore/SentryStartupFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

namespace Sentry.AspNetCore
{
/// <inheritdoc />
internal class SentryStartupFilter : IStartupFilter
/// <summary>
/// Starts Sentry integration.
/// </summary>
public class SentryStartupFilter : IStartupFilter
{
/// <summary>
/// Adds Sentry to the pipeline.
/// </summary>
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => e =>
{
e.UseSentry();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<PackageTags>$(PackageTags);GCP;Google Cloud Functions</PackageTags>
<Description>Official Google Cloud Functions integration for Sentry - Open-source error tracking that helps developers monitor and fix crashes in real time.</Description>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../Sentry.AspNetCore/Sentry.AspNetCore.csproj" />
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" />
</ItemGroup>

</Project>
82 changes: 82 additions & 0 deletions src/Sentry.Google.Cloud.Functions/SentryStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.FlushOnCompletedRequest = 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;
}
}
}
}
Loading