完善通道管理机制 & 增强 otel 记录字段

This commit is contained in:
2026-05-08 17:30:51 +08:00
parent 042c8d1a51
commit 65f8ee360b
12 changed files with 265 additions and 194 deletions

View File

@@ -11,6 +11,8 @@ import (
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/google/uuid"
"github.com/jxskiss/base62"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
func ApplyMiddlewares(app *fiber.App) {
@@ -20,14 +22,6 @@ func ApplyMiddlewares(app *fiber.App) {
EnableStackTrace: true,
}))
// cors
app.Use(cors.New(cors.Config{
AllowCredentials: true,
AllowOriginsFunc: func(origin string) bool {
return true
},
}))
// logger
app.Use(logger.New(logger.Config{
Next: func(c *fiber.Ctx) bool {
@@ -37,6 +31,34 @@ func ApplyMiddlewares(app *fiber.App) {
// metric
app.Use(otelfiber.Middleware())
app.Use(func(c *fiber.Ctx) error {
err := c.Next()
span := trace.SpanFromContext(c.UserContext())
if !span.IsRecording() {
return err
}
status := c.Response().StatusCode()
body := []byte{}
if status < 200 || status >= 300 {
body = c.Response().Body()
if len(body) > 1024 {
body = body[:1024]
}
}
span.SetAttributes(attribute.String("http.response.error", string(body)))
return err
})
// cors
app.Use(cors.New(cors.Config{
AllowCredentials: true,
AllowOriginsFunc: func(origin string) bool {
return true
},
}))
// request id
app.Use(requestid.New(requestid.Config{