I wanted to add trace id to logging done for each request to the micro service.I want this in similar as for springboot application we can set trace id in MDC and fetch it and use it while logging.
I have done some research and I found that MDC equivalent in go lang is context. So, I have set the trace id in my context. Now the problem is where ever I have to log with trace id ,I need to pass context to that function which is very ugly way. I am looking for a better solution for this problem.
func HandlerFunction(f gin.HandlerFunc) gin.HandlerFunc{
return func(cxt *gin.Context) {
reqraceId := cxt.Request.Header.Get("trace-id")
requid , _ := uuid.NewRandom()
if reqTraceId == "" {
c.Request.Header.Set("trace-id", requid.String())
}
f(c)
}
}