【发布时间】:2018-02-26 11:53:19
【问题描述】:
作为我们正在构建的基于微服务的解决方案的一部分,我们在一个 Azure Function App 中拥有多个 Azure Functions。这些函数编排了对不同 API 的大量请求,其中一些请求需要很长时间才能完成。我们向函数添加了 Application Insights 以允许对发出的请求进行一些跟踪,但依赖项跟踪在 Azure Functions 中尚不工作。可以手动跟踪依赖关系,但这涉及在每个依赖调用周围插入一些跟踪代码,但是我们希望避免手动跟踪每个调用的依赖关系。
我想到的解决方案之一是创建一个请求跟踪器来跟踪来自函数的所有传出 Web 请求。在请求跟踪器中,我可以跟踪依赖请求,包括它们的时间。我想将请求跟踪器连接到某种网络流量处理程序中,不幸的是我找不到太多关于这样做的信息。很多posts 提到为此使用 System.Net 跟踪编写器,但据我所知,这需要一个 Web.config 来设置,而函数没有。
我看到一些帖子提到创建请求包装器并将其放置在我的传出请求中,但不幸的是,这不是一个选项,因为我们使用了许多在内部发出请求的包。如果您有任何想法可以让我朝着正确的方向前进,请告诉我。谢谢
更新:
我添加了以下帮助方法,它允许我手动跟踪任务作为依赖请求
public static async Task<T> TrackDependency<T>(this Task<T> task, string dependecyName, string callName, string operationId)
{
var telemtryClient = new TelemetryClient();
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
var success = true;
T result = default(T);
try
{
result = await task;
}
catch (Exception)
{
success = false;
}
finally
{
timer.Stop();
var dependencyTelemetry = new DependencyTelemetry(dependecyName, callName, startTime, timer.Elapsed, success);
dependencyTelemetry.Context.Operation.Id = operationId;
telemtryClient.Track(dependencyTelemetry);
}
return result;
}
然后可以按如下方式使用:
client.Accounts.UpdateWithHttpMessagesAsync(accountId, account).TrackDependency("Accounts", "UpdateAccounts", requestContextProvider.CorrelationId);
我现在可以在 Application Insights 中看到各个请求的依赖关系,但显然对它们的实际遥测非常有限,它不包含路径信息或其他很多信息。
【问题讨论】:
标签: c# azure azure-functions azure-application-insights system.net