From 743821c71df0206a2148202ec3c79bbdf529149e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sat, 3 Aug 2024 12:35:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=A6=E6=88=AA=E4=B8=AD=E9=97=B4=E4=BB=B6?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E8=AF=B7=E6=B1=82=E5=92=8C=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E4=BD=93=E6=97=B6=E7=9A=84=E5=BC=82=E5=B8=B8=EF=BC=8C=E5=9F=8B?= =?UTF-8?q?=E7=82=B9=E5=8A=A8=E4=BD=9C=E4=B8=8D=E5=BA=94=E8=AF=A5=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E6=AD=A3=E5=B8=B8=E4=B8=9A=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stardust.Extensions/TracerMiddleware.cs | 41 +++++++++++++++++-------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/Stardust.Extensions/TracerMiddleware.cs b/Stardust.Extensions/TracerMiddleware.cs index 44e5ac65..bfda61b4 100644 --- a/Stardust.Extensions/TracerMiddleware.cs +++ b/Stardust.Extensions/TracerMiddleware.cs @@ -31,9 +31,10 @@ public async Task Invoke(HttpContext ctx) { //!! 以下代码不能封装为独立方法,因为有异步存在,代码被拆分为状态机,导致这里建立的埋点span无法关联页面接口内的下级埋点 ISpan? span = null; + var action = ""; if (Tracer != null && !ctx.WebSockets.IsWebSocketRequest) { - var action = GetAction(ctx); + action = GetAction(ctx); if (!action.IsNullOrEmpty()) { // 请求主体作为强制采样的数据标签,便于分析链路 @@ -51,13 +52,20 @@ public async Task Invoke(HttpContext ctx) req.ContentType != null && req.ContentType.StartsWithIgnoreCase(TagTypes)) { - req.EnableBuffering(); + try + { + req.EnableBuffering(); - var buf = new Byte[1024]; - var count = await req.Body.ReadAsync(buf, 0, buf.Length); - span.AppendTag("\r\n<=\r\n" + buf.ToStr(null, 0, count)); - req.Body.Position = 0; - flag = true; + var buf = new Byte[1024]; + var count = await req.Body.ReadAsync(buf, 0, buf.Length); + span.AppendTag("\r\n<=\r\n" + buf.ToStr(null, 0, count)); + req.Body.Position = 0; + flag = true; + } + catch (Exception ex) + { + XTrace.Log.Error("[{0}]读取请求主体失败:{1}", action, ex.Message); + } } if (span.Tag.Length < 500) @@ -102,12 +110,19 @@ public async Task Invoke(HttpContext ctx) res.ContentType != null && res.ContentType.StartsWithIgnoreCase(TagTypes)) { - var buf = new Byte[1024]; - var p = res.Body.Position; - var count = await res.Body.ReadAsync(buf, 0, buf.Length); - span.AppendTag("\r\n=>\r\n" + buf.ToStr(null, 0, count)); - res.Body.Position = p; - flag = true; + try + { + var buf = new Byte[1024]; + var p = res.Body.Position; + var count = await res.Body.ReadAsync(buf, 0, buf.Length); + span.AppendTag("\r\n=>\r\n" + buf.ToStr(null, 0, count)); + res.Body.Position = p; + flag = true; + } + catch (Exception ex) + { + XTrace.Log.Error("[{0}]读取响应主体失败:{1}", action, ex.Message); + } } if (span.Tag == null || span.Tag.Length < 500)