DEV Community

Cover image for How to integrate Application Insights into Azure Functions
Sumit Kharche
Sumit Kharche

Posted on • Originally published at c-sharpcorner.com

How to integrate Application Insights into Azure Functions

Introduction

In this article, we are going to discuss about Application Insights and how to integrate Application Insight into the Azure Function app. We will also create a simple Azure function app and also logs data into application insights.

If you are new to Azure functions then first check the below articles:

In this article, we will cover the below topics,

  • What is Application Insights?
  • Enable Application Insight at the time of creating Function App
  • Integrate Application Insight into Existing Function App
  • Log data from Azure Functions into Application Insights

What is Application Insights?

When creating a Cloud application or on-prem application, monitoring always helps us to quickly analyze and debug any issue. Azure Monitor is a service provided by Azure to monitor, analyze, improve the availability and performance of your application. 

According to Microsoft docs,
Application Insights, a feature of Azure Monitor, is an extensible Application Performance Management (APM) service for developers and DevOps professionals. Use it to monitor your live applications. It will automatically detect performance anomalies, and includes powerful analytics tools to help you diagnose issues and to understand what users actually do with your app.

Application insights collect telemetry data from connected apps and provides Live Metrics, Log Analytics, etc. Application insights have an instrumentation key which we need to configure into our function app i.e. APPINSIGHTS_INSTRUMENTATIONKEY and with this key app insights grab data from our app. We can integrate application insights into Azure function in multiple ways as below:

Prerequisites

Enable Application Insight at the time of creating Function App

When we create a function app and also create application insights along with that then the instrumentation key is already set up in application settings with the name as APPINSIGHTS_INSTRUMENTATIONKEY

Log in to the Azure portal. In the Search bar, search as "function app" and then select the Function app

Image description

Image description

After that click on the "Add" button to add a new function app. Fill out the basic details:

Image description

Once we filled out basic details, then click on the Next: Hosting button.

Image description

Once we filled out hosting details then click on the Next: Monitoring button.

Image description

So here we can enable the application insight for our function app. By default, it takes the same name as the function name but if you want to give a custom name then click on the "Create new" button and then enter the name for insight & also you can select a preferred location.

Image description

Now click on Review:Create button and review all the details and click on the Create button. Wait for a few minutes to create the resources.

Open function app -> click on Application Insight from the left pane. You will see that our app insight is now linked to the function app.

Image description

Open the application insight and you can verify the Instrumentation Key here.

Image description

Go to Configuration to see that Azure automatically added APPINSIGHTS_INSTRUMENTATIONKEY into our function app.

Image description

Integrate Application Insight into Existing Function App

If you have already created a function app and at that time you haven't enabled the Application Insight then you can either integrate the function app to existing app insight if you have any or create a new app insight resource and integrate it with your function app.

Click on the Create Resource button and search for Application Insight and click on the Create button.

Image description

Fill in all the details and click on the Review + create button. Wait for a few minutes to create the resources.

Image description

Now open your function app to which you want to link and click on Application Insights from the left sidebar. Click on the Turn on Application Insights button.

Image description

Now you can either create resources or select the existing app insight. Select the existing application insight resource to create in the above step. Finally, click on the Apply button.

Image description

That's it we have now integrated application insight into our function app.

Image description

Log data from Azure Functions into Application Insights

For the function app to send data to Application insight it requires the instrumentation key of the application insight resource. First, we will create a function app locally using Visual Studio and adding the instrumentation key into the local.settings.json file.

Open a visual studio and create a new function app. If you are not familiar with how to create it from Visual Studio then check my article here.

Image description

Go to your application insight resource and copy the instrumentation key

Image description

Open local.settings.json file & add a new setting called APPINSIGHTS_INSTRUMENTATIONKEY and paste the copied key from the 2nd step.



{  
    "IsEncrypted": false,  
    "Values": {  
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",  
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",  
        "APPINSIGHTS_INSTRUMENTATIONKEY": "5916dc8e-7c17-4f59-ab1d-932ea1abac21" //<replace instrumentation key here>  
    }  
}


Enter fullscreen mode Exit fullscreen mode

Run the app and hit the function URL. You can see the telemetry data into application insight.

Image description

Image description

Image description

Conclusion

In this article, we have discussed about Application Insights and also different ways to integrate application insights into the Azure function app. I really hope that you enjoy this article, share it with friends, and please do not hesitate to send me your thoughts or comments.

Stay tuned for more Azure Functions articles.

You can follow me on Twitter @sumitkharche01 and LinkedIn

Happy Coding!

Top comments (0)