避免将响应代码2xx和3xx的遥测数据发送到Azure应用程序洞察

gijlo24d  于 2023-02-25  发布在  其他
关注(0)|答案(1)|浏览(79)

从我的Angular 应用程序,我想避免发送遥测数据的响应代码范围之间的2xx和3xx(不要索引他们),为我实现这个代码,但这些日志出现在我的人工智能日志无论如何!

import { Injectable } from '@angular/core';
import { ApplicationInsights, IExceptionTelemetry } from '@microsoft/applicationinsights-web';
import { Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root',
})
export class ApplicationInsightsService {
  private appInsights : ApplicationInsights;
  constructor(private router: Router) {
    this.appInsights = new ApplicationInsights({
      config: {
        instrumentationKey: `${environment.applicationInsights.instrumentationKey}`
      }
    });
    this.appInsights.loadAppInsights();
    this.loadCustomTelemetryProperties();
    this.createRouterSubscription();
  }
  setUserId(userId: string) {
    this.appInsights.setAuthenticatedUserContext(userId);
  }
  clearUserId() {
    this.appInsights.clearAuthenticatedUserContext();
  }
  logPageView(name?: string, uri?: string) {
    this.appInsights.trackPageView({ name, uri});
  }
  logException(error : Error){
    let exception : IExceptionTelemetry = {
      exception : error
    };
    this.appInsights.trackException(exception);
  }
  private loadCustomTelemetryProperties()
  {
    this.appInsights.addTelemetryInitializer(envelope => 
      {
        var item = envelope.baseData;
        item.properties = item.properties || {};
        item.properties["ApplicationPlatform"] = "WEB";
        item.properties["ApplicationName"] = "test";
        item.properties["roleName"] = "front";
        // Filter out logs with 2xx and 3xx responses
        if (item && item.responseCode) {
          const responseCode = parseInt(item.responseCode);
          if (responseCode >= 200 && responseCode < 400) {
            // Discard logs with 2xx and 3xx responses
            return ;
          }
        }
      }
    );
  }
  private createRouterSubscription()
  {
    this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event: NavigationEnd) => {
      this.logPageView(null, event.urlAfterRedirects);
    });
  }
}

我的疑问是如何使用envelope.baseData或www.example.com中的属性envelope.data?如何设置它们?以及如何避免发送它们?

zbsbpyhn

zbsbpyhn1#

据我所知,您应该返回false以指示应该丢弃这些事件。

if (responseCode >= 200 && responseCode < 400) {
            // Discard logs with 2xx and 3xx responses
            return false;
          }

参考:www.example.comhttps://learn.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#javascript-web-applications

相关问题