How to Set App Insights Cloud Role Name for Azure Functions
WEBSITE_CLOUD_ROLENAME environment variable (source).
Longer story ¶
In Azure, recommended way to collect logs from apps is to use Application Insights (which is pretty cool by the way). You even can construct the application maps to see how they communicate with each other.
App Insights Application Map
To create such map you will instruct your applications to use the same Application Insights Instance. But how to segregate logs to understand that they are coming from different apps?
If you are using some flavour of the Application Insights SDK, then this can be achieved by setting the name of the app in the
APPLICATIONINSIGHTS_ROLE_NAME environment variable or by following the instruction for the SDK.
Is it different for Azure Functions? ¶
Yes. Don’t ask me why, this is not the case for Azure Functions. Setting this variable will not give you the desired result.
You will see one name for all your function which is “Web” for some reason.
We need to go deeper ¶
What do we know?
We know that Azure is Microsoft thing. Microsoft loves .NET. What framework is there to build web apps? ASP.NET! What is the one popular service to run the apps in the Azure? Azure App Service!
Let’s see what environment variables Azure App Services have.
Okay, they have something called
Unfortunately it’s read-only.
Nice thing though, it seems like all environment variables start with
How Azure Function Hosts are built? Let’s take a look into Azure/azure-functions-host repo.
But before that… Azure Functions are activated by triggers and there is such thing called extension bundles.
Never was interested to go that deep, but let’s see what host bundles can be found in that repo and try to find a clue within them.
extensionrequirements.json file which tells what WebJobs to add…
What is this?
Let’s search for “azure webjobs sdk”.
The first link would lead to Azure/azure-webjobs-sdk.
Looking into WebJobs SDK documentation we can see that they are even using the same configuration files as for Azure Functions, e.g.
So Azure Functions are actually App Services baked by WebJobs SDK and some runtime spice. Nice. Back to the business.
Can we find in the repo something by the
Found the following commit which adds
WEBSITE_CLOUD_ROLENAME environment variable.
It resolves issue #2653 for Azure Static Web Apps (issue #204) to resolve that particular issue with naming of the apps in Application Insights.
Let’s open that file on the line with the change.
We see the variable
So here it is!
After adding it as part of the Azure Functions environment variables Application Insights started to name functions as I wanted.
✨ If you found this post helpful and want to say "Thank you! 💖", you can treat me with a cup of tea.