Skip to content

Commit

Permalink
Merge pull request #80 from Microsoft/sergkanz/failedRequests
Browse files Browse the repository at this point in the history
mark request as failed for runaway exceptions
  • Loading branch information
Sergey Kanzhelev authored and Sergey Kanzhelev committed May 14, 2015
2 parents f59e449 + 6921020 commit 06fe58d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
namespace Microsoft.ApplicationInsights.AspNet
{
using System;
using System.Linq;
using System.Reflection;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.AspNet.Extensions;

Expand All @@ -32,31 +29,34 @@ public RequestTrackingMiddleware(RequestDelegate next, TelemetryClient client)

public async Task Invoke(HttpContext httpContext, RequestTelemetry telemetry)
{
telemetry.Timestamp = DateTimeOffset.UtcNow;

var sw = new Stopwatch();
sw.Start();

var now = DateTimeOffset.UtcNow;
bool requestFailed = false;

try
{
await this.next.Invoke(httpContext);
}
catch (Exception)
{
requestFailed = true;
throw;
}
finally
{
if (this.telemetryClient != null)
{
sw.Stop();
sw.Stop();

telemetry.Timestamp = now;
telemetry.Duration = sw.Elapsed;
telemetry.ResponseCode = httpContext.Response.StatusCode.ToString();
telemetry.Success = httpContext.Response.StatusCode < 400;
telemetry.HttpMethod = httpContext.Request.Method;
telemetry.Url = httpContext.Request.GetUri();
telemetry.Context.GetInternalContext().SdkVersion = this.sdkVersion;
telemetry.Duration = sw.Elapsed;
telemetry.ResponseCode = httpContext.Response.StatusCode.ToString();
telemetry.Success = (!requestFailed) && (httpContext.Response.StatusCode < 400);
telemetry.HttpMethod = httpContext.Request.Method;
telemetry.Url = httpContext.Request.GetUri();
telemetry.Context.GetInternalContext().SdkVersion = this.sdkVersion;

this.telemetryClient.TrackRequest(telemetry);
}
this.telemetryClient.TrackRequest(telemetry);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
namespace Microsoft.ApplicationInsights.AspNet
{
using System;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.AspNet.Tests.Helpers;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Core;
using Xunit;
using Microsoft.AspNet.Http;
using System;

public class RequestTrackingMiddlewareTest
{
Expand Down Expand Up @@ -67,5 +67,22 @@ public async Task TestRequestUriIsPopulatedByMiddleware()
new Uri(string.Format("{0}://{1}{2}{3}", HttpRequestScheme, httpRequestHost.Value, httpRequestPath.Value, httpRequestQueryString.Value)),
telemetry.Url);
}

[Fact]
public async Task RequestWillBeMarkedAsFailedForRunawayException()
{
var context = new DefaultHttpContext();
context.Request.Scheme = HttpRequestScheme;
context.Request.Host = this.httpRequestHost;

var requestMiddleware = new RequestTrackingMiddleware(
httpContext => { throw new InvalidOperationException(); },
CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry));


await Assert.ThrowsAsync<InvalidOperationException>(async () => { await requestMiddleware.Invoke(context, new RequestTelemetry()); } );

Assert.False(((RequestTelemetry)this.sentTelemetry).Success);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void TestBasicRequestPropertiesAfterRequestingControllerThatThrows()
//TODO: default template of Web API applicaiton doesn't have error handling middleware
//that will set appropriate status code
expectedRequestTelemetry.ResponseCode = "200";
expectedRequestTelemetry.Success = true;
expectedRequestTelemetry.Success = false;
expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath);

this.ValidateBasicRequest(server, RequestPath, expectedRequestTelemetry);
Expand Down

0 comments on commit 06fe58d

Please sign in to comment.