diff --git a/Labs/Assets/Exercise-4-Telemetry/Middleware/ApplicationInsightsMiddleware.cs b/Labs/Assets/Exercise-4-Telemetry/Middleware/ApplicationInsightsMiddleware.cs
new file mode 100644
index 0000000..2415302
--- /dev/null
+++ b/Labs/Assets/Exercise-4-Telemetry/Middleware/ApplicationInsightsMiddleware.cs
@@ -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
+
+ ///
+ /// Extensions to help adding middleware to the OWIN pipeline
+ ///
+ public static class OwinExtensions
+ {
+ ///
+ /// Add Application Insight Request Tracking to the OWIN pipeline
+ ///
+ ///
+ public static void UseApplicationInsights(this IAppBuilder app) => app.Use(typeof(ApplicationInsights));
+
+ }
+
+ ///
+ /// Allows for tracking requests via Application Insight
+ ///
+ public class ApplicationInsights : OwinMiddleware
+ {
+
+ ///
+ /// Allows for tracking requests via Application Insight
+ ///
+ ///
+ public ApplicationInsights(OwinMiddleware next) : base(next)
+ {
+ }
+
+ ///
+ /// Tracks the request and sends telemetry to application insights
+ ///
+ ///
+ ///
+ 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);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Labs/lab04.md b/Labs/lab04.md
index 78df025..5c36bd6 100644
--- a/Labs/lab04.md
+++ b/Labs/lab04.md
@@ -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`.
@@ -40,6 +36,8 @@
```
+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)
@@ -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
@@ -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.
![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