Search code examples
goelastic-apmkrakend

How To Forward Elastic APM to Golang Plugin [KrakenD]


I implemented the Elastic APM Go Agent on Lura's gin router. Lura is a Framework used by KrakenD. I use it on gin middleware:

endpointGroup.Use(apmgin.Middleware(r.cfg.Engine))

The problem is, that the instrumentation only traces the HTTP request. It looks like this on Kibana:

enter image description here

I read that APM Go Agent doesn't support the instrumentation. So I want to add a manual APM span on several transactions like cache operation, etc. The problem is I want to add the APM span on the Go plugin, and it doesn't send any data to my APM server. I guess that the request lifecycle is not passed through the go plugin. I've tried to create a new APM instance and pass the APM context to go plugin but it doesn't work. How do I achieve it?


Solution

  • Just found out where to continue the Gin http request context from Lura to Krakend. If you use Lura v1.2.0. It is on the endpoint handler router/gin/endpoint.go on CustomErrorEndpointHandler function:

    return func(c *gin.Context) {
                // requestCtx, cancel := context.WithTimeout(c, configuration.Timeout)
                requestCtx := c.Request.Context() // <- change to this
    
                c.Header(core.KrakendHeaderName, core.KrakendHeaderValue)
    
                response, err := prxy(requestCtx, requestGenerator(c, configuration.QueryString))
    
                ...
    
    }