Applicaotn  Insigths 使用 Application Maps 构建请求链路视图 

构建系统时,请求的逻辑操作大多数情况下都需要在不同的服务,或接口中完成整个请求链路。一个请求可以经历多个组件,极有可能出现客户端请求站点1,站点1请求站点2, … 站点N才是最终处理数据然后依次返回。

在这样的情况,如果有一个直观的视图来展示请求在每一个站点上的状态(成功失败),当问题发生时,非常有帮助定位问题发生的地点。借助Azure 应用程序见解(Application Insights)中的遥测关联建立的Application Maps就能实现

效果展示

【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

实现原理

Application Insights定义了用于分配遥测关联的数据模型,每个传出操作(例如,对另一个组件的 HTTP 调用)是由依赖项遥测表示的。 依赖项遥测也定义了自身的全局独一无二的 id,此依赖项调用发起的请求遥测将此 id 用作其 operation_parentId。通过operation_Id、operation_parentId 和 request.id,即可以生成分布式逻辑操作的视图 (这些字段也定义了遥测调用的因果关系顺序)。

实例构建Application Maps

在实例中,这一个请求进行了4段转发。

第一段:本地代码访问APIM(test01.azure-api.cn),

第二段:APIM访问站点1(lbphptest.chinacloudsites.cn)

第三段:站点1访问站点2(lbjavatest.chinacloudsites.cn)

第四段:站点2访问最终处理请求的Azure Function(Http Trigger)( functionapp120201013155425.chinacloudsites.cn).

准备条件

  • 创建Application Insights (lbphptest202011050549)
  • 创建APIM(test01)
  • 创建两个App Service (lbphptest 和lbjavatest)
  • 创建一个Azure Function(functionapp120201013155425)

:如不熟悉如何创建以上资源,可以导航到文末的参考资料部分

步骤一:在Azure Funciton中启用并关联Application Insights服务

进入Azure Funciton门户,选择Application Insights功能,根据页面提示选择已创建好的Application Insights (lbphptest202011050549).

【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

创建的Function为默认的HttpTrigger模式,测试目的,代码可以不需任何修改。参考下图获取到Function的URL,用于下一步在站点2中调用。

【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

 

步骤二:在站点2(lbjavatest)中启用并关联Application Insights服务,并部署代码请求Azure Function

进入站点2的门户页面,在Application Insights目录中根据提示Enable Application Insights并选择相同的Application Insights。

【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

部署代码调用步骤一中的Azure Function

        //Level 3
        [HttpGet]
        [Route("[Action]")]
        public string Fun([FromQuery] string name)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                var url = $"https://functionapp120201013155425.chinacloudsites.cn/api/HttpTrigger1?name={name}";
                HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
                httpRequest.Headers.Add("Accept", "application/json, text/plain, */*");

                var response = httpClient.SendAsync(httpRequest).Result;
                string responseContent = response.Content.ReadAsStringAsync().Result;

                return responseContent;
            }
        }

 

步骤三:在站点1(lbphptest)中启用并关联Application Insights服务,并部署代码请求站点2

进入站点1的门户页面,在Application Insights目录中根据提示Enable Application Insights并选择相同的Application Insights。

 【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

部署代码调用步骤二中的站点2的URL,代码与访问Azure Function相似。

        //Level 2
        [HttpGet]
        [Route("[Action]")]
        public string FunSub([FromQuery] string name)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                var url = $"https://lbjavatest.chinacloudsites.cn/WeatherForecast/fun?name={name}";
                HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
                httpRequest.Headers.Add("Accept", "application/json, text/plain, */*");

                var response = httpClient.SendAsync(httpRequest).Result;
                string responseContent = response.Content.ReadAsStringAsync().Result;

                return responseContent;
            }
        }

 

步骤四:在APIM中启用并关联Application Insights服务, 并设置API访问站点1

进入APIM的门户页面,在Application Insights目录中添加相同的Application Insights。

 【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

在APIM配置API访问站点1(lbphptest)

  • 点击“Add API” 按钮
  • 选择从App Service中创建
  • 选择站点1(lbphptest)

 【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

在接口中添加操作一个新操作,访问站点1中的接口/weatherforecast/funsub?name={name}

 【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

 

步骤五:在ASP.NET Core代码中添加Application Insights SDK并配置连接字符串,在接口中访问APIM.

在本地代码中添加Application Insights SDK

【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

配置连接字符串(字符串中Application Insights的Overview页面复制)

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=xxx-xxx-xxx-xxx-xxxxx;EndpointSuffix=applicationinsights.azure.cn;IngestionEndpoint=https://chinaeast2-0.in.applicationinsights.azure.cn/"
  },
  "AllowedHosts": "*"
}

启动本地程序,并通过在浏览器中访问 https://localhost:44323/weatherforecast/Funsubfornt?name=test from local -- apim -- app 1 – app 2 -- function

查看最终效果图:

 【应用程序见解 Application Insights】Application  Insights 使用 Application Maps 构建请求链路视图-LMLPHP

  【END】

参考文档:

在 Azure 门户中创建第一个函数https://docs.azure.cn/zh-cn/azure-functions/functions-create-first-azure-function

适用于 ASP.NET Core 应用程序的 Application Insightshttps://docs.azure.cn/zh-cn/azure-monitor/app/asp-net-core

关于 API 管理https://docs.azure.cn/zh-cn/api-management/api-management-key-concepts

在 Azure 中创建 ASP.NET Core Web 应用https://docs.azure.cn/zh-cn/app-service/quickstart-dotnetcore?pivots=platform-linux

11-07 12:12