Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SERF-498] Fix passing data using context in Gorm #69

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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),
Neurostep marked this conversation as resolved.
Show resolved Hide resolved
})
}

// 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)
Neurostep marked this conversation as resolved.
Show resolved Hide resolved
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")
}
}