Skip to content

Commit

Permalink
contrib/gofiber/fiber.v2: fix UserContext test in Middleware for span…
Browse files Browse the repository at this point in the history
… creation

The Middleware was incorrectly using `c.Context()` instead of `c.UserContext()`
to start spans. This could break the propagation of the context defined by other
middlewares earlier in the chain.

This change ensures that `tracer.StartSpanFromContext` uses the correct context
provided by Fiber, preserving context propagation and ensuring proper tracing behavior.

Fixes #2199
  • Loading branch information
tonyduburque committed Jan 15, 2025
1 parent 61cfd11 commit 1fdf56d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions contrib/gofiber/fiber.v2/fiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package fiber

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -178,11 +179,28 @@ func TestUserContext(t *testing.T) {

// setup
router := fiber.New()

// define a custom context key
type contextKey string
const fooKey contextKey = "foo"

router.Use(func(c *fiber.Ctx) error {
ctx := context.WithValue(context.Background(), fooKey, "bar")
c.SetUserContext(ctx)
return c.Next()
})

// add the middleware
router.Use(Middleware(WithServiceName("foobar")))

router.Get("/", func(c *fiber.Ctx) error {
// check if not default empty context
assert.NotEmpty(c.UserContext())

// checks that the user context still has the information provided before using the middleware
_, ok := c.UserContext().Value(fooKey).(string)
assert.True(ok)

span, _ := tracer.StartSpanFromContext(c.UserContext(), "http.request")
defer span.Finish()
return c.SendString("test")
Expand Down

0 comments on commit 1fdf56d

Please sign in to comment.