From 2f0c6e6cb72b508caa1d76b561309e9f9db0ef73 Mon Sep 17 00:00:00 2001 From: luorijun Date: Thu, 17 Sep 2026 13:33:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20trace=20attr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 16 ++++++++++++++++ web/middlewares.go | 19 +++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e5756ae --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "main", + "type": "go", + "request": "launch", + "mode": "debug", + "cwd": "${workspaceFolder}", + "program": "cmd/main", + } + ] +} \ No newline at end of file diff --git a/web/middlewares.go b/web/middlewares.go index f1bd32b..510dbc9 100644 --- a/web/middlewares.go +++ b/web/middlewares.go @@ -25,7 +25,7 @@ func ApplyMiddlewares(app *fiber.App) { EnableStackTrace: true, })) - // metric + // otel app.Use(otelfiber.Middleware()) // logger @@ -45,12 +45,23 @@ func ApplyMiddlewares(app *fiber.App) { return err } - str := "" if err != nil { - str = err.Error() + span.SetAttributes(attribute.String("http.response.error", err.Error())) + } + + authCtx := auth.GetAuthCtx(c) + if authCtx != nil { + if authCtx.User != nil { + span.SetAttributes(attribute.Int("auth.user.id", int(authCtx.User.ID))) + } + if authCtx.Admin != nil { + span.SetAttributes(attribute.Int("auth.admin.id", int(authCtx.Admin.ID))) + } + if authCtx.Client != nil { + span.SetAttributes(attribute.Int("auth.client.id", int(authCtx.Client.ID))) + } } - span.SetAttributes(attribute.String("http.response.error", str)) return err })