Application Insights - Undefined Cloud Role Name with 2.4.0-beta3

孤街醉人 提交于 2021-01-21 09:52:48

问题


We're trying out the app insights multi role preview as announced here:

https://azure.microsoft.com/en-us/blog/app-insights-microservices/

https://docs.microsoft.com/en-us/azure/application-insights/app-insights-monitor-multi-role-apps#use-cloudrolename-to-separate-components

We've added the 2.4.0-beta3 packages for appinsights & appinsights.windowsserver as the app we're using is hosted on prem (IIS) currently.

Our cloud_rolename seems to be undefined in our request telemetry. Is there anything further we need to do other than updating the packages?

We also found this:

AzureRoleEnvironmentTelemetryInitializer updates the RoleName and RoleInstance properties of the Device context for all telemetry items with information extracted from the Azure runtime environment.

..though our Cloud_RoleInstance property is being properly populated.


回答1:


For ASP.NET apps, I had to assign Cloud.RoleName using a custom ITelemetryInitializer to assign ITelemetry.Context.Cloud.RoleName property which will assign the proper context for a given request when using multi-component apps.

Telemetry Initializer for Multi-Component Apps

  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 the server side APIs - I have not found the equivalent hook for the client JS SDK.




回答2:


If you want to include cloud role instance and name with the app insights js sdk, I had success doing this:

appInsights.queue.push(() => {
appInsights.context.addTelemetryInitializer((envelope) => {
  envelope.tags["ai.cloud.role"] = "your role name";
  envelope.tags["ai.cloud.roleInstance"] = "your role isntance";
});

});




回答3:


In web applications, Cloud_RoleName is a value of an environment variable WEBSITE_SITE_NAME which is set automatically when hosted in Azure.
For on-premises you can set this variable manually.
For debugging environment variables can be set in Project settings > Debug > Environment variables (example).

Source code of Application insights where Cloud_RoleName is retrieved.




回答4:


Make sure you add all the relevant appinsights packages. I had the same problem with my Stateless service fabric service. After adding Microsoft.ApplicationInsights.ServiceFabric package rolename is added to the telemetry data.




回答5:


This was an edit to Brian's answer but it was rejected which is illogical tbh.

If you want to include cloud role instance and name with the app insights js sdk, I had success doing this:

appInsights.queue.push(() => {
    appInsights.context.addTelemetryInitializer((envelope) => {
      envelope.tags["ai.cloud.role"] = "your role name";
      envelope.tags["ai.cloud.roleInstance"] = "your role isntance";
    });
});

You should add this in your layout/master pages with the Application Insights initialization script right before appInsights.trackPageView(); so 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 with AppInsights JavaScript SDK Telemetry Initializer

Source2: Filtering and preprocessing telemetry in the Application Insights SDK

SDK Reference: addTelemetryInitializer




回答6:


If you are using @microsoft/applicationinsights-web npm package then 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';
});


来源:https://stackoverflow.com/questions/44137269/application-insights-undefined-cloud-role-name-with-2-4-0-beta3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!