Skip to content
Open
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
6 changes: 6 additions & 0 deletions contrib/database/sql/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func defaults(cfg *config, driverName string, rc *registerConfig) {
cfg.ignoreQueryTypes = rc.ignoreQueryTypes
cfg.childSpansOnly = rc.childSpansOnly
cfg.dbStats = rc.dbStats
if cfg.tags == nil {
cfg.tags = make(map[string]interface{})
}
for k, v := range rc.tags {
cfg.tags[k] = v
}
Comment on lines +161 to +166
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure how much this matters for Go, but a quick optimization here might be to only initialize cfg.tags to an empty map IF we have rc.tags to copy over. So, if len(rc.tags) > 0.

This depends on whether we expect cfg.tags to already be initialized later on, when we read from it, though.

}
}

Expand Down
31 changes: 31 additions & 0 deletions contrib/database/sql/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
package sql

import (
"context"
"log"
"testing"

"github.com/DataDog/datadog-go/v5/statsd"
"github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"

"github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer"
"github.com/DataDog/dd-trace-go/v2/instrumentation/testutils"
)

Expand Down Expand Up @@ -77,3 +81,30 @@ func TestCheckStatsdRequired(t *testing.T) {
assert.False(t, cfg.dbStats)
})
}

func TestRegisteredTags(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
Register("register-name", &mysql.MySQLDriver{}, WithCustomTag("test", "this-is-a-test"))
defer unregister("register-name")

db, err := Open("register-name", "test:test@tcp(127.0.0.1:3306)/test")
if err != nil {
log.Fatal(err)
}
defer db.Close()
mt.Reset()

// conduct a normal query to the database. This creates 2 spans
rows, err := db.QueryContext(context.Background(), "SELECT 1")
assert.NoError(t, err)
rows.Close()

// all created spans should have the tag from Register
spans := mt.FinishedSpans()
assert.Len(t, spans, 2)
for _, sp := range spans {
assert.Equal(t, "this-is-a-test", sp.Tag("test"))
}

}
Loading