使用适用于 .NET 的 X-Ray 开发工具包跟踪对下游 HTTP Web 服务的调用 - Amazon X-Ray
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用适用于 .NET 的 X-Ray 开发工具包跟踪对下游 HTTP Web 服务的调用

在您的应用程序调用微服务或公共 HTTP API 时,您可以使用适用于 .NET 的 X-Ray 开发工具包针对 System.Net.HttpWebRequestGetResponseTraced 扩展方法检测这些调用,并将 API 作为下游服务添加到服务图。

例 HttpWebRequest
using System.Net; using Amazon.XRay.Recorder.Core; using Amazon.XRay.Recorder.Handlers.System.Net; private void MakeHttpRequest() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://names.example.com/api"); request.GetResponseTraced(); }

对于异步调用,请使用 GetAsyncResponseTraced

request.GetAsyncResponseTraced();

如果您使用 system.net.http.httpclient,请使用 HttpClientXRayTracingHandler 委托处理程序来记录调用。

例 HttpClient
using System.Net.Http; using Amazon.XRay.Recorder.Core; using Amazon.XRay.Recorder.Handlers.System.Net; private void MakeHttpRequest() { var httpClient = new HttpClient(new HttpClientXRayTracingHandler(new HttpClientHandler())); httpClient.GetAsync(URL); }

在您检测对下游 Web API 的调用时,适用于 .NET 的 X-Ray 开发工具包会使用有关 HTTP 请求和响应的信息记录子分段。X-Ray 使用子分段为 API 生成推断分段。

例 下游 HTTP 调用的子分段
{ "id": "004f72be19cddc2a", "start_time": 1484786387.131, "end_time": 1484786387.501, "name": "names.example.com", "namespace": "remote", "http": { "request": { "method": "GET", "url": "https://names.example.com/" }, "response": { "content_length": -1, "status": 200 } } }
例 下游 HTTP 调用的推断分段
{ "id": "168416dc2ea97781", "name": "names.example.com", "trace_id": "1-62be1272-1b71c4274f39f122afa64eab", "start_time": 1484786387.131, "end_time": 1484786387.501, "parent_id": "004f72be19cddc2a", "http": { "request": { "method": "GET", "url": "https://names.example.com/" }, "response": { "content_length": -1, "status": 200 } }, "inferred": true }