Skip to content

Commit

Permalink
Updated lab 4 with request tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandersen committed Jun 12, 2016
1 parent c37a213 commit b96351d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 15 deletions.
@@ -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);
}
}
}
45 changes: 30 additions & 15 deletions Labs/lab04.md
Expand Up @@ -10,10 +10,6 @@

## Create Application Insights

1. **Discussion points:**
* Discuss why telemetry and logging are important especially in Microservices architectures
* Describe the basics of Application Insights (e.g. high-level features, architecture, pricing models, etc.)

1. Open [Azure Portal](https://portal.azure.com) and sign in.

1. Add *Application Insights* named `PracticalDevOps-Dev` to the resource group `PracticalDevOps-Dev`.<br/>
Expand All @@ -40,6 +36,8 @@
</configuration>
```

1. Add the `Microsoft.ApplicationInsights.Web` NuGet package to the Books project

1. Add code setting the instrumentation key to `Startup.cs`:
```
public void Configuration(IAppBuilder app)
Expand Down Expand Up @@ -67,9 +65,35 @@
...
}
```

```
[HttpPost]
[Route("books")]
public IHttpActionResult Post(Book newBook)
{
var telemetryClient = new TelemetryClient();
telemetryClient.TrackEvent($"Trying to add a book");
1. **Discussion points:**
* Describe basics of Application Insights SDK (e.g. exception logging, metrics)
// For demo purposes, return an HTTP 500 error (used to demonstrate logging)
return this.InternalServerError();
}
```

1. Copy Request tracking middleware implementation from [Assets/Exercise-4-Telemetry](Assets/Exercise-4-Telemetry) that logs information about all requests to Application Insights.

1. Configure OWIN to use the request tracking middleware by updating `Configuration` method in `Startup.cs`:
```
public void Configuration(IAppBuilder app)
{
TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["InstrumentationKey"];
app.UseApplicationInsights();
// Allow CORS
app.UseCors(CorsOptions.AllowAll);
...
}
```


## Run Application and View Telemetry
Expand All @@ -83,18 +107,9 @@

1. See if your application telemetry appears.

1. **Discussion points:**
* Let people play a bit with building Application Insights dashboards in the Azure portal
* Describe concept of custom processing of Application Insights data
* Brief overview about other Application Insights modules (e.g. for IaaS)

1. Open *Application Insights Search* in Visual Studio while debugging your application. Refresh `http://localhost:2690/api/books` multiple times. See if your application telemetry appears.<br/>
![Application Insights Search](img/visual-studio-application-insights.png)

1. **Discussion points:**
* Point out how calls to dependent services are tracked automatically
* Show how unnecessary calls to Blob Storage in our app become visible by analyzing Application Insights telemetry data


## Further Ideas

Expand Down

0 comments on commit b96351d

Please sign in to comment.