func()

in module/apmfiber/middleware.go [60:119]


func (m *middleware) handle(c *fiber.Ctx) (result error) {
	reqCtx := c.Context()
	if !m.tracer.Recording() || m.requestIgnorer(reqCtx) {
		return c.Next()
	}

	name := string(reqCtx.Method()) + " " + c.Path()
	tx, body, err := apmfasthttp.StartTransactionWithBody(reqCtx, m.tracer, name)
	if err != nil {
		reqCtx.Error(err.Error(), http.StatusInternalServerError)

		return err
	}

	defer func() {
		resp := c.Response()
		route := c.Route()

		var fiberErr *fiber.Error
		if route.Path == "/" && errors.As(result, &fiberErr) && fiberErr.Code == http.StatusNotFound {
			tx.Name = string(reqCtx.Method()) + " unknown route"
		} else {
			// Workaround for set tx.Name as template path, not absolute
			tx.Name = string(reqCtx.Method()) + " " + route.Path
		}

		if v := recover(); v != nil {
			if m.panicPropagation {
				defer panic(v)
			}

			e := m.tracer.Recovered(v)
			e.SetTransaction(tx)
			setContext(&e.Context, resp)
			e.Send()

			c.Status(http.StatusInternalServerError)
		}

		statusCode := resp.StatusCode()

		tx.Result = apmhttp.StatusCodeResult(statusCode)
		if tx.Sampled() {
			setContext(&tx.Context, resp)
		}

		body.Discard()
	}()

	result = c.Next()
	if result != nil {
		resp := c.Response()
		e := m.tracer.NewError(result)
		e.Handled = true
		e.SetTransaction(tx)
		setContext(&e.Context, resp)
		e.Send()
	}
	return result
}