【应用程序见解 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

 

版权声明:本文版权归作者所有,遵循 CC 4.0 BY-SA 许可协议, 转载请注明原文链接
https://www.cnblogs.com/lulight/p/13938752.html

【api管理 apim】如何查看apim中的request与response详细信息,如header,body中的参数内容_路边两盏灯-编程思维

问题描述 通过APIM门户或者是Developer门户,我们可以通过Test功能测试某一个接口,通过Trace可以获取非常详细的Request,Response的信息,包含Header,X-Forward-To,及Respnse的信息。但是,当我们正式使用时,确无法得到这些信息。APIM门户及日志中都没有保存请求中携带

【应用程序见解 application insights】使用azure monitor application insights agent获取azure vm中监控数据及iis请求指标等信息_路边两盏灯-编程思维

问题情形 为了使用Application Insights也可以监控Azure VM中的相关性能数据,如CPU, Memory,IIS Reuqest等信息,可以在VM中开始一个一个扩展插件: Azure Monitor Application Insights Agent   在Azure Application I

【应用程序见解 application insights】在application insights中通过自定义查询结果定义指标并显示在dashboard中_路边两盏灯-编程思维

问题情形 通过Application Insights收集到指标数据后,如Request,Trace,Exception。但是默认的Insights图表不能满足业务的需求,需要自定义相应的类SQL语句并制作图表以便直观的显示,避免每次都需要重新查询数据并转换为图表。 类SQL的查询语句在Applicaiton Insi

【azure application insights】在azure function中启用application insights后,如何配置不输出某些日志到ai 的trace中_路边两盏灯-编程思维

问题描述 基于.NET Core的Function App如果配置了Application Insights之后,每有一个函数被执行,则在Application Insights中的Logs中的trace里都可以查询到函数的执行启动,执行结束的信息。类似如下的日志, 函数执行开始 Executing Functi

【azure 应用程序见解】在docker中运行的asp.net core应用如何开启application insights的profiler trace呢? _路边两盏灯-编程思维

问题描述 使用Azure Application Insights收集AKS中ASP.NET Core应用的监控数据,自动收集请求的Trace情况,用于分析单个请求在应用内部的耗时及处理事件情况,参考Application Insights的文档,功能“Profiler Trace”可以追踪请求详情,但是在中国区的Ap