Skip to content

Commit

Permalink
fix: the problem of missing spans while using the Row function in GOR…
Browse files Browse the repository at this point in the history
…M has been resolved (#998)
  • Loading branch information
nithinputhenveettil authored Jan 13, 2025
1 parent 0608262 commit 91a6a53
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions instrumentation/instagorm/instagorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func (wdB *wrappedDB) registerQueryCallbacks() {
}

func (wdB *wrappedDB) registerRowCallbacks() {
wdB.logError(wdB.Callback().Raw().Before("gorm:row").Register("instagorm:before_row",
wdB.logError(wdB.Callback().Row().Before("gorm:row").Register("instagorm:before_row",
preOpCb(wdB)))

wdB.logError(wdB.Callback().Raw().After("gorm:row").Register("instagorm:after_row",
wdB.logError(wdB.Callback().Row().After("gorm:row").Register("instagorm:after_row",
postOpCb()))
}

Expand Down
67 changes: 65 additions & 2 deletions instrumentation/instagorm/instagorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
SELECT = "SELECT * FROM `products` WHERE code = ? AND `products`.`deleted_at` IS NULL ORDER BY `products`.`id` LIMIT 1"
RAWSQL = "SELECT * FROM products"
DB_TYPE = "sqlite"
ROW = "SELECT code,price FROM `product` WHERE code = ?"
)

type product struct {
Expand Down Expand Up @@ -329,7 +330,6 @@ func TestRawSQL(t *testing.T) {
assert.EqualValues(t, instana.ExitSpanKind, rawSQLSpan.Kind)
require.IsType(t, instana.SDKSpanData{}, rawSQLSpan.Data)

RawSqlOutput := "SELECT * FROM `products` WHERE code = ? AND `products`.`deleted_at` IS NULL ORDER BY `products`.`id` LIMIT 1"
data := rawSQLSpan.Data.(instana.SDKSpanData)
assert.Equal(t, instana.SDKSpanTags{
Name: "sdk.database",
Expand All @@ -339,7 +339,70 @@ func TestRawSQL(t *testing.T) {
"tags": ot.Tags{
"span.kind": ext.SpanKindRPCClientEnum,
"db.instance": dsn,
"db.statement": RawSqlOutput,
"db.statement": RAWSQL,
"db.type": DB_TYPE,
"peer.address": dsn,
},
},
}, data.Tags)

})
}

func TestRow(t *testing.T) {
recorder := instana.NewTestRecorder()
s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{
Service: "go-sensor-test",
AgentClient: alwaysReadyClient{},
}, recorder))
defer instana.ShutdownSensor()

pSpan := s.Tracer().StartSpan("parent-span")
ctx := context.Background()
if pSpan != nil {
ctx = instana.ContextWithSpan(ctx, pSpan)
}

t.Run("Exec", func(t *testing.T) {
dsn, tearDownFn := setupEnv(t)
defer tearDownFn(t)

db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}

db.Statement.Context = ctx
instagorm.Instrument(db, s, dsn)

if err = db.AutoMigrate(&product{}); err != nil {
panic("failed to migrate the schema")
}
require.NoError(t, err)

db.Create(&product{Code: "D42", Price: 100})

var p product
rw := db.Table("product").Where("code = ?", "D42").Select("code", "price").Row()
rw.Scan(&p)

spans := recorder.GetQueuedSpans()

rowSpan := spans[len(spans)-1]
assert.Equal(t, 0, rowSpan.Ec)
assert.EqualValues(t, instana.ExitSpanKind, rowSpan.Kind)
require.IsType(t, instana.SDKSpanData{}, rowSpan.Data)

data := rowSpan.Data.(instana.SDKSpanData)
assert.Equal(t, instana.SDKSpanTags{
Name: "sdk.database",
Type: "exit",
Custom: map[string]interface{}{
"baggage": map[string]string{"dbKey": "db"},
"tags": ot.Tags{
"span.kind": ext.SpanKindRPCClientEnum,
"db.instance": dsn,
"db.statement": ROW,
"db.type": DB_TYPE,
"peer.address": dsn,
},
Expand Down

0 comments on commit 91a6a53

Please sign in to comment.