完善通道管理机制 & 增强 otel 记录字段
This commit is contained in:
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user