skywalking icon indicating copy to clipboard operation
skywalking copied to clipboard

[Bug] When the sampling rate is 0 and the code exists across goroutine, ExitSpan has a problem of failure to create. err: span type is wrong

Open Donghui0 opened this issue 1 year ago • 2 comments

Search before asking

  • [X] I had searched in the issues and found no similar issues.

Apache SkyWalking Component

Go Agent (apache/skywalking-go)

What happened

In the following scenario, Gin handler serves as the entry point and calls 2 mongo operations through goroutine.

Gin -- EntrySpan (NewNoopSpan() -> SET_GLS(NoopSpan{stackCount: 1})) | (sync.Group) MongoClient ExitSpan (GET_GLS() -> NoopSpan{stackCount:0} -> noop.enterNoSpan() -> span.End() -> SET_GLS(nil) ) | (Why stackCount is 0 ?) MongoClient ExitSpan (GET_GLS() -> nil)

In the 2nd ExitSpan, ctx.ActiveSpan() returns nil, Then createSpan0() returns NoopSpan. image

The following is debug mode: Gin CreateEntrySpan,ctx: image

The first CreateExitSpan within the goroutine, ctx: image

Second CreateExitSpan inside goroutine, ctx: image

What you expected to happen

In the first ExitSpan, The stackCount of ActiveSpan() obtained by GetTracingContext() should be 1. in the 2nd ExitSpan, getTracingContext() -> ActiveSpan() Should not be nil.

How to reproduce

After changing the sampling rate to 0, run the following test code.

package main

import (
    "fmt"
    _ "github.com/apache/skywalking-go"
    "github.com/gin-gonic/gin"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "golang.org/x/sync/errgroup"
)

type ErrorGroup struct {
    errgroup.Group
}

func main() {
    r := gin.Default()
    r.GET("/mongo_get", mongoGet)
    r.Run()
}

func mongoGet(c *gin.Context) {
    var e ErrorGroup
    var m, _ = mongo.Connect(c, options.Client().ApplyURI("mongodb://127.0.0.1:27017"))
    filter := bson.D{{"name", "test"}}
    e.Go(func() (err error) {
       collection := m.Database("demo").Collection("testCollection")
       _, err = collection.Find(c, filter)
       if err != nil {
          return err
       }
       fmt.Println("first Find Done")
       _, err = collection.Find(c, filter)
       if err != nil {
          return err
       }
       fmt.Println("second Find Done")
       return nil
    })
    err := e.Wait()
    if err != nil {
       fmt.Println(err)
    }
}

Observe the log, you can find that the log will print the following error: cannot create exit span on mongo client: span type is wrong

Anything else

No response

Are you willing to submit a pull request to fix on your own?

  • [ ] Yes I am willing to submit a pull request on my own!

Code of Conduct

Donghui0 avatar Apr 08 '24 12:04 Donghui0

Which version are you using? There was a relative fix in 0.4 release, https://github.com/apache/skywalking-go/pull/161

wu-sheng avatar Apr 08 '24 12:04 wu-sheng

Which version are you using? There was a relative fix in 0.4 release, apache/skywalking-go#161

version 0.4.0

Donghui0 avatar Apr 08 '24 12:04 Donghui0

Which version are you using? There was a relative fix in 0.4 release, apache/skywalking-go#161

version 0.4.0

Please check gmail to discuss the same issue,ty

Qiangel avatar Jun 21 '24 02:06 Qiangel

Recheck 0.5. It should have been fixed.

wu-sheng avatar Jun 21 '24 02:06 wu-sheng