Step by Step: Add Application Insights to Azure Function HTTP Triggers
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
- Go to the https://portal.azure.com
- Click New(or the plus icon) on the left
- Click Monitoring + Management
- Select Application Insights
- 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
6. Click Create
Step 2 — Get your Instrumentation Key
- Open the application insight instance that you just created
- Click Properties under Configure
- Copy the instrumentation key from the screen below
Step 3 — Create a new functions app (optional you can use an existing one)
- Click on new or + again
- Expand Compute
- Click on Function App
- 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
Step 3 — Add your key to your app settings
- Go to your new function app
- Click on Function App Settings on the bottom left
- Under Develop click Configure App Settings
- Scroll down to App settings
- In key enter APP_INSIGHTS_KEY
- In Value paste the key from Step 2
- Click the disk icon at the top to save
Step 4 — Create a new HTTP Trigger
- Back in your function app click + New Function
- Under Language pull down C#
- Under Scenario pull down API & Webhooks
- Click on 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