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

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

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

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

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

效果展示

实现原理

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).

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

 

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

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

部署代码调用步骤一中的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。

部署代码调用步骤二中的站点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。

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

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

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

 

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

在本地代码中添加Application Insights SDK

配置连接字符串(字符串中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

查看最终效果图:

 【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

 

相关文章
|
5月前
|
Arthas 人工智能 Java
我们做了比你更懂 Java 的 AI-Agent -- Arthas Agent
Arthas Agent 是基于阿里开源Java诊断工具Arthas的AI智能助手,支持自然语言提问,自动匹配排障技能、生成安全可控命令、循证推进并输出结构化报告,大幅降低线上问题定位门槛。
2176 64
我们做了比你更懂 Java 的 AI-Agent -- Arthas Agent
|
监控 Java Linux
开源流程引擎Camunda
开源流程引擎Camunda
|
XML 分布式计算 资源调度
查看YARN上应用的日志之JobHistory
查看YARN上应用的日志之JobHistory
1237 0
查看YARN上应用的日志之JobHistory
|
运维 监控 Serverless
MCP Server 之旅第 7 站:助力 MCP 打破“黑盒困境”
通过本次升级,FC 函数计算与 OpenTelemetry 的深度融合实现了全链路透明化,覆盖从系统层到业务层的完整追踪,并基于统一的 W3C 协议标准打破数据孤岛,确保跨环境一致性。同时,动态采样策略的引入有效平衡了性能与成本,为可观测性提供经济高效的解决方案。
|
算法 程序员 Python
用伪代码表示算法
在算法设计和编程中,伪代码是一种非常重要的工具。它允许我们以一种既非特定编程语言又足够详细的方式来描述算法。伪代码的目标是提供一个清晰、简洁的算法表示,而不必拘泥于特定的编程语法或规则。本文将探讨伪代码的优势,并提供一个用伪代码表示算法的例子。
1432 1
|
自然语言处理 fastjson Java
记一次细思极恐的FastJson差点引发的大面积故障
作者记录了一次FastJson差点引发的大面积故障的排查过程和解决方案。
|
前端开发 JavaScript 开发者
React craco 详细使用与介绍(类似 Vue 外抛的 vue.config.js)
React craco 详细使用与介绍(类似 Vue 外抛的 vue.config.js)
3027 0
|
关系型数据库 MySQL
mysql查看当前实时连接数最大连接数
mysql查看当前实时连接数最大连接数
3154 0