Skip to content

Commit

Permalink
Merge branch 'fix/ignore-diagnosticsource-with-no-transactions' of ht…
Browse files Browse the repository at this point in the history
…tps://github.com/getsentry/sentry-dotnet into fix/ignore-diagnosticsource-with-no-transactions
  • Loading branch information
lucas-zimerman committed Oct 12, 2021
2 parents ad61946 + b66a034 commit 81585a6
Show file tree
Hide file tree
Showing 59 changed files with 10,771 additions and 82 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ jobs:
- name: Publish coverage report
uses: codecov/codecov-action@v1

- name: Upload Verify Results
if: failure()
uses: actions/upload-artifact@v2
with:
name: verify-test-results
path: |
**/*.received.*
- name: Archive Artifacts
# only the ps1 script packages the SDK
if: matrix.os == 'windows-latest'
Expand Down
84 changes: 15 additions & 69 deletions samples/Sentry.Samples.GenericHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,22 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Sentry;

internal static class Program
{
public static Task Main()
=> new HostBuilder()
.ConfigureHostConfiguration(c =>
{
c.SetBasePath(Directory.GetCurrentDirectory());
c.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureServices((_, s) => { s.AddHostedService<SampleHostedService>(); })
.ConfigureLogging((c, l) =>
{
l.AddConfiguration(c.Configuration);
l.AddConsole();
l.AddSentry();
})
.UseConsoleLifetime()
.Build()
.RunAsync();

internal class SampleHostedService : IHostedService
await Host.CreateDefaultBuilder()
.ConfigureHostConfiguration(c =>
{
private readonly IHub _hub;
private readonly ILogger _logger;

public SampleHostedService(IHub hub, ILogger<SampleHostedService> logger)
{
_hub = hub;
_logger = logger;
}

public Task StartAsync(CancellationToken cancellationToken)
{
// Logging integration by default keeps informational logs as Breadcrumb
_logger.LogInformation("Starting sample hosted service. This goes as a breadcrumb");
// You can also add breadcrumb directly through Sentry.Hub:
_hub.AddBreadcrumb("Breadcrumb added directly to Sentry Hub")
;
// Hub allows total control of the scope
_hub.ConfigureScope(s => s.SetTag("Worker", nameof(SampleHostedService)));

// By default Error and Critical log messages are sent to sentry as events
_logger.LogError("An event sent to sentry.");

return Task.Run(() =>
{
try
{
var zero = 0;
_ = 10 / zero; // Throws DivideByZeroException
}
catch (Exception e)
{
// Direct control of capturing errors with Sentry
_hub.CaptureException(e);
}
}, cancellationToken);
}

public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping sample hosted service.");
return Task.CompletedTask;
}
}
}
c.SetBasePath(Directory.GetCurrentDirectory());
c.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureServices((_, s) => s.AddHostedService<SampleHostedService>())
.ConfigureLogging((c, l) =>
{
l.AddConfiguration(c.Configuration);
l.AddConsole();
l.AddSentry();
})
.UseConsoleLifetime()
.Build()
.RunAsync();
52 changes: 52 additions & 0 deletions samples/Sentry.Samples.GenericHost/SampleHostedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Sentry;

internal class SampleHostedService : IHostedService
{
private readonly IHub _hub;
private readonly ILogger _logger;

public SampleHostedService(IHub hub, ILogger<SampleHostedService> logger)
{
_hub = hub;
_logger = logger;
}

public Task StartAsync(CancellationToken cancellationToken)
{
// Logging integration by default keeps informational logs as Breadcrumb
_logger.LogInformation("Starting sample hosted service. This goes as a breadcrumb");
// You can also add breadcrumb directly through Sentry.Hub:
_hub.AddBreadcrumb("Breadcrumb added directly to Sentry Hub")
;
// Hub allows total control of the scope
_hub.ConfigureScope(s => s.SetTag("Worker", nameof(SampleHostedService)));

// By default Error and Critical log messages are sent to sentry as events
_logger.LogError("An event sent to sentry.");

return Task.Run(() =>
{
try
{
var zero = 0;
_ = 10 / zero; // Throws DivideByZeroException
}
catch (Exception e)
{
// Direct control of capturing errors with Sentry
_hub.CaptureException(e);
}
}, cancellationToken);
}

public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping sample hosted service.");
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,14 +13,12 @@

<ItemGroup>
<ProjectReference Include="../../src/Sentry.Extensions.Logging/Sentry.Extensions.Logging.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
<PackageReference Update="Roslynator.Analyzers" Version="3.2.2" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Verify.Xunit" Version="13.1.0" />
<PackageReference Include="PublicApiGenerator" Version="10.2.0" />
<PackageReference Include="GitHubActionsTestLogger" Version="1.2.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[assembly: System.Runtime.Versioning.TargetFramework(".NETFramework,Version=v4.6.1", FrameworkDisplayName=".NET Framework 4.6.1")]
namespace Sentry.AspNet
{
public static class HttpContextExtensions
{
public static void FinishSentryTransaction(this System.Web.HttpContext httpContext) { }
public static Sentry.ITransaction StartSentryTransaction(this System.Web.HttpContext httpContext) { }
}
public static class SentryAspNetOptionsExtensions
{
public static void AddAspNet(this Sentry.SentryOptions options, Sentry.Extensibility.RequestSize maxRequestBodySize = 0) { }
}
}
17 changes: 17 additions & 0 deletions test/Sentry.AspNet.Tests/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using Sentry.Tests;
using VerifyXunit;
using Xunit;

namespace Sentry.AspNet.Tests
{
[UsesVerify]
public class ApiApprovalTests
{
[Fact]
public Task Run()
{
return typeof(SentryAspNetOptionsExtensions).Assembly.CheckApproval();
}
}
}
1 change: 1 addition & 0 deletions test/Sentry.AspNet.Tests/Sentry.AspNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Sentry.AspNet\Sentry.AspNet.csproj" />
<ProjectReference Include="..\Sentry.Testing\Sentry.Testing.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v3.0", FrameworkDisplayName="")]
namespace Sentry.AspNetCore.Grpc
{
public class DefaultProtobufRequestPayloadExtractor : Sentry.AspNetCore.Grpc.IProtobufRequestPayloadExtractor
{
public DefaultProtobufRequestPayloadExtractor() { }
public Google.Protobuf.IMessage ExtractPayload<TRequest>(Sentry.AspNetCore.Grpc.IProtobufRequest<TRequest> request)
where TRequest : class, Google.Protobuf.IMessage { }
}
public interface IProtobufRequestPayloadExtractor
{
Google.Protobuf.IMessage? ExtractPayload<TRequest>(Sentry.AspNetCore.Grpc.IProtobufRequest<TRequest> request)
where TRequest : class, Google.Protobuf.IMessage;
}
public interface IProtobufRequest<TRequest>
{
long? ContentLength { get; }
TRequest Request { get; }
}
public class ProtobufRequestExtractionDispatcher : Sentry.AspNetCore.Grpc.IProtobufRequestPayloadExtractor
{
public ProtobufRequestExtractionDispatcher(System.Collections.Generic.IEnumerable<Sentry.AspNetCore.Grpc.IProtobufRequestPayloadExtractor> extractors, Sentry.SentryOptions options, System.Func<Sentry.Extensibility.RequestSize> sizeSwitch) { }
public Google.Protobuf.IMessage? ExtractPayload<TRequest>(Sentry.AspNetCore.Grpc.IProtobufRequest<TRequest> request)
where TRequest : class, Google.Protobuf.IMessage { }
}
public static class ScopeExtensions
{
public static void Populate<TRequest>(this Sentry.Scope scope, Grpc.Core.ServerCallContext context, TRequest? request, Sentry.AspNetCore.SentryAspNetCoreOptions options)
where TRequest : class { }
}
public static class SentryBuilderExtensions
{
public static Sentry.AspNetCore.ISentryBuilder AddGrpc(this Sentry.AspNetCore.ISentryBuilder builder) { }
}
public class SentryGrpcInterceptor : Grpc.Core.Interceptors.Interceptor
{
public SentryGrpcInterceptor(System.Func<Sentry.IHub> hubAccessor, Microsoft.Extensions.Options.IOptions<Sentry.AspNetCore.SentryAspNetCoreOptions> options) { }
public override System.Threading.Tasks.Task<TResponse> ClientStreamingServerHandler<TRequest, TResponse>(Grpc.Core.IAsyncStreamReader<TRequest> requestStream, Grpc.Core.ServerCallContext context, Grpc.Core.ClientStreamingServerMethod<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class { }
public override System.Threading.Tasks.Task DuplexStreamingServerHandler<TRequest, TResponse>(Grpc.Core.IAsyncStreamReader<TRequest> requestStream, Grpc.Core.IServerStreamWriter<TResponse> responseStream, Grpc.Core.ServerCallContext context, Grpc.Core.DuplexStreamingServerMethod<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class { }
public override System.Threading.Tasks.Task ServerStreamingServerHandler<TRequest, TResponse>(TRequest request, Grpc.Core.IServerStreamWriter<TResponse> responseStream, Grpc.Core.ServerCallContext context, Grpc.Core.ServerStreamingServerMethod<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class { }
public override System.Threading.Tasks.Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, Grpc.Core.ServerCallContext context, Grpc.Core.UnaryServerMethod<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v5.0", FrameworkDisplayName="")]
namespace Sentry.AspNetCore.Grpc
{
public class DefaultProtobufRequestPayloadExtractor : Sentry.AspNetCore.Grpc.IProtobufRequestPayloadExtractor
{
public DefaultProtobufRequestPayloadExtractor() { }
public Google.Protobuf.IMessage ExtractPayload<TRequest>(Sentry.AspNetCore.Grpc.IProtobufRequest<TRequest> request)
where TRequest : class, Google.Protobuf.IMessage { }
}
public interface IProtobufRequestPayloadExtractor
{
Google.Protobuf.IMessage? ExtractPayload<TRequest>(Sentry.AspNetCore.Grpc.IProtobufRequest<TRequest> request)
where TRequest : class, Google.Protobuf.IMessage;
}
public interface IProtobufRequest<TRequest>
{
long? ContentLength { get; }
TRequest Request { get; }
}
public class ProtobufRequestExtractionDispatcher : Sentry.AspNetCore.Grpc.IProtobufRequestPayloadExtractor
{
public ProtobufRequestExtractionDispatcher(System.Collections.Generic.IEnumerable<Sentry.AspNetCore.Grpc.IProtobufRequestPayloadExtractor> extractors, Sentry.SentryOptions options, System.Func<Sentry.Extensibility.RequestSize> sizeSwitch) { }
public Google.Protobuf.IMessage? ExtractPayload<TRequest>(Sentry.AspNetCore.Grpc.IProtobufRequest<TRequest> request)
where TRequest : class, Google.Protobuf.IMessage { }
}
public static class ScopeExtensions
{
public static void Populate<TRequest>(this Sentry.Scope scope, Grpc.Core.ServerCallContext context, TRequest? request, Sentry.AspNetCore.SentryAspNetCoreOptions options)
where TRequest : class { }
}
public static class SentryBuilderExtensions
{
public static Sentry.AspNetCore.ISentryBuilder AddGrpc(this Sentry.AspNetCore.ISentryBuilder builder) { }
}
public class SentryGrpcInterceptor : Grpc.Core.Interceptors.Interceptor
{
public SentryGrpcInterceptor(System.Func<Sentry.IHub> hubAccessor, Microsoft.Extensions.Options.IOptions<Sentry.AspNetCore.SentryAspNetCoreOptions> options) { }
public override System.Threading.Tasks.Task<TResponse> ClientStreamingServerHandler<TRequest, TResponse>(Grpc.Core.IAsyncStreamReader<TRequest> requestStream, Grpc.Core.ServerCallContext context, Grpc.Core.ClientStreamingServerMethod<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class { }
public override System.Threading.Tasks.Task DuplexStreamingServerHandler<TRequest, TResponse>(Grpc.Core.IAsyncStreamReader<TRequest> requestStream, Grpc.Core.IServerStreamWriter<TResponse> responseStream, Grpc.Core.ServerCallContext context, Grpc.Core.DuplexStreamingServerMethod<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class { }
public override System.Threading.Tasks.Task ServerStreamingServerHandler<TRequest, TResponse>(TRequest request, Grpc.Core.IServerStreamWriter<TResponse> responseStream, Grpc.Core.ServerCallContext context, Grpc.Core.ServerStreamingServerMethod<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class { }
public override System.Threading.Tasks.Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, Grpc.Core.ServerCallContext context, Grpc.Core.UnaryServerMethod<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class { }
}
}
17 changes: 17 additions & 0 deletions test/Sentry.AspNetCore.Grpc.Tests/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using Sentry.Tests;
using VerifyXunit;
using Xunit;

namespace Sentry.AspNetCore.Grpc.Tests
{
[UsesVerify]
public class ApiApprovalTests
{
[Fact]
public Task Run()
{
return typeof(SentryGrpcInterceptor).Assembly.CheckApproval();
}
}
}
Loading

0 comments on commit 81585a6

Please sign in to comment.