0

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.

iron_man83
  • 67
  • 2
  • 9
  • TransactionFromContext returns a transaction previously stored in the context using apm.ContextWithTransaction, or nil if the context does not contain a transaction. In your case it seems to be getting nil for some input. To avoid the panic check if the transaction is not nil before you call `tx.Context.SetCustom("postData", data)` – Wishwa Perera Jun 30 '21 at 07:53
  • @WishwaPerera thanks for the reply, i have already done it, but I think that would be somehow hiding the problem, I want to know if there is any solution for it. and how can we stop this random behaviour. – iron_man83 Jun 30 '21 at 07:58
  • please include a code where we can see the construction of the value ctx that is passed to the SendPostDataToAPM function – Wishwa Perera Jun 30 '21 at 08:19
  • @WishwaPerera that is nothing but just the `gin.Context.Request.Context()` , i.e. ctx := gin.Context.Request.Context() , it is constructed manually by me – iron_man83 Jun 30 '21 at 09:23
  • What is the second line of the stack trace in the error message? Which value in the call to `tx.Context.SetCustom` is 0x00? The function is a method and receives two arguments + the 1st one, which is an address of the receiver, and the second one is a string which is two integer values. So, the question is: is the 1st argument 0x00 (the receiver, or the last one (`data`; it's an interface value so it is itself composed of two pointers)? – kostix Jun 30 '21 at 10:10

0 Answers0