Application Insights - TelemetryClient - DependencyTelemetry - UseSampling

Application Insights - TelemetryClient - DependencyTelemetry - UseSampling

我正在尝试使用我的 AppInsightsHelper class 启用采样,它会启动 Depedancy 操作以跟踪性能。

这就是我初始化 TelematryClient 的方式:

 public ApplicationInsightsHelper(string key)
 {
        var config = TelemetryConfiguration.CreateDefault();
        config.InstrumentationKey = key;
        config.DefaultTelemetrySink.TelemetryProcessorChainBuilder.UseAdaptiveSampling(maxTelemetryItemsPerSecond: 1);

        _telemetryClient = new TelemetryClient(config);
 }

然后开始和停止操作:

   IOperationHolder<DependencyTelemetry> operation = null;
   operation = _telemetryClient.StartOperation<DependencyTelemetry>(friendlyName);
   operation.Telemetry.Name = friendlyName;
   operation.Telemetry.Type = type;
   operation.Telemetry.Timestamp = DateTime.UtcNow;

   operation.Telemetry.Duration = DateTime.UtcNow - operation.Telemetry.Timestamp;
   _telemetryClient.StopOperation(operation);

问题是上面的代码似乎忽略了采样设置并且跟踪了所有操作。我还在 UseAdaptiveSampling 中包含:excludedTypes: "Dependency" 以查看是否发生任何事情,并且正如预期的那样,不会忽略依赖项。

如果是azure函数,可以通过host.json设置采样,详见here and here。示例如下:

{
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "maxTelemetryItemsPerSecond" : 1
      }
    }
  }
}

如果你想在设置中使用 TelemetryClient,你应该遵循这个 article。在 azure 函数的构造函数中,使用如下代码:

        /// Using dependency injection will guarantee that you use the same configuration for telemetry collected automatically and manually.
        public HttpTrigger2(TelemetryConfiguration telemetryConfiguration)
        {
            this.telemetryClient = new TelemetryClient(telemetryConfiguration);
        }

但截至目前,有一个 issue 通过使用 telemetryConfiguration。

我已经为 ASP.NET Web 应用程序工作。我添加了以下配置并专门添加了我的 'MaksingTelemetryInitializer'.

    public void StartApplicationInsights(string logType)
    {
        string appInsightsComponentId = string.Empty;
        try
        {
            telemetryClient = new TelemetryClient();
            TelemetryConfiguration.Active.InstrumentationKey = GetConfigvalue("AppInsightsAppId"); ;
            TelemetryConfiguration.Active.TelemetryInitializers.Add(new MaskingTelemetryInitializer());
        }
        catch (Exception exception)
        {
            // Log Exception to WadLog if logging to Wadlog is enabled
            if (logType != LoggingType.Both) return;
            WadLogWriter.LogToWadLogs(Logger.BuildErrorString(exception), EventLevel.Error);
        }
    }

这里我想要掩码 PII 数据电子邮件 ID,它正在工作。