elastic / apm-agent-go

https://www.elastic.co/guide/en/apm/agent/go/current/index.html
Apache License 2.0
422 stars 197 forks source link

apm.TransactionFromContext always nil #1637

Closed ga845937 closed 4 months ago

ga845937 commented 4 months ago

Describe the bug I am using a combination of: go.elastic.co/apm v1.15.0 go.elastic.co/apm/module/apmgin/v2 v2.6.0 go.elastic.co/apm/module/apmsql/v2 v2.6.0

apmgin and apmsql work image

To Reproduce Steps to reproduce the behavior:

gin middleware setting

engine := gin.New()
engine.Use(apmgin.Middleware(engine), middleware.Logger())

I try to get TraceID in middleware.Logger(), Elastic Docs module/apmgin say [ For each request, a transaction is stored in the request context, which can be obtained via gin.Context.Request.Context() in your handler. ]

so in middleware.Logger()

var traceID string
tx := apm.TransactionFromContext(context.Request.Context())
if tx != nil {
    traceID = tx.TraceContext().Trace.String()
}

logger.Info( "request",
    zap.String("traceID", traceID),
    .... other field
)

tx always nil, so traceID always ""

Expected behavior get the traceID

axw commented 4 months ago

I am using a combination of: go.elastic.co/apm v1.15.0 go.elastic.co/apm/module/apmgin/v2 v2.6.0 go.elastic.co/apm/module/apmsql/v2 v2.6.0

Using a combination of v1 and v2 is not going to work - you will need to update from apm to apm/v2.