Step by Step: Add Application Insights to Azure Function HTTP Triggers

Mark Hedley
4 min readMar 14, 2017

--

I spent so much time doing performance testing on an Azure function last week that I think I went hysterically blind. Hopefully we can leverage this new and interesting neurosis for your advantage.

Below you’ll find a really simple set of steps to get started with Application Insights on an Azure Function with an HTTP trigger in C#.

Step 1 — Create a new Application Insight Instance

  1. Go to the https://portal.azure.com
  2. Click New(or the plus icon) on the left
  3. Click Monitoring + Management
  4. Select Application Insights
  5. Fill in the screen below

Name — The name of your app insights instance

Application Type — since this is an http trigger select ASP.NET web application

Subscription — select your subscription

Location — Select an appropriate region

Create App Insights blade

6. Click Create

Step 2 — Get your Instrumentation Key

  1. Open the application insight instance that you just created
  2. Click Properties under Configure
  3. Copy the instrumentation key from the screen below
Properties blade

Step 3 — Create a new functions app (optional you can use an existing one)

  1. Click on new or + again
  2. Expand Compute
  3. Click on Function App
  4. Fill in the screen below

App Name — DNS Name for your new function app

Subscription — Select your Subscription

Resource Group — Select the resource group

Hosting Plan — Choose either consumption to be billed by the memory second or App Service plan to place it in a traditional app service plan

Location — Select an Azure region to deploy in

Storage Account — Select or create a storage account to link to your function

Create Function App

Step 3 — Add your key to your app settings

  1. Go to your new function app
  2. Click on Function App Settings on the bottom left
  3. Under Develop click Configure App Settings
  4. Scroll down to App settings
  5. In key enter APP_INSIGHTS_KEY
  6. In Value paste the key from Step 2
  7. Click the disk icon at the top to save
App Setting

Step 4 — Create a new HTTP Trigger

  1. Back in your function app click + New Function
  2. Under Language pull down C#
  3. Under Scenario pull down API & Webhooks
  4. Click on HttpTrigger-CSharp
HttpTrigger-CSharp

5. Name your function something appropriate

6. Select your Authorization level

7. Click create

Step 5 — Instrument your function

Update your code in your function to look as follows:

// Setup Telemetry
TelemetryClient telemetry = new TelemetryClient();
telemetry.InstrumentationKey = System.Environment.GetEnvironmentVariable(“APP_INSIGHTS_KEY”);
String requestname = req.RequestUri.ToString();
var time = DateTime.Now;
var sw = Stopwatch.StartNew();
telemetry.Context.Operation.Id = Guid.NewGuid().ToString();

// Your Code here

//send result to App Insights
telemetry.TrackRequest(requestname, time, sw.Elapsed, “200”, true);

//Return here
return req.CreateResponse(HttpStatusCode.OK, “message”);

All Done

Now each of your requests are flowing into application insights and you can watch performance to your hearts delight in that platform.

Further Reading

The original blog that got me started:

Further documentation on custom integration with Application Insights

Details on App Insights Analytics you will want to use this if you’re really interested in going deep into performance details

--

--