Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions contrib/gorm.io/gorm.v1/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ func Open(dialector gorm.Dialector, cfg *gorm.Config, opts ...Option) (*gorm.DB,
}

func withCallbacks(db *gorm.DB, opts ...Option) (*gorm.DB, error) {
cfg := new(config)
defaults(cfg)
cfg := newConfigWithDefaults()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the defaults function was only ever used here with a new config, so I thought that this would be an improvement (so that I didn't need do duplicate two lines in my new unit test)

for _, fn := range opts {
fn.apply(cfg)
}
Expand Down Expand Up @@ -151,12 +150,6 @@ func before(db *gorm.DB, operationName string, cfg *config) {
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
for key, tagFn := range cfg.tagFns {
if tagFn != nil {
opts = append(opts, tracer.Tag(key, tagFn(db)))
}
}

_, ctx := tracer.StartSpanFromContext(db.Statement.Context, operationName, opts...)
db.Statement.Context = ctx
}
Expand All @@ -175,6 +168,15 @@ func after(db *gorm.DB, cfg *config) {
dbErr = db.Error
}
span.SetTag(ext.ResourceName, db.Statement.SQL.String())

// process tagFns after setting the resource name to allow the resource
// name to be overridden
for key, tagFn := range cfg.tagFns {
if tagFn != nil {
span.SetTag(key, tagFn(db))
}
}

span.Finish(tracer.WithError(dbErr))
}
}
30 changes: 30 additions & 0 deletions contrib/gorm.io/gorm.v1/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"log"
"os"
"strings"
"testing"

sqltrace "github.com/DataDog/dd-trace-go/contrib/database/sql/v2"
Expand Down Expand Up @@ -610,3 +611,32 @@ func TestPlugin(t *testing.T) {
assert.NotNil(t, db.Callback().Raw().Get("dd-trace-go:before_raw_query"))
assert.NotNil(t, db.Callback().Raw().Get("dd-trace-go:before_raw_query"))
}

func newStubDB() *gorm.DB {
sqlbuilder := strings.Builder{}
sqlbuilder.WriteString("SELECT * FROM products WHERE id = ?")
statement := &gorm.Statement{SQL: sqlbuilder, Context: context.Background()}
config := &gorm.Config{DryRun: false}
return &gorm.DB{Statement: statement, Config: config}
}

func TestCustomResourceName(t *testing.T) {
// This test is meant to ensure that the resource name can be overridden by a custom tag.
assert := assert.New(t)
mt := mocktracer.Start()
defer mt.Stop()

cfg := newConfigWithDefaults()
customResourceName := "custom resource name"
WithCustomTag(ext.ResourceName, func(db *gorm.DB) interface{} {
return customResourceName
})(cfg)

db := newStubDB()
before(db, "gorm.query", cfg)
after(db, cfg)

spans := mt.FinishedSpans()
assert.True(len(spans) > 0)
assert.Equal(customResourceName, spans[len(spans)-1].Tag(ext.ResourceName))
}
4 changes: 3 additions & 1 deletion contrib/gorm.io/gorm.v1/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ func (fn OptionFn) apply(cfg *config) {
fn(cfg)
}

func defaults(cfg *config) {
func newConfigWithDefaults() *config {
cfg := new(config)
cfg.serviceName = "gorm.db"
cfg.analyticsRate = instr.AnalyticsRate(false)
cfg.errCheck = func(error) bool { return true }
cfg.tagFns = make(map[string]func(db *gorm.DB) interface{})
return cfg
}

// WithService sets the given service name when registering a driver,
Expand Down
Loading