Skip to content

Commit 2a58e9f

Browse files
will-bemrarguelloF
andauthored
fix(contrib/gorm.io): allow resource name to be overridden by custom tags (#3909)
Co-authored-by: rodrigo.arguello <[email protected]>
1 parent 0cbdb0c commit 2a58e9f

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

contrib/gorm.io/gorm.v1/gorm.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ func Open(dialector gorm.Dialector, cfg *gorm.Config, opts ...Option) (*gorm.DB,
6565
}
6666

6767
func withCallbacks(db *gorm.DB, opts ...Option) (*gorm.DB, error) {
68-
cfg := new(config)
69-
defaults(cfg)
68+
cfg := newConfigWithDefaults()
7069
for _, fn := range opts {
7170
fn.apply(cfg)
7271
}
@@ -151,12 +150,6 @@ func before(db *gorm.DB, operationName string, cfg *config) {
151150
if !math.IsNaN(cfg.analyticsRate) {
152151
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
153152
}
154-
for key, tagFn := range cfg.tagFns {
155-
if tagFn != nil {
156-
opts = append(opts, tracer.Tag(key, tagFn(db)))
157-
}
158-
}
159-
160153
_, ctx := tracer.StartSpanFromContext(db.Statement.Context, operationName, opts...)
161154
db.Statement.Context = ctx
162155
}
@@ -175,6 +168,15 @@ func after(db *gorm.DB, cfg *config) {
175168
dbErr = db.Error
176169
}
177170
span.SetTag(ext.ResourceName, db.Statement.SQL.String())
171+
172+
// process tagFns after setting the resource name to allow the resource
173+
// name to be overridden
174+
for key, tagFn := range cfg.tagFns {
175+
if tagFn != nil {
176+
span.SetTag(key, tagFn(db))
177+
}
178+
}
179+
178180
span.Finish(tracer.WithError(dbErr))
179181
}
180182
}

contrib/gorm.io/gorm.v1/gorm_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"log"
1212
"os"
13+
"strings"
1314
"testing"
1415

1516
sqltrace "github.com/DataDog/dd-trace-go/contrib/database/sql/v2"
@@ -610,3 +611,32 @@ func TestPlugin(t *testing.T) {
610611
assert.NotNil(t, db.Callback().Raw().Get("dd-trace-go:before_raw_query"))
611612
assert.NotNil(t, db.Callback().Raw().Get("dd-trace-go:before_raw_query"))
612613
}
614+
615+
func newStubDB() *gorm.DB {
616+
sqlbuilder := strings.Builder{}
617+
sqlbuilder.WriteString("SELECT * FROM products WHERE id = ?")
618+
statement := &gorm.Statement{SQL: sqlbuilder, Context: context.Background()}
619+
config := &gorm.Config{DryRun: false}
620+
return &gorm.DB{Statement: statement, Config: config}
621+
}
622+
623+
func TestCustomResourceName(t *testing.T) {
624+
// This test is meant to ensure that the resource name can be overridden by a custom tag.
625+
assert := assert.New(t)
626+
mt := mocktracer.Start()
627+
defer mt.Stop()
628+
629+
cfg := newConfigWithDefaults()
630+
customResourceName := "custom resource name"
631+
WithCustomTag(ext.ResourceName, func(db *gorm.DB) interface{} {
632+
return customResourceName
633+
})(cfg)
634+
635+
db := newStubDB()
636+
before(db, "gorm.query", cfg)
637+
after(db, cfg)
638+
639+
spans := mt.FinishedSpans()
640+
assert.True(len(spans) > 0)
641+
assert.Equal(customResourceName, spans[len(spans)-1].Tag(ext.ResourceName))
642+
}

contrib/gorm.io/gorm.v1/option.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ func (fn OptionFn) apply(cfg *config) {
3131
fn(cfg)
3232
}
3333

34-
func defaults(cfg *config) {
34+
func newConfigWithDefaults() *config {
35+
cfg := new(config)
3536
cfg.serviceName = "gorm.db"
3637
cfg.analyticsRate = instr.AnalyticsRate(false)
3738
cfg.errCheck = func(error) bool { return true }
3839
cfg.tagFns = make(map[string]func(db *gorm.DB) interface{})
40+
return cfg
3941
}
4042

4143
// WithService sets the given service name when registering a driver,

0 commit comments

Comments
 (0)