Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
optimizations (#1)
Browse files Browse the repository at this point in the history
* removal of continue with

* general information breadcrumb

* package settings
  • Loading branch information
michalderdak authored Jun 21, 2018
1 parent 4c98eaf commit 2c10eea
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 89 deletions.
12 changes: 12 additions & 0 deletions NetGrpcSentry/Breadcrumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public Breadcrumber(RavenClient sentryClient)
_sentryClient = sentryClient;
}

public void GeneralInformationBreadcrumb()
{
_sentryClient.AddTrail(new Breadcrumb("General Information")
{
Message = "General information for the exception",
Data = new Dictionary<string, string>()
{
{nameof(DateTime), DateTime.Now.ToLongDateString()}
}
});
}

public void MessageBreadcrumb<T>(T message)
{
_sentryClient.AddTrail(new Breadcrumb("Message")
Expand Down
14 changes: 7 additions & 7 deletions NetGrpcSentry/NetGrpcSentry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Michal Derdak</Authors>
<Company>Michal Derdak</Company>
<Description>A gRPC interceptor logging exceptions to the Sentry</Description>
<Copyright></Copyright>
<PackageLicenseUrl>https://github.com/michalderdak/csharp-grpc-sentry/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/michalderdak/csharp-grpc-sentry</PackageProjectUrl>
<RepositoryUrl>https://github.com/michalderdak/csharp-grpc-sentry</RepositoryUrl>
<Authors>Visma e-conomic</Authors>
<Company>Visma e-conomic</Company>
<Description>A gRPC interceptor for automatic logging of exceptions to the Sentry</Description>
<Copyright>Copyright © 2018 Visma e-conomic</Copyright>
<PackageLicenseUrl>https://github.com/e-conomic/csharp-grpc-sentry/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/e-conomic/csharp-grpc-sentry</PackageProjectUrl>
<RepositoryUrl>https://github.com/e-conomic/csharp-grpc-sentry</RepositoryUrl>
<PackageTags>grpc interceptor intercept sentry logging logger log</PackageTags>
<PackageIconUrl>https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQS5lIpI_POK12bWwXMmM_WYj-r4Z4NDb2VcxPxco8WUKUd_jp5</PackageIconUrl>
</PropertyGroup>
Expand Down
137 changes: 56 additions & 81 deletions NetGrpcSentry/SentryInterceptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Interceptors;
Expand Down Expand Up @@ -46,127 +45,103 @@ public SentryInterceptor(Dsn dsn, IJsonPacketFactory jsonPacketFactory = null,

#region SERVER

public override Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request,
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request,
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation)
{
return continuation(request, context).ContinueWith(task =>
try
{
if (task.Exception == null)
{
return task.Result;
}

if (task.Exception != null &&
task.Exception.InnerExceptions.All(exception => exception is RpcException))
{
return task.Result;
}

return await continuation(request, context);
}
catch (RpcException)
{
throw;
}
catch (Exception e)
{
_breadcrumber.GeneralInformationBreadcrumb();
_breadcrumber.MessageBreadcrumb(request);
_breadcrumber.ContextBreadcrumb(context);
_breadcrumber.MethodBreadcrumb(continuation.Method);

var exceptions = task.Exception.InnerExceptions.Where(e => !(e.InnerException is RpcException));
foreach (var exception in exceptions)
{
_sentryClient.Capture(new SentryEvent(exception));
}

throw task.Exception;
_sentryClient.Capture(new SentryEvent(e));

}, TaskContinuationOptions.OnlyOnFaulted);
throw;
}
}

public override Task ServerStreamingServerHandler<TRequest, TResponse>(TRequest request,
public override async Task ServerStreamingServerHandler<TRequest, TResponse>(TRequest request,
IServerStreamWriter<TResponse> responseStream,
ServerCallContext context, ServerStreamingServerMethod<TRequest, TResponse> continuation)
{
return continuation(request, responseStream, context).ContinueWith(task =>
try
{
if (task.Exception == null)
{
return;
}

if (task.Exception != null &&
task.Exception.InnerExceptions.All(exception => exception is RpcException))
{
return;
}

await continuation(request, responseStream, context);
}
catch (RpcException)
{
throw;
}
catch (Exception e)
{
_breadcrumber.GeneralInformationBreadcrumb();
_breadcrumber.MessageBreadcrumb(request);
_breadcrumber.ContextBreadcrumb(context);
_breadcrumber.MethodBreadcrumb(continuation.Method);

var exceptions = task.Exception.InnerExceptions.Where(e => !(e.InnerException is RpcException));
foreach (var exception in exceptions)
{
_sentryClient.Capture(new SentryEvent(exception));
}
_sentryClient.Capture(new SentryEvent(e));

}, TaskContinuationOptions.OnlyOnFaulted);
throw;
}
}

public override Task<TResponse> ClientStreamingServerHandler<TRequest, TResponse>(
public override async Task<TResponse> ClientStreamingServerHandler<TRequest, TResponse>(
IAsyncStreamReader<TRequest> requestStream, ServerCallContext context,
ClientStreamingServerMethod<TRequest, TResponse> continuation)
{
return continuation(requestStream, context).ContinueWith(task =>
try
{
return await continuation(requestStream, context);
}
catch (RpcException)
{
if (task.Exception == null)
{
return task.Result;
}

if (task.Exception != null &&
task.Exception.InnerExceptions.All(exception => exception is RpcException))
{
return task.Result;
}

throw;
}
catch (Exception e)
{
_breadcrumber.GeneralInformationBreadcrumb();
_breadcrumber.ContextBreadcrumb(context);
_breadcrumber.MethodBreadcrumb(continuation.Method);

var exceptions = task.Exception.InnerExceptions.Where(e => !(e.InnerException is RpcException));
foreach (var exception in exceptions)
{
_sentryClient.Capture(new SentryEvent(exception));
}

return task.Result;
_sentryClient.Capture(new SentryEvent(e));

}, TaskContinuationOptions.OnlyOnFaulted);
throw;
}
}

public override Task DuplexStreamingServerHandler<TRequest, TResponse>(
public override async Task DuplexStreamingServerHandler<TRequest, TResponse>(
IAsyncStreamReader<TRequest> requestStream,
IServerStreamWriter<TResponse> responseStream, ServerCallContext context,
DuplexStreamingServerMethod<TRequest, TResponse> continuation)
{
return continuation(requestStream, responseStream, context).ContinueWith(task =>
try
{
if (task.Exception == null)
{
return;
}

if (task.Exception != null &&
task.Exception.InnerExceptions.All(exception => exception is RpcException))
{
return;
}

await continuation(requestStream, responseStream, context);
}
catch (RpcException)
{
throw;
}
catch (Exception e)
{
_breadcrumber.GeneralInformationBreadcrumb();
_breadcrumber.ContextBreadcrumb(context);
_breadcrumber.MethodBreadcrumb(continuation.Method);

var exceptions = task.Exception.InnerExceptions.Where(e => !(e.InnerException is RpcException));
foreach (var exception in exceptions)
{
_sentryClient.Capture(new SentryEvent(exception));
}
_sentryClient.Capture(new SentryEvent(e));

}, TaskContinuationOptions.OnlyOnFaulted);
throw;
}
}

#endregion
Expand Down
4 changes: 3 additions & 1 deletion NetGrpcSentryTest/Helpers/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public TestServer()
{
Port = PortFinder.FreeTcpPort();

var dsn = Environment.GetEnvironmentVariable("NetGrpcSentry_DSN");

_server = new Server()
{
Services =
{
TestService.BindService(new ServiceImp())
.Intercept(new SentryInterceptor(Environment.GetEnvironmentVariable("NetGrpcSentry_DSN")))
.Intercept(new SentryInterceptor(dsn))
},
Ports = {new ServerPort("localhost", Port, ServerCredentials.Insecure)}
};
Expand Down

0 comments on commit 2c10eea

Please sign in to comment.