i have written a Golang app using GIN middleware with apm enabled and facing a problem which is random in nature.
for enabling the APM and elastic search monitoring i have used the middleware like below
func NewRouter() *gin.Engine {
gin.SetMode(gin.DebugMode)
router := gin.New()
router.Use(middlewares.Recover())
router.Use(apmgin.Middleware(router))
router.Use(middlewares.Logger(), gin.Logger())
//API Route Group
v2 := router.Group("/api")
//V1 Route
v2Routes(v2)
return router
}
uptill now , logging is going on fine on elastic search with all necessary parameters.
but now i want to send by POST request data also, which is not getting logged currently by default
now inside my main handler function of the API , I am using a custom function to log my post request data to APM
func SendPostDataToAPM(ctx context.Context, data interface{}) {
defer RoutineRecovery()
tx := apm.TransactionFromContext(ctx)
tx.Context.SetCustom("postData", data)
Now the main problem that i am facing is that , the above function SendPostDataToAPM is giving me panic randomly, means it is working fine for some time and then randomly throws an panic, and again works fine for the next request
panic is coming specifically from the last line tx.Context.SetCustom("postData", data)
panic: runtime error: invalid memory address or nil pointer dereference
if anyone knows any workaround around this, kindly help
Updates
I know that there is some nil problem here so I have implemented a nil check before calling the SetCustom function, the solution worked , but still there were rare cases(like only 1 or 2 times in a day) , when even that too didn't work.
So I am trying to find a solution to this random behaviour, why this is panic is coming randomly.