c# - 应用程序洞察 - TelemetryClient - DependencyTelemetry - UseSampling

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

我正在尝试使用我的 AppInsightsHelper 类启用采样,该类启动依赖操作来跟踪性能。

这就是我初始化 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 中添加了: exceptedTypes: "Dependency"以查看是否发生任何情况,并且正如预期的那样,依赖项不会被忽略。

最佳答案

如果是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通过使用 telemetryConfiguration。

关于c# - 应用程序洞察 - TelemetryClient - DependencyTelemetry - UseSampling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631566/

相关文章:

c# - 任务服务不工作

.net - 当涉及 (WPF) native 图像时,如何获得混合模式小型转储的完整堆栈跟踪?

c# - 在 metro 中,FolderInformation 和 StorageFolder 之间有什么区别

sql - Azure SQL,将数据库的大部分内容复制到现有的(不是新的)同一服务器中

c# - 相当于将我的 newtonsoft 实现转换为 .net core 3.0 中的新 JSON 库期间的默认值

c# - 对 DataTable 进行排序不起作用

c# - 从 xslt 中,您可以输出整个 XML 吗?

c# - 在 Xamarin 的 View 中将 ViewModel 属性与 ListView 绑定(bind)

azure - 在同一reactjs应用程序和aspnet core web api中混合Azure AD和Azure AD B2C身份验证

azure - 无法使用 Azure Bicep 创建资源组