应用程序见解 - 遥测客户端 - 依赖项遥测 - 使用采样



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

这就是我初始化我的远程矩阵客户端的方式:

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 设置采样,有关详细信息,请参阅此处和此处。示例如下:

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

如果要将遥测客户端与设置一起使用,则应遵循本文。在 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);
}

但截至目前,使用遥测配置存在问题。

这已经为我 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,它正在工作。

最新更新