Skip to content

Commit

Permalink
Merge pull request #69 from scribd/laynax/fix-gorm
Browse files Browse the repository at this point in the history
[SERF-498] Fix passing data using context in Gorm
  • Loading branch information
laynax committed Feb 22, 2023
2 parents e1b066b + d490f60 commit 5ac7bc0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/instrumentation/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ import (
"gorm.io/gorm"
)

const (
type spanContextKey string

var (
// ParentSpanGormKey is the name of the parent span key
ParentSpanGormKey = "tracingParentSpan"
parentSpanGormKey = spanContextKey("trancingParentSpan")
// SpanGormKey is the name of the span key
SpanGormKey = "tracingSpan"
spanGormKey = spanContextKey("tracingSpan")
)

// TraceDatabase sets span to gorm settings, returns cloned DB
func TraceDatabase(ctx context.Context, db *gorm.DB) *gorm.DB {
if ctx == nil {
return db
}

parentSpan, _ := ddtrace.SpanFromContext(ctx)
return db.Set(ParentSpanGormKey, parentSpan)

return db.Session(&gorm.Session{
Context: context.WithValue(db.Statement.Context, parentSpanGormKey, parentSpan),
})
}

// InstrumentDatabase adds callbacks for tracing, call TraceDatabase to make it work
Expand Down Expand Up @@ -58,12 +64,15 @@ func (c *callbacks) afterDelete(db *gorm.DB) { c.after(db) }
func (c *callbacks) beforeRow(db *gorm.DB) { c.before(db, "", c.serviceName) }
func (c *callbacks) afterRow(db *gorm.DB) { c.after(db) }
func (c *callbacks) before(db *gorm.DB, operationName string, serviceName string) {
val, ok := db.Get(ParentSpanGormKey)
if db.Statement == nil || db.Statement.Context == nil {
return
}

parentSpan, ok := db.Statement.Context.Value(parentSpanGormKey).(ddtrace.Span)
if !ok {
return
}

parentSpan := val.(ddtrace.Span)
spanOpts := []ddtrace.StartSpanOption{
ddtrace.ChildOf(parentSpan.Context()),
ddtrace.SpanType(ext.SpanTypeSQL),
Expand All @@ -73,16 +82,19 @@ func (c *callbacks) before(db *gorm.DB, operationName string, serviceName string
operationName = strings.Split(db.Statement.SQL.String(), " ")[0]
}
sp := ddtrace.StartSpan(operationName, spanOpts...)
db.Set(SpanGormKey, sp)
db.Statement.Context = context.WithValue(db.Statement.Context, spanGormKey, sp)
}

func (c *callbacks) after(db *gorm.DB) {
val, ok := db.Get(SpanGormKey)
if db.Statement == nil || db.Statement.Context == nil {
return
}

sp, ok := db.Statement.Context.Value(spanGormKey).(ddtrace.Span)
if !ok {
return
}

sp := val.(ddtrace.Span)
sp.SetTag(ext.ResourceName, strings.ToUpper(db.Statement.SQL.String()))
sp.SetTag("db.table", db.Statement.Table)
sp.SetTag("db.query", strings.ToUpper(db.Statement.SQL.String()))
Expand Down
15 changes: 15 additions & 0 deletions pkg/instrumentation/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,18 @@ func TestInstrumentDatabase(t *testing.T) {
}
}
}

func TestTraceDatabase(t *testing.T) {
dbFile := path.Join(t.TempDir(), "test_db")
db, err := gorm.Open(sqlite.Open(dbFile))
if err != nil {
t.Fatalf("Failed to open DB: %s", err)
}

InstrumentDatabase(db, "test_app_name")
db = TraceDatabase(context.Background(), db)

if sp := db.Statement.Context.Value(parentSpanGormKey); sp == nil {
t.Error("Parent span not set on statement")
}
}

0 comments on commit 5ac7bc0

Please sign in to comment.