contrib icon indicating copy to clipboard operation
contrib copied to clipboard

🤗 [Question]: Otelfiber - How to set span status to Error on panic?

Open matteobassan opened this issue 1 year ago • 0 comments

Question Description

I've been using otel-fiber for a while, and I've noticed that by editing the fiber.go (around line 123) file in this way, I can set the main span status to error:

if err := c.Next(); err != nil {
	span.RecordError(err)
	span.SetStatus(codes.Error, err.Message) //<-- added this line
			
	// invokes the registered HTTP error handler
	// to get the correct response status code
	_ = c.App().Config().ErrorHandler(c, err)
}

Now, this solution works every time there is an error returned, but clearly not when there is a panic inside the handler function or one of the sub-functions, since no error is returned from the handler. I checked with breaks in debug mode, and the program never enters the if statemen, so the span is not set to Error. What sounds strange is that the error is still recorded and logged as "event" in the span, and i can find it in the Uptrace application, with all the exception-related info, even if span.RecordError(err) is (apparently) never called.

Can anyone explain this behavior? I'm sure I'm missing something.

Is worth noting that i am using the recover middleware, in this order:

router.Use(logger.New())
router.Use(recover.New())
router.Use(cors.New())
router.Use(otelfiber.Middleware())

Thanks

Code Snippet (optional)

No response

Checklist:

  • [X] I agree to follow Fiber's Code of Conduct.
  • [X] I have checked for existing issues that describe my questions prior to opening this one.
  • [X] I understand that improperly formatted questions may be closed without explanation.

matteobassan avatar Oct 08 '23 08:10 matteobassan