when implementing use otel tracing like this Gin gin context with tracing not work

and apply to router like "router.POST("/v4/*xxxx", gatewayPostHandler, WithTracer())"

then tried to get a trace I found that gin context does not save trace id and span id it empty so how to reproduce the problem?

handler function Gin gin context with tracing not work trace_id and span_id is empty

GetTraceIds function Gin gin context with tracing not work

Environment

  • go version: 1.22.4
  • gin version (or commit ref): github.com/gin-gonic/gin v1.10.0

Comment From: FarmerChillax

problem locating the hasRequestContext function return False: https://github.com/gin-gonic/gin/blob/9c081de9cdd1948f521d47d170d18cbc2981c33a/context.go#L1240

It seems that the problem is caused by the ContextWithFallback field, but I am not sure what this field does.

Solution:

// ...
app := gin.Default()
app.ContextWithFallback = true
// ...

Comment From: JimChenWYU

router.POST("/v4/*xxxx", gatewayPostHandler, WithTracer())

replace by

router.POST("/v4/*xxxx", WithTracer(), gatewayPostHandler)

can it work ?

Comment From: mgerasimchuk

Faced a similar issue too..

Using tracing middleware instead of a handlers chain will help you

Comment From: FarmerChillax

This issue is same with #4074

Add example in https://github.com/gin-gonic/examples/pull/171, hope this helps for you @mgerasimchuk @pepea23 @JimChenWYU

Comment From: FarmerChillax

It seems that the problem is caused by the ContextWithFallback field, but I am not sure what this field does.

Do I need to open a new issue to track this? @appleboy

Comment From: appleboy

@FarmerChillax Yes. Please open the new issue.