forked from rstropek/PracticalDevOpsTraining
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c37a213
commit b96351d
Showing
2 changed files
with
106 additions
and
15 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Labs/Assets/Exercise-4-Telemetry/Middleware/ApplicationInsightsMiddleware.cs
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,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using Microsoft.ApplicationInsights; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.Owin; | ||
using Owin; | ||
|
||
namespace Books.Middleware | ||
{ | ||
// Example from http://stackoverflow.com/questions/29471811/how-do-i-enable-application-insights-server-telemetry-on-webapi-project-that-use | ||
|
||
/// <summary> | ||
/// Extensions to help adding middleware to the OWIN pipeline | ||
/// </summary> | ||
public static class OwinExtensions | ||
{ | ||
/// <summary> | ||
/// Add Application Insight Request Tracking to the OWIN pipeline | ||
/// </summary> | ||
/// <param name="app"><see cref="IAppBuilder"/></param> | ||
public static void UseApplicationInsights(this IAppBuilder app) => app.Use(typeof(ApplicationInsights)); | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Allows for tracking requests via Application Insight | ||
/// </summary> | ||
public class ApplicationInsights : OwinMiddleware | ||
{ | ||
|
||
/// <summary> | ||
/// Allows for tracking requests via Application Insight | ||
/// </summary> | ||
/// <param name="next"><see cref="OwinMiddleware"/></param> | ||
public ApplicationInsights(OwinMiddleware next) : base(next) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Tracks the request and sends telemetry to application insights | ||
/// </summary> | ||
/// <param name="context"><see cref="IOwinContext"/></param> | ||
/// <returns></returns> | ||
public override async Task Invoke(IOwinContext context) | ||
{ | ||
// Start Time Tracking | ||
var sw = new Stopwatch(); | ||
var startTime = DateTimeOffset.Now; | ||
sw.Start(); | ||
|
||
await Next.Invoke(context); | ||
|
||
// Send tracking to AI on request completion | ||
sw.Stop(); | ||
|
||
var request = new RequestTelemetry( | ||
name: context.Request.Path.Value, | ||
startTime: startTime, | ||
duration: sw.Elapsed, | ||
responseCode: context.Response.StatusCode.ToString(), | ||
success: context.Response.StatusCode >= 200 && context.Response.StatusCode < 300 | ||
) | ||
{ | ||
Url = context.Request.Uri, | ||
HttpMethod = context.Request.Method | ||
}; | ||
|
||
var client = new TelemetryClient(); | ||
client.TrackRequest(request); | ||
} | ||
} | ||
} |
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