diff --git a/contrib/gofiber/fiber.v2/fiber.go b/contrib/gofiber/fiber.v2/fiber.go index 93a454417d..0311539e07 100644 --- a/contrib/gofiber/fiber.v2/fiber.go +++ b/contrib/gofiber/fiber.v2/fiber.go @@ -73,7 +73,7 @@ func Middleware(opts ...Option) func(c *fiber.Ctx) error { tracer.Tag(ext.Component, componentName), tracer.Tag(ext.SpanKind, ext.SpanKindServer), ) - span, ctx := tracer.StartSpanFromContext(c.Context(), cfg.spanName, opts...) + span, ctx := tracer.StartSpanFromContext(c.UserContext(), cfg.spanName, opts...) defer span.Finish() diff --git a/contrib/gofiber/fiber.v2/fiber_test.go b/contrib/gofiber/fiber.v2/fiber_test.go index 6bd0fb786b..ef652ef2c7 100644 --- a/contrib/gofiber/fiber.v2/fiber_test.go +++ b/contrib/gofiber/fiber.v2/fiber_test.go @@ -6,6 +6,7 @@ package fiber import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -178,11 +179,30 @@ func TestUserContext(t *testing.T) { // setup router := fiber.New() + + // define a custom context key + type contextKey string + const fooKey contextKey = "foo" + + // add a middleware that adds a value to the context + router.Use(func(c *fiber.Ctx) error { + ctx := context.WithValue(c.UserContext(), 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 + foo, ok := c.UserContext().Value(fooKey).(string) + assert.True(ok) + assert.Equal(foo, "bar") + span, _ := tracer.StartSpanFromContext(c.UserContext(), "http.request") defer span.Finish() return c.SendString("test")