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
  1. Log in to the Azure portal. In the Search bar, search as "function app" and then select the Function app. 

    How To Integrate Application Insights Into Azure Functions

    How To Integrate Application Insights Into Azure Functions

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

    How To Integrate Application Insights Into Azure Functions

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

    How To Integrate Application Insights Into Azure Functions

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

    How To Integrate Application Insights Into Azure Functions

    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.

    How To Integrate Application Insights Into Azure Functions

  5. 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. 

  6. 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.

    How To Integrate Application Insights Into Azure Functions

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

    How To Integrate Application Insights Into Azure Functions

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

    How To Integrate Application Insights Into Azure Functions

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. 
  1. Click on the Create Resouce button and search for Application Insight and click on the Create button.

    How To Integrate Application Insights Into Azure Functions

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

    How To Integrate Application Insights Into Azure Functions

  3. 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.

    How To Integrate Application Insights Into Azure Functions

  4. 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. 

    How To Integrate Application Insights Into Azure Functions

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

    How To Integrate Application Insights Into Azure Functions

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. 
  1. 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.

    How To Integrate Application Insights Into Azure Functions

  2. Go to your application insight resource and copy the instrumentation key

    How To Integrate Application Insights Into Azure Functions

  3. Open local.settings.json file & add a new setting called  APPINSIGHTS_INSTRUMENTATIONKEY and paste the copied key from the 2nd step.
    1. {  
    2.     "IsEncrypted"false,  
    3.     "Values": {  
    4.         "AzureWebJobsStorage""UseDevelopmentStorage=true",  
    5.         "FUNCTIONS_WORKER_RUNTIME""dotnet",  
    6.         "APPINSIGHTS_INSTRUMENTATIONKEY""5916dc8e-7c17-4f59-ab1d-932ea1abac21" //<replace instrumentation key here>  
    7.     }  
    8. }  
  4. Run the app and hit the function URL. You can see the telemetry data into application insight.

    How To Integrate Application Insights Into Azure Functions

    How To Integrate Application Insights Into Azure Functions

    How To Integrate Application Insights Into Azure Functions

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!