How to integrate Application Insights into Azure Functions
Tags: azure, serverless, cloud, beginners
Published: 2022-04-10 09:42:50

## 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:

- [How To Create And Publish Azure Function From Visual Studio](https://dev.to/sumitkharche/how-to-create-and-publish-azure-function-from-visual-studio-2cdc)
- [How To Easily Create Azure Functions Using Azure Portal](https://dev.to/sumitkharche/how-to-easily-create-azure-functions-using-azure-portal-5ghd)


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](https://docs.microsoft.com/en-us/azure/azure-monitor/overview) is a service provided by Azure to monitor, analyze, improve the availability and performance of your application. 

According to Microsoft [docs](https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview),
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
- [Azure ](https://portal.azure.com/)account (If you don't have an Azure subscription, [create a free trial account](https://azure.microsoft.com/free))

## 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](https://portal.azure.com/). In the Search bar, search as "function app" and then select the Function app

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdqwuzisu3dwmsdciqdd.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyyq9mw9jtx6w6ae1lwe.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dl8janzmzpn8lg1sguh.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8j4inie8u0fp6p7zxni.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7uzodx0hskfrxwvtn4e.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0dypx7bq9pbbc1bwbgh4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuy901duy628yruc66j2.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcsxxaa6r7gtfvqgq9mf.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dlck5fvzza3x8o4b8zc.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hccmwqhhw42wp3bzlcj.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tgwfxsbraka7xv72xub.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tfra8h8o28lbfjo3rv2.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2b4ld46vs1faj8fpaoq.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw8pk70ggwvwzp1u1d5b.png)

## 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](https://www.c-sharpcorner.com/article/how-to-create-publish-azure-function-from-visual-studio/).   

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00z8f50aixnwfeoyoyum.png)

Go to your application insight resource and copy the instrumentation key

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rj2uq63c0pda5862qym1.png)

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

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

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsphk13f5jashhc47mde.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0py7u7rpzf927fse4p1p.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0alzl0ruffnguvfsd8hh.png)

## 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](https://twitter.com/sumitkharche01) and [LinkedIn](https://www.linkedin.com/in/sumit-kharche-890426a9/)
 
Happy Coding!