-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Google Cloud Function support (#1085)
- Loading branch information
1 parent
7d008a9
commit 2ee1a05
Showing
19 changed files
with
395 additions
and
7 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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"); | ||
} | ||
} |
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,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) |
20 changes: 20 additions & 0 deletions
20
samples/Sentry.Samples.Google.Cloud.Functions/Sentry.Samples.Google.Cloud.Functions.csproj
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,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> |
7 changes: 7 additions & 0 deletions
7
samples/Sentry.Samples.Google.Cloud.Functions/appsettings.json
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,7 @@ | ||
{ | ||
"Sentry": { | ||
"Dsn": "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537", | ||
"IncludeRequestPayload": true, | ||
"SendDefaultPii": true | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/Sentry.Google.Cloud.Functions/Sentry.Google.Cloud.Functions.csproj
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,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> |
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,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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.