Skip to content

Commit 7bd189e

Browse files
committed
feat: add GetRequestIDFromContext helper
1 parent c9b4b93 commit 7bd189e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

middleware.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/gofiber/fiber/v2"
1313
"github.com/google/uuid"
14+
"github.com/valyala/fasthttp"
1415
"go.opentelemetry.io/otel/trace"
1516
)
1617

@@ -281,16 +282,22 @@ func NewWithConfig(logger *slog.Logger, config Config) fiber.Handler {
281282
}
282283
}
283284

284-
// GetRequestID returns the request identifier
285+
// GetRequestID returns the request identifier.
285286
func GetRequestID(c *fiber.Ctx) string {
286-
requestID, ok := c.Context().UserValue("request-id").(string)
287+
return GetRequestIDFromContext(c.Context())
288+
}
289+
290+
// GetRequestIDFromContext returns the request identifier from the context.
291+
func GetRequestIDFromContext(ctx *fasthttp.RequestCtx) string {
292+
requestID, ok := ctx.UserValue("request-id").(string)
287293
if !ok {
288294
return ""
289295
}
290296

291297
return requestID
292298
}
293299

300+
// AddCustomAttributes adds custom attributes to the request context.
294301
func AddCustomAttributes(c *fiber.Ctx, attr slog.Attr) {
295302
v := c.Context().UserValue(customAttributesCtxKey)
296303
if v == nil {

0 commit comments

Comments
 (0)