Application Insights - Undefined Role Name in the Cloud since 2.4.0-beta3
We are testing a preview of the role of multiple applications as shown here:
https://azure.microsoft.com/en-us/blog/app-insights-microservices/
We added the 2.4.0-beta3 packages for appinsights and appinsights.windowsserver because the application we are using is currently hosted on IIS.
Ours cloud_rolename
seems undefined in our request telemetry. Is there anything else we need to do besides updating the packages?
We also found this:
AzureRoleEnvironmentTelemetryInitializer
updates the properties RoleName
and RoleInstance
device context for all telemetry items with information retrieved from the Azure Runtime.
.. although our property is Cloud_RoleInstance
correctly filled.
source to share
For ASP.NET applications, I had to assign Cloud.RoleName
using customITelemetryInitializer
to assign a property ITelemetry.Context.Cloud.RoleName
that will assign the correct context for a given request when using multi-component applications .
Telemetry initializer for multicomponent applications
public class MultiComponentTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry?.Context?.Cloud == null) return;
requestTelemetry.Context.Cloud.RoleName = "myapp-api";
}
}
This initializer only works on server side APIs - I have n't found an equivalent hook for the client side JS SDK .
source to share
If you want to enable instance and role name in cloud using js sdk apps, I managed to do this:
appInsights.queue.push(() => {
appInsights.context.addTelemetryInitializer((envelope) => {
envelope.tags["ai.cloud.role"] = "your role name";
envelope.tags["ai.cloud.roleInstance"] = "your role isntance";
});
});
source to share
In web applications, Cloud_RoleName is the value of the WEBSITE_SITE_NAME environment variable , which is automatically set when hosted in Azure.
For local objects, you can set this variable manually.
For debugging environment variables can be set in project settings> Debugging> Environment Variables (example) .
Source code for the app details, where Cloud_RoleName .
source to share
It was Brian's answer, but it was rejected, which is illogical.
If you want to include the instance and name of the cloud role in the insights of the js sdk app, I got it all:
appInsights.queue.push(() => {
appInsights.context.addTelemetryInitializer((envelope) => {
envelope.tags["ai.cloud.role"] = "your role name";
envelope.tags["ai.cloud.roleInstance"] = "your role isntance";
});
});
You have to add this to your layouts / master pages using the Application Insights init script just before appInsights.trackPageView();
it becomes:
var appInsights=window.appInsights||function(config)
{
function r(config){ t[config] = function(){ var i = arguments; t.queue.push(function(){ t[config].apply(t, i)})} }
var t = { config:config},u=document,e=window,o='script',s=u.createElement(o),i,f;for(s.src=config.url||'//az416426.vo.msecnd.net/scripts/a/ai.0.js',u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=['Event','Exception','Metric','PageView','Trace','Ajax'];i.length;)r('track'+i.pop());return r('setAuthenticatedUserContext'),r('clearAuthenticatedUserContext'),config.disableExceptionTracking||(i='onerror',r('_'+i),f=e[i],e[i]=function(config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o),s}),t
}({
instrumentationKey:'Instrumentation Key'
});
window.appInsights = appInsights;
appInsights.queue.push(() => {
appInsights.context.addTelemetryInitializer((envelope) => {
envelope.tags["ai.cloud.role"] = "your role name";
envelope.tags["ai.cloud.roleInstance"] = "your role isntance";
});
});
appInsights.trackPageView();
Source1: Modifying and Filtering Telemetry Using the AppInsights JavaScript SDK Telemetry Initializer
Source2: Filtering and Preprocessing Telemetry in Application Insights SDK
SDK Reference: addTelemetryInitializer
source to share
If you are using the @microsoft/applicationinsights-web
npm package , then the shorter version would be:
const appInsights = new ApplicationInsights({
config: {
instrumentationKey: 'your instrumentation key'
}
});
appInsights.loadAppInsights();
appInsights.addTelemetryInitializer((telemetryItem) => {
telemetryItem.tags['ai.cloud.role'] = 'your app name';
});
source to share