【问题标题】:Application Insights - TelemetryClient - DependencyTelemetry - UseSamplingApplication Insights - TelemetryClient - DependencyTelemetry - UseSampling
【发布时间】:2020-01-07 15:40:20
【问题描述】:

我正在尝试使用我的 AppInsightsHelper 类启用采样,该类启动 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" 以查看是否发生任何事情,并且正如预期的那样,Dependencies 不会被忽略。

【问题讨论】:

  • 这是一个 .NET 框架 Web 项目(不是 .NET 核心)吗?以及如何使用 AppInsightsHelper 类?
  • 是的,.NetCore。基本上我在 AzureFunction 的构造函数中初始化了一次类。
  • 能否提供azure函数的代码?
  • 而且如果是azure函数,应该很容易在host.json中使用采样设置,见here
  • 您好,感谢您的帮助。我已经设置了这个并将对其进行测试。我唯一的问题是,这条规则是否也适用于我在代码级别跟踪的遥测数据,还是仅适用于 AzureFunction(通过 ILogger)生成的日志。

标签: c# .net azure azure-application-insights


【解决方案1】:

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

{
  "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

【讨论】:

    【解决方案2】:

    这对我来说适用于 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,它正在工作。

    【讨论】:

      猜你喜欢
      • 2016-08-14
      • 2017-10-03
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 2019-07-14
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多