Using otelgin.Middleware will cause temporary MultipartForm files not to be destroyed. #5946
Open
Description
Description
Using otelgin.Middleware will cause temporary MultipartForm files not to be destroyed.
Environment
- OS: MacOS
- Architecture: x86
- Go Version: 1.22
otelgin
version: 0.53.0
Steps To Reproduce
- Using this code start up gin http server
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)
func main() {
engine := gin.Default()
engine.MaxMultipartMemory = 1024 * 1024
engine.Use(otelgin.Middleware("my-server"))
engine.POST("/upload", func(c *gin.Context) {
ff, err := c.FormFile("file")
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
_ = ff
c.JSON(http.StatusOK, map[string]interface{}{"c": 0})
})
engine.Run(":8624")
}
- Upload a file to the http api
curl --request POST \
--url http://127.0.0.1:8624/upload \
--header 'Content-Type: multipart/form-data' \
--header 'User-Agent: insomnia/9.3.2' \
--form '[email protected]'
Expected behavior
Upload a file larger than MaxMultipartMemory, and the HTTP server will generate multipart-xxxx temporary files in the system temporary directory. These files will be automatically destroyed when the request ends. However, if the otelgin.Middleware is used, the temporary files will not be destroyed.
the HTTP server holds the original request, and using otelgin.Middleware replaces the request with c.request = c.request.WithContext(ctx), causing subsequent c.FormFile reads to fall on the new request. As a result, the original request held by the HTTP server does not have the MultipartForm, preventing it from being destroyed.