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

Dispose of ASP.NET Core bits #804

Merged
merged 6 commits into from
Feb 24, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace Google.Cloud.Diagnostics.AspNet
/// https://msdn.microsoft.com/en-us/library/system.web.mvc.iexceptionfilter.onexception(v=vs.118).aspx
/// Docs: https://cloud.google.com/error-reporting/docs/
/// </remarks>
public class ErrorReportingExceptionFilter : IExceptionFilter
public class ErrorReportingExceptionFilter : IExceptionFilter, IDisposable
{
// The service context in which this error has occurred.
// See: https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext
Expand Down Expand Up @@ -97,6 +97,9 @@ public void OnException(ExceptionContext context)
_consumer.Receive(new[] { errorEvent });
}

/// <inheritdoc />
public void Dispose() => _consumer.Dispose();

/// <summary>
/// Gets information about the HTTP request and response when the exception occured
/// and populates a <see cref="HttpRequestContext"/> object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Google.Cloud.Diagnostics.AspNet
/// Reports unhandled exceptions to Google Cloud Error Reporting.
/// Docs: https://cloud.google.com/error-reporting/docs/
/// </remarks>
public sealed class ErrorReportingExceptionLogger : ExceptionLogger
public sealed class ErrorReportingExceptionLogger : ExceptionLogger, IDisposable
{
// The service context in which this error has occurred.
// See: https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext
Expand Down Expand Up @@ -107,6 +107,9 @@ public override bool ShouldLog(ExceptionLoggerContext context)
return context?.Exception != null;
}

/// <inheritdoc />
public void Dispose() => _consumer.Dispose();

/// <summary>
/// Gets information about the HTTP request and response when the exception occured
/// and populates a <see cref="HttpRequestContext"/> object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ namespace Google.Cloud.Diagnostics.AspNet
///
/// Docs: https://cloud.google.com/trace/docs/
/// </remarks>
public sealed class CloudTrace
public sealed class CloudTrace : IDisposable
{
private readonly IManagedTracerFactory _tracerFactory;

private readonly IConsumer<TraceProto> _consumer;

/// <summary>Gets the current <see cref="IManagedTracer"/> for the given request.</summary>
public static IManagedTracer CurrentTracer =>
TracerManager.GetCurrentTracer() ?? DoNothingTracer.Instance;
Expand All @@ -88,10 +90,10 @@ private CloudTrace(string projectId, TraceConfiguration config = null, Task<Trac
client = client ?? TraceServiceClient.CreateAsync();
config = config ?? TraceConfiguration.Create();

var consumer = ConsumerFactory<TraceProto>.GetConsumer(
_consumer = ConsumerFactory<TraceProto>.GetConsumer(
new GrpcTraceConsumer(client), MessageSizer<TraceProto>.GetSize, config.BufferOptions);

_tracerFactory = new ManagedTracerFactory(projectId, consumer,
_tracerFactory = new ManagedTracerFactory(projectId, _consumer,
RateLimitingTraceOptionsFactory.Create(config), TraceIdFactory.Create());
}

Expand All @@ -110,8 +112,12 @@ public static void Initialize(string projectId, HttpApplication application, Tra
// Add event handlers to the application.
application.BeginRequest += trace.BeginRequest;
application.EndRequest += trace.EndRequest;
application.Disposed += (object sender, EventArgs e) => { trace.Dispose(); };
}

/// <inheritdoc />
public void Dispose() => _consumer.Dispose();

private void BeginRequest(object sender, EventArgs e)
{
var headerContext = TraceHeaderContextUtils.CreateContext(HttpContext.Current.Request);
Expand Down