Azure AppInsights默认TelemetryInitializer-Javascript SDK

时间:2019-06-08 13:38:37

标签: javascript azure-application-insights

我正在使用NPM版本的AppInsights,并且似乎无法将此遥测初始化程序附加到每个传出的信封:

import { ApplicationInsights } from '@microsoft/applicationinsights-web'
appInsights = new ApplicationInsights({ config: {
  instrumentationKey: 'removed',
  autoTrackPageVisitTime: true
}});

appInsights.loadAppInsights();

var userInitializer = (envelope) => {
  var telemetryItem = envelope.baseData;
  telemetryItem.properties = telemetryItem.properties || {};
  telemetryItem.properties["role"] = userProfile.role;
}
appInsights.queue.push(function () {
  appInsights.context.addTelemetryInitializer(userInitializer);
});

问题是appInsights.queue始终未定义。我唯一能找到的就是将其包装在检查中,以确保它不会尝试执行两次,但事实并非如此,它永远不会执行。如果我在每次跟踪之前分别调用遥测初始化程序,则它会很好地工作:

appInsights.addTelemetryInitializer(userInitializer);
appInsights.trackPageView();

当我console.log(appInsights)时,绝对没有队列属性,但是在每个示例中都使用它。

3 个答案:

答案 0 :(得分:1)

这对我们有用

this.appInsights.addTelemetryInitializer(envelope => {
  envelope.tags['ai.cloud.role'] = 'your cloud role name';
  envelope.baseData.properties['item'] = 'some property';
});

答案 1 :(得分:0)

调用两次代码会发生这种情况,而第二次调用不再存在“ queue ”。解决方案是使代码段带有选中项

if (this.AppInsights.queue) {
        this.AppInsights.queue.push(function() { 
        this.AppInsights.context.addTelemetryInitializer(function(envelope) {
        var telemetryItem = envelope.data.baseData;

        telemetryItem.Properties = telemetryItem.Properties || {};
        telemetryItem.Properties["prop1"] = "This is a custom property";
        telemetryItem.Properties["prop2"] = "This is another custom property";
      });
    });
}

希望有帮助。

答案 2 :(得分:0)

您应该能够做到

appInsights.addTelemetryInitializer(userInitializer);