Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib/gofiber/fiber.v2: add reference to initial middleware span/context #992

Merged
merged 11 commits into from
Aug 31, 2021
5 changes: 4 additions & 1 deletion contrib/gofiber/fiber.v2/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ func Middleware(opts ...Option) func(c *fiber.Ctx) error {
}

opts = append(opts, cfg.spanOpts...)
span, _ := tracer.StartSpanFromContext(c.Context(), "http.request", opts...)
span, ctx := tracer.StartSpanFromContext(c.Context(), "http.request", opts...)

defer span.Finish()

// pass the span through the request UserContext
c.SetUserContext(ctx)

resourceName := c.Path()
if resourceName == "" {
resourceName = "unknown"
Expand Down
28 changes: 28 additions & 0 deletions contrib/gofiber/fiber.v2/fiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,34 @@ func TestCustomError(t *testing.T) {
assert.Equal(fiber.ErrBadRequest, span.Tag(ext.Error).(*fiber.Error))
}

func TestUserContext(t *testing.T) {
assert := assert.New(t)
mt := mocktracer.Start()
defer mt.Stop()

// setup
router := fiber.New()
router.Use(Middleware(WithServiceName("foobar")))

router.Get("/", func(c *fiber.Ctx) error {
// check if not default empty context
assert.NotEmpty(c.UserContext())
span, _ := tracer.StartSpanFromContext(c.UserContext(), "http.request")
defer span.Finish()
return c.SendString("test")
})
r := httptest.NewRequest("GET", "/", nil)

router.Test(r, 100)

// verify both middleware span and router span finished
spans := mt.FinishedSpans()
assert.Len(spans, 2)
if len(spans) < 2 {
t.Fatalf("no spans")
}
}
knusbaum marked this conversation as resolved.
Show resolved Hide resolved
knusbaum marked this conversation as resolved.
Show resolved Hide resolved

func TestGetSpanNotInstrumented(t *testing.T) {
assert := assert.New(t)
router := fiber.New()
Expand Down