使用適用於 .NET 的 X-Ray 開發套件追蹤下游 HTTP Web 服務的呼叫 - AWS X-Ray

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用適用於 .NET 的 X-Ray 開發套件追蹤下游 HTTP Web 服務的呼叫

注意

End-of-support通知 – 在 2027 年 2 月 25 日, AWS X-Ray 將停止支援 AWS X-Ray SDKs 和協助程式。2027 年 2 月 25 日之後,您將不再收到更新或版本。如需支援時間表的詳細資訊,請參閱 X-Ray SDK 和協助程式終止支援時間表。建議您遷移至 OpenTelemetry。如需遷移至 OpenTelemetry 的詳細資訊,請參閱從 X-Ray 檢測遷移至 OpenTelemetry 檢測

當您的應用程式呼叫微服務或公有 HTTP APIs 時,您可以使用適用於 .NET 的 X-Ray 開發套件GetResponseTraced延伸方法來System.Net.HttpWebRequest檢測這些呼叫,並將 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 }