0

我有一个角度项目,我想将 AppInsight 与遥测(自定义角色)包括在内。该项目是用 Angular 和 TypeScript 编写的,我按照本教程将 appinsights 集成到 Angular 项目中,它似乎工作正常。我按照此链接 添加自定义遥测(使用角色),但它是用 Java/JavaScript/Node.JS 编写的

当我尝试使用 JS 代码时

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

我收到一个错误:

TS2339: Property 'queue' does not exist on type 'Initialization'. 

这可能是因为它在JS而不是TS中。

我也尝试遵循此解决方案但没有任何成功

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

这是我的MonitoringService的代码

import {Injectable} from '@angular/core';
import {environment} from '../../../../environments/environment';
import {ApplicationInsights} from '@microsoft/applicationinsights-web';

@Injectable()
export class MonitoringService {
  appInsights: ApplicationInsights;

  constructor() {
    this.appInsights = new ApplicationInsights({
      config: {
        instrumentationKey: environment.appInsights.instrumentationKey,
        enableAutoRouteTracking: true // option to log all route changes
      }
    });
    this.appInsights.queue.push(() => {
      this.appInsights.addTelemetryInitializer((envelope) => {
        envelope.tags["ai.cloud.role"] = "your role name";
        envelope.tags["ai.cloud.roleInstance"] = "your role instance";
      });
    });
    this.appInsights.loadAppInsights();
  }

  logPageView(name?: string, url?: string) { // option to call manually
    this.appInsights.trackPageView({
      name: name,
      uri: url
    });
  }

  logEvent(name: string, properties?: { [key: string]: any }) {
    this.appInsights.trackEvent({name: name}, properties);
  }

  logMetric(name: string, average: number, properties?: { [key: string]: any }) {
    this.appInsights.trackMetric({name: name, average: average}, properties);
  }

  logException(exception: Error, severityLevel?: number) {
    this.appInsights.trackException({exception: exception, severityLevel: severityLevel});
  }

  logTrace(message: string, properties?: { [key: string]: any }) {
    this.appInsights.trackTrace({message: message}, properties);
  }
}

我还尝试将 Microsoft 依赖项从 2.4.4 更新到 2.5.10,但没有做出任何改变。

当我打开 AppInsights 时,有一个角色,但它没有正确设置

在此处输入图像描述

4

1 回答 1

0

我没有完全相同的问题,但我需要在 loadAppInsigthts 之后运行遥测初始化程序,如下所示:

      this.appInsights.loadAppInsights();

      const telemetryInitializer = (envelope) => {
        envelope.tags['ai.cloud.role'] = 'name-of-your-app';
      };
      this.appInsights.addTelemetryInitializer(telemetryInitializer);

(来源:https ://github.com/microsoft/applicationinsights-js#example-setting-cloud-role-name )

这有什么区别吗?

(我使用的是 2.5.9 版)

于 2020-12-03T16:12:08.847 回答