Iva*_*tev 5 .net c# azure azure-application-insights
我的问题是:
在我的方案中注入自定义关联/操作ID的正确/更好的方法是什么,这不会导致内存泄漏并且在Azure门户上正确使用和显示?
以下是我的方案的具体细节:
我有一个Azure辅助角色,它不断地从Azure队列中出列消息并进行处理.该消息包含相关ID(操作ID)和操作名称,我希望将其用于在该消息的处理工作期间通过Application Insights收集和推送的所有遥测.
目前实现的方式是通过自定义ITelemetryInitializer,看起来像这样:
public class MiTelemetryInitialiser : ITelemetryInitializer
{
private readonly ILoggingContext _context;
public MiTelemetryInitialiser(ILoggingContext context)
{
_context = context;
}
public void Initialize(ITelemetry telemetry)
{
if (string.IsNullOrEmpty(telemetry.Context.Operation.Id) && _context != null)
{
telemetry.Context.Operation.Id = _context.OperationId;
if (!String.IsNullOrWhiteSpace(_context.OperationName))
telemetry.Context.Operation.Name = _context.OperationName;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我的worker角色处理循环看起来像这样(si:
while(!cancellationToken.IsCancellationRequested) {
// 1. Get message from the queue
// 2. Extract operation name and Correlation Id from the message
TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
var loggingContext = //new logging context with operation name and correlation id
config.TelemetryInitializers.Add(new MiTelemetryInitialiser(loggingContext));
TelemetryClient telemetryClient = new TelemetryClient(config);
// do work
client.Flush();
}
Run Code Online (Sandbox Code Playgroud)
我用这个TelemetryChannel:
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/>
Run Code Online (Sandbox Code Playgroud)
有两个问题:
ServerTelemetryChannel(不幸的是不像大多数.NET SDK - 这个通道不是开源的)并且由于紧密的循环?client.TrackRequest()推送我的自定义关联ID(使用Fiddler验证) - Application Insights会在Azure门户上覆盖它并显示自己的操作ID而不是我的.请求期间的其他遥测具有我设置的正确OperationId.这是运行循环约10分钟后内存泄漏的照片(一段时间后增长到1GB +):
小智 2
对于内存泄漏问题——我们找到了根本原因,我们将在下一个版本中修复它。但是,在您的代码示例中,我们建议不要每次都创建新通道,而是重用相同的通道。它将允许更好的批处理和更少的内存开销:
TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
var loggingContext = //new logging context with operation name and correlation id
config.TelemetryInitializers.Add(new MiTelemetryInitialiser(loggingContext));
while(!cancellationToken.IsCancellationRequested) {
// 1. Get message from the queue
// 2. Extract operation name and Correlation Id from the message
TelemetryClient telemetryClient = new TelemetryClient(config);
// do work
client.Flush();
}
Run Code Online (Sandbox Code Playgroud)
或者只是使用这个:
while(!cancellationToken.IsCancellationRequested) {
// 1. Get message from the queue
// 2. Extract operation name and Correlation Id from the message
TelemetryClient telemetryClient = new TelemetryClient();
// do work
client.Flush();
}
Run Code Online (Sandbox Code Playgroud)
并将遥测初始值设定项添加到ApplicationInsigths.config:
<Add Type="Namespace.MiTelemetryInitialiser , AssemblyName" />
Run Code Online (Sandbox Code Playgroud)
另请参阅我的博客文章,了解如何将遥测初始化程序添加到全局单例配置中:
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.TelemetryInitializers.Add(new MiTelemetryInitialiser());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1421 次 |
| 最近记录: |