【问题标题】:App Insights: Disable SQL Dependency telemetryApp Insights:禁用 SQL 依赖遥测
【发布时间】:2016-11-14 05:22:06
【问题描述】:

我正在将 Azure Application Insights 用于网站(Azure 应用服务)。 我正在使用集群的 Umbraco 设置和hangfire。仅这两个就每分钟都在访问数据库,并且淹没了我的“App Insights”。

所以我的问题是,如何禁用 Sql Dependency Tracker? 我查看了 ApplicationInsights.config 并找不到任何明显的东西。 我可以看到Microsoft.ApplicationInsights.DependencyCollector 这可能是负责任的,但我不想删除所有类型的依赖遥测, sql。

谢谢

【问题讨论】:

    标签: azure azure-sql-database azure-application-insights


    【解决方案1】:

    您最好的选择是使用遥测处理器来过滤掉某些类型的依赖请求。查看下面的这些资源以获取信息。

    Sampling, filtering and preprocessing telemetry in the Application Insights SDK

    Request filtering in Application Insights with Telemetry Processor

    示例处理器可能如下所示。

    using Microsoft.ApplicationInsights.Channel;
    using Microsoft.ApplicationInsights.Extensibility;
    using Microsoft.ApplicationInsights.DataContracts;
    
    public class NoSQLDependencies : ITelemetryProcessor
    {
        private ITelemetryProcessor Next { get; set; }
    
        // Link processors to each other in a chain.
        public NoSQLDependencies(ITelemetryProcessor next)
        {
            this.Next = next;
        }
        public void Process(ITelemetry item)
        {
            if (IsSQLDependency(item)) { return; }
            this.Next.Process(item);
        }
    
        private bool IsSQLDependency(ITelemetry item)
        {
            var dependency = item as DependencyTelemetry;
            if (dependency?.DependencyTypeName == "SQL")
            {
                return true;
            }
            return false;
        }
    }
    

    【讨论】:

    • 您是否知道 DependencyTypeName 的可能值是什么?我想停止跟踪对 blob 存储的访问
    • @batmaci 是的,我做到了。我创建了一个 ITelemetryProcessor 在调试器中运行应用程序,在处理器中放置一个条件断点,直到找到我想要阻止的遥测。检查 ITelemetry 项目的属性允许我定义标准以过滤掉我想要过滤掉的特定遥测数据,如本答案所示
    • github.com/Microsoft/ApplicationInsights-dotnet-server/blob/…找到源代码 public const string SQL = "SQL";公共常量字符串 HTTP = "Http";公共常量字符串 AzureBlob = "Azure blob";公共常量字符串 AzureTable = "Azure 表"; public const string AzureQueue = "Azure 队列";
    • 只能禁用hangfire sql。我还想看标准的 sql 查询吗?
    • DependencyTypeNameDependencyKind 现在都已过时。只使用Type
    【解决方案2】:

    在 .net5 asp.net 项目中,我们使用类似这样的遥测处理器过滤掉挂起的 SQL。请注意,我们为 Hangfire 使用了不同的数据库,因此遥测处理器可以通过检查它是否连接到 Hangfire 数据库来轻松地输出 Hangfire SQL。

    Startup.Configure()内:

    var hangFireConnectionString = // ... get from somewhere
    
    configuration.TelemetryProcessorChainBuilder
        .Use(next => new IgnoreHangfireTelemetry(next,hangFireConnectionString))
        .Build();
    

    这是处理器类:

    public class IgnoreHangfireTelemetry : ITelemetryProcessor
    {
        private readonly ITelemetryProcessor next;
        private readonly string hangfireDashboardPath;
        private readonly string sqlDatabase;  // name of the hangfire db
    
        public IgnoreHangfireTelemetry(
            ITelemetryProcessor next,
            string sqlConnectionString = null,
            string hangfireDashboardPath = "/hangfire")
        {
            this.next = next ?? throw new ArgumentNullException(nameof(next));
    
            if (!string.IsNullOrEmpty(sqlConnectionString))
            {
                var builder = new SqlConnectionStringBuilder(sqlConnectionString);
    
                sqlDatabase = builder.InitialCatalog;
            }
    
            this.hangfireDashboardPath = hangfireDashboardPath ?? throw new ArgumentNullException(nameof(hangfireDashboardPath));
        }
    
        public void Process(ITelemetry item)
        {
            var request = item as RequestTelemetry;
    
            // If it's a request for Hangfire Dashboard don't record it
            if (request != null
                && request.Url.AbsolutePath.StartsWith(hangfireDashboardPath))
            {
                return;
            }
    
            var telemetry = item as DependencyTelemetry;
    
            // If it's a SQL dependency to the Hangfire db don't record it
            if (telemetry != null)
            {
                if (sqlDatabase != null  && telemetry.Type == "SQL"
                    && telemetry.Target.EndsWith($"| {sqlDatabase}", StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
            
                if (telemetry.Type == "SQL"
                    && telemetry.Name.ToLower().Contains("hangfire")
                    && telemetry.Success.GetValueOrDefault(false))
                {
                    return;
                }
            }
    
            // Looks like it's not Hangfire, process the telemetry as usual.
            next.Process(item);
        }
    }
    

    如果您不使用单独的 Hangfire 数据库,您可以通过检查其他 DependencyTelemetry 属性来实现相同的目的,例如查看 DependencyTelemetry.Data 或 .CommandName (包含 SQL 语句)并检查它是否包含 [Hangfire] (如果您已将 Hangfire 更改为使用不同的架构,则为另一个数据库架构名称)。如果您只是在 sql 上进行过滤,则需要过滤掉更多命令。只需逐步使用调试器,看看您需要排除哪些。

    【讨论】:

      猜你喜欢
      • 2020-09-08
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多