Logo

dev-resources.site

for different kinds of informations.

Adding Application Insights to Azure Functions

Published at
8/7/2020
Categories
codeproject
technology
applicationinsights
telemetry
Author
documentednerd
Author
14 person written this
documentednerd
open
Adding Application Insights to Azure Functions

So I wanted to share a quick post on something that is rather small but I was surprised at how little documentation there was with regard to how to implement it.

Monitoring is honestly one of the most important things you can do with regard to cloud applications. There’s an old saying, “what gets measured…matters.” And I find this expression to be very true in Application Development.

So if you are building out an azure function, what steps are required to enable Azure Application Insights for your Functions. For this post I’ll be focusing on adding it to a DotNetCore function app.

Step 1 – Add the nuget package

No surprise, it all starts with a nuget package, you are going to want to add “Microsoft.ApplicationInsights.AspNetCore”, shown below:

But additionally, you are going to need the nuget package “Microsoft.Azure.Functions.Extensions” shown here:

Step 2 – Update the Startup.cs

Now if you haven’t configured your function app to have a startup.cs file, then I’m of the opinion you’ve made some mistakes. Because honestly I can’t understate the importance of dependency injection to ensure you are falling recommended practices and setting yourself up for success. Once you’ve done that, you will be able to add the following line to the override of “Configure”, shown below:

[assembly: FunctionsStartup(typeof(SampleProject.Services.Startup))]namespace SampleProject.Services{ public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddScoped<ITelemetryProvider, TelemetryProvider>(); builder.Services.AddApplicationInsightsTelemetry(); } }}

The above code will use the “AddApplicationInsightsTelemetry()” method to capture the out-of-the box telemetry.

That leaves the outstanding question of capturing custom or application specific telemetry, for that I recommend implementing a wrapper class around the Telemetry client. Personally this is a general practice I always implement as it removes the hard dependencies in an application and helps with flexibility later.

For this, the “TelemetryProvider” is the following:

public class TelemetryProvider : ITelemetryProvider { private TelemetryClient \_client; public TelemetryProvider() { \_client = new TelemetryClient(TelemetryConfiguration.CreateDefault()); } public void TrackEvent(string name) { \_client.TrackEvent(name); } public void TrackEvent(string name, Dictionary<string, string> properties) { \_client.TrackEvent(name, properties); } public void TrackEvent(string name, Dictionary<string, string> properties, Dictionary<string, double> metrics) { \_client.TrackEvent(name, properties, metrics); } public void TrackMetric(string name, double value) { \_client.TrackMetric(name, value); } public void TrackException(Exception ex) { \_client.TrackException(ex); } public void TrackDependency(string name, string typeName, string data, DateTime startTime, TimeSpan duration, bool success) { \_client.TrackDependency(typeName, name, data, startTime, duration, success); } }

With an interface of the following:

public interface ITelemetryProvider { void TrackDependency(string name, string typeName, string data, DateTime startTime, TimeSpan duration, bool success); void TrackEvent(string name); void TrackEvent(string name, Dictionary<string, string> properties); void TrackEvent(string name, Dictionary<string, string> properties, Dictionary<string, double> metrics); void TrackException(Exception ex); void TrackMetric(string name, double value); }}

After you’ve implemented this provider, it makes it very easy to capture telemetry relates to specific functionality in your application and leverage application insights as a total telemetry solution.

applicationinsights Article's
30 articles in total
Favicon
Optimize and Monitor Power Apps Like a Pro with Application Insights
Favicon
Application Insights - 5 Tips to Improve Your Observability
Favicon
Announcing the Tracetest Integration with Azure App Insights
Favicon
Application Insights: Display Heatmap of Page Views per calendar week
Favicon
Using Application Insights with Bicep to monitor Azure Static Web Apps and Azure Functions
Favicon
Using Application Insights For Better Application Logging
Favicon
kusto & concat string array
Favicon
Week 12 2021 - Tips I learned this week
Favicon
Application Insights for Worker Service using Serilog
Favicon
Expose data from Application Insights
Favicon
Using Fiddler for Application Insights codeless java agent with docker on WSL2
Favicon
A quick intro: Application Insights for your Angular Java stack
Favicon
Monitorando uma aplicação VueJS com o Application Insights
Favicon
Cell CMS — Criando logs robustos e monitorando uma API
Favicon
Global Error Handler in Angular + Application Insights
Favicon
Monitor application via Application Insights and send alert: Part 4 Offline Scenario?
Favicon
Monitor application via Application Insights and send alert: Part 2 Understand Telemetry Types and use advanced monitoring
Favicon
Monitor application via Application Insights and send alert: Part 1 Set Rule and Action in Unified Alert
Favicon
Web App Monitoring with Application Insights
Favicon
Application Insights SDK for Node.js part 5 : Out of box telemetries - Exception, Performance and Live Metrics Stream
Favicon
Application Insights SDK for Node.js part 4 : Out of box telemetries - Basics and Console
Favicon
Application Insights SDK for Node.js part 3 : Application Map
Favicon
Application Insights SDK for Node.js part 1 : Basic usage
Favicon
Application Insights SDK for Node.js part 2 : Track events
Favicon
Bulk add Application Insights Availability Test IPs to Azure App Service Access Restrictions using Az PowerShell
Favicon
Deploying Azure Functions with Application Insights via ARM
Favicon
Gated Release
Favicon
Adding Application Insights to Azure Functions
Favicon
What the heck is Business Intelligence?
Favicon
Monitor application via Application Insights and send alert: Part 3 Optimize telemetry and alerts

Featured ones: