problem with new traces being created on a fiber using ddtrace

50 Views Asked by At

I have a problem with new traces being created on a fiber using ddtrace. please help

I tried using ddtrace debug mode, but it keeps generating new trace ids.

ddtrace debug logs

The logs show that a trace is created when the request arrives, and a new trace is created by the subsequent call to StartSpanFromContext. I don't want a new trace to be created, I want it to be created from an existing trace. My code looks like this.

1. main.go
func main() {
    config, err := core.Settings()
    if err != nil {
       panic(err)
    }
    tracer.Start(
       tracer.WithDebugMode(true),
    )
    defer tracer.Stop()

    app := fiber.New(fiber.Config{
       Prefork:       false,
       CaseSensitive: true,
       StrictRouting: true,
       ServerHeader:  "unboxers",
       AppName:       "Deco My Tree Backend",
    })
    app.Use(fibertrace.Middleware(fibertrace.WithServiceName("deco-my-tree")))
    routes.SetupRoutes(app, db, readDB, redisClient, config)
    .....

2. routes.go

func SetupRoutes(app *fiber.App, db *gorm.DB, readDB *gorm.DB, redisClient *redis.Client, config *core.Config) {
    v1 := app.Group("/api/v1")
    v1User := v1.Group("/user")
    v1User.Post("/login", UserLoginHandler(db, config))
        ......

func UserLoginHandler(readDB *gorm.DB, config *core.Config) fiber.Handler {
    return func(c *fiber.Ctx) error {
        span, ctx := tracer.StartSpanFromContext(c.Context(), "UserLoginHandler")
        defer span.Finish()
        req := new(models.UserLoginRequest)
        if err := c.BodyParser(req); err != nil {
            return err
        }
        var user models.User
        result := readDB.WithContext(ctx).Table("user").Where("email = ?", req.Email).Find(&user)
                 ......
  1. move fibertrace's middleware registration to the top.
  2. I also tried readDB.WithContext(c) without using StartSpanFromContext and using the existing c *fiber.Ctx, but in this case, no log is left in the first place.
1

There are 1 best solutions below

0
On

I found the answer on my own, I just need to use UserContext() instead of Context(). I'll leave a post just in case~.