How To Integrate Application Insights Into Azure Functions

Introduction

In this article, we are going to discuss Application Insights and how to integrate Application Insight into the Azure Function app. We will also create a simple Azure function app and log 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, and 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 include powerful analytics tools to help you diagnose issues and understand what users actually do with your app.

Application insights collect telemetry data from connected apps and provide 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 APPINSIGHTS_INSTRUMENTATIONKEY

  1. Log in to the Azure portal. In the Search bar, search "function app" and then select the Function app. 
    Azure portal
    Function app
  2. After that click on the "Add" button to add a new function app. Fill out the basic details,
    Add button
  3. Once we fill out the basic details, then click on the Next: Hosting button.
    Hosting button
  4. Once we fill out the hosting details then click on the Next: Monitoring button.
    Monitoring button
  5. 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.
    Create new
  6. Now click on the Review: Create button review all the details and click on the Create button. Wait for a few minutes to create the resources.
  7. Open the function app -> click on Application Insight from the left pane. You will see that our app insight is now linked to the function app.
    Open function app
  8. Open the application insight and you can verify the Instrumentation Key here.
    Instrumentation Key
  9. Go to Configuration to see that Azure automatically added APPINSIGHTS_INSTRUMENTATIONKEY into our function app.
     Configuration

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 search for Application Insight and click on the Create button.
    Create Resouce button
  2. Fill in all the details and click on the Review + Create button. Wait for a few minutes to create the resources.
    Create button
  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.
    Application Insights button
  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.
    Apply button
  5. That's it we have now integrated application insight into our function app.
    Integrated application insight

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 add the instrumentation key to 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.
    Visual studio
  2. Go to your application insight resource and copy the instrumentation key
    Application insight resource
  3. Open the 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>
        }
    }
    
  4. Run the app and hit the function URL. You can see the telemetry data in application insight.
    Function URL
    New az
    Transaction

Conclusion

In this article, we have discussed 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!