【问题标题】:Asynchronous HttpClient calls not shown as dependency in Azure App insights for App service automatically异步 HttpClient 调用未在 Azure App 洞察中自动显示为应用服务的依赖项
【发布时间】:2022-01-24 05:31:29
【问题描述】:

我是 Azure 应用洞察力的新手,想知道为什么异步 HttpClient 调用未在 Azure 应用程序洞察力中自动显示为应用服务的依赖项。 另外,我应该在 ASP.NET 项目中进行哪些配置更改或代码更改以跟踪 http 依赖关系?

当我参考文档时,它说“依赖项是由您的应用程序调用的组件。它通常是使用 HTTP、数据库或文件系统调用的服务。Application Insights 测量依赖项调用的持续时间,无论它是失败与否,以及依赖名称等附加信息。您可以调查特定的依赖调用,并将它们与请求和异常相关联。"

【问题讨论】:

  • 新观察:我看到 Http 依赖项被跟踪为 Application Insights 遥测日志中的请求的 AppDependency,但它在 Azure 门户中的依赖项下没有被跟踪。可能是什么原因?

标签: azure async-await httpclient azure-application-insights


【解决方案1】:

您可以使用以下代码示例自动跟踪http dependency

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DependencyCollector;
using Microsoft.ApplicationInsights.Extensibility;
using System.Net.Http;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();

            configuration.InstrumentationKey = "removed";
            configuration.TelemetryInitializers.Add(new HttpDependenciesParsingTelemetryInitializer());

            var telemetryClient = new TelemetryClient(configuration);
            using (InitializeDependencyTracking(configuration))
            {
                // run app...

                telemetryClient.TrackTrace("Hello World!");

                using (var httpClient = new HttpClient())
                {
                    // Http dependency is automatically tracked!
                    httpClient.GetAsync("https://microsoft.com").Wait();
                }

            }

            // before exit, flush the remaining data
            telemetryClient.Flush();

            // flush is not blocking when not using InMemoryChannel so wait a bit. There is an active issue regarding the need for `Sleep`/`Delay`
            // which is tracked here: https://github.com/microsoft/ApplicationInsights-dotnet/issues/407
            Task.Delay(5000).Wait();

        }

        static DependencyTrackingTelemetryModule InitializeDependencyTracking(TelemetryConfiguration configuration)
        {
            var module = new DependencyTrackingTelemetryModule();

            // prevent Correlation Id to be sent to certain endpoints. You may add other domains as needed.
            module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.windows.net");
            module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.chinacloudapi.cn");
            module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.cloudapi.de");
            module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.usgovcloudapi.net");
            module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("localhost");
            module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("127.0.0.1");

            // enable known dependency tracking, note that in future versions, we will extend this list. 
            // please check default settings in https://github.com/microsoft/ApplicationInsights-dotnet-server/blob/develop/WEB/Src/DependencyCollector/DependencyCollector/ApplicationInsights.config.install.xdt

            module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus");
            module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs");

            // initialize the module
            module.Initialize(configuration);

            return module;
        }
    }
}

注意:-

基于MSDOC Azure Monitor Application Insights Agent 目前仅支持 ASP.NET 4.x。

更多信息请参考以下链接:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2021-02-10
    • 2021-03-07
    • 2019-08-09
    • 2017-05-13
    • 2022-08-02
    相关资源
    最近更新 更多