App Insights:禁用 SQL 依赖项遥测
App Insights: Disable SQL Dependency telemetry
我正在为网站使用 Azure Application Insights(Azure 应用服务)。
我正在使用集群 Umbraco 设置和 hangfire。仅这两个就每分钟都在访问数据库,并充斥着我的 'App Insights'.
所以我的问题是,如何禁用 Sql Dependency Tracker?
我查看了 ApplicationInsights.config 并没有发现任何明显的内容。
我可以看到 Microsoft.ApplicationInsights.DependencyCollector
这可能是负责任的,但我不想删除所有类型的依赖遥测,仅 sql.
谢谢
你最好的选择是使用遥测处理器来过滤掉某些类型的依赖请求。查看下面的这些资源以获取信息。
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;
}
}
我们在 .net5 asp.net 项目中使用像这样的遥测处理器过滤掉 hangfire SQL。请注意,我们为 Hangfire 使用了不同的数据库,因此遥测处理器可以通过检查它是否连接到 Hangfire 数据库来轻松关闭 hangfire SQL。
在Startup.Configure()
内:
var hangFireConnectionString = // ... get from somewhere
configuration.TelemetryProcessorChainBuilder
.Use(next => new IgnoreHangfireTelemetry(next,hangFireConnectionString))
.Build();
这是处理器 class:
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,则需要过滤掉更多命令。只需逐步使用调试器,看看您需要排除哪些。
我正在为网站使用 Azure Application Insights(Azure 应用服务)。 我正在使用集群 Umbraco 设置和 hangfire。仅这两个就每分钟都在访问数据库,并充斥着我的 'App Insights'.
所以我的问题是,如何禁用 Sql Dependency Tracker?
我查看了 ApplicationInsights.config 并没有发现任何明显的内容。
我可以看到 Microsoft.ApplicationInsights.DependencyCollector
这可能是负责任的,但我不想删除所有类型的依赖遥测,仅 sql.
谢谢
你最好的选择是使用遥测处理器来过滤掉某些类型的依赖请求。查看下面的这些资源以获取信息。
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;
}
}
我们在 .net5 asp.net 项目中使用像这样的遥测处理器过滤掉 hangfire SQL。请注意,我们为 Hangfire 使用了不同的数据库,因此遥测处理器可以通过检查它是否连接到 Hangfire 数据库来轻松关闭 hangfire SQL。
在Startup.Configure()
内:
var hangFireConnectionString = // ... get from somewhere
configuration.TelemetryProcessorChainBuilder
.Use(next => new IgnoreHangfireTelemetry(next,hangFireConnectionString))
.Build();
这是处理器 class:
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,则需要过滤掉更多命令。只需逐步使用调试器,看看您需要排除哪些。