Skip to content

Commit 4585046

Browse files
authored
Merge pull request #180 from google/go/db/driverName
database type in driver tag
2 parents 1bffcae + 75f6487 commit 4585046

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

go/database/sql/go-sql.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import (
2525

2626
type DB struct {
2727
*sql.DB
28-
options core.CommenterOptions
28+
driverName string
29+
options core.CommenterOptions
2930
}
3031

3132
func Open(driverName string, dataSourceName string, options core.CommenterOptions) (*DB, error) {
3233
db, err := sql.Open(driverName, dataSourceName)
33-
return &DB{DB: db, options: options}, err
34+
return &DB{DB: db, driverName: driverName, options: options}, err
3435
}
3536

3637
// ***** Query Functions *****
@@ -79,7 +80,7 @@ func (db *DB) withComment(ctx context.Context, query string) string {
7980
// `driver` information should not be coming from framework.
8081
// So, explicitly adding that here.
8182
if db.options.EnableDBDriver {
82-
commentsMap[core.Driver] = "database/sql"
83+
commentsMap[core.Driver] = fmt.Sprintf("database/sql:%s", db.driverName)
8384
}
8485

8586
if db.options.EnableFramework && (ctx.Value(core.Framework) != nil) {

go/database/sql/go-sql_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestDisabled(t *testing.T) {
3232
if err != nil {
3333
t.Fatalf("MockSQL failed with unexpected error: %s", err)
3434
}
35-
db := DB{DB: mockDB, options: core.CommenterOptions{}}
35+
db := DB{DB: mockDB, driverName: "mocksql", options: core.CommenterOptions{}}
3636
query := "SELECT 2"
3737
if got, want := db.withComment(context.Background(), query), query; got != want {
3838
t.Errorf("db.withComment(context.Background(), %q) = %q, want = %q", query, got, want)
@@ -45,15 +45,15 @@ func TestHTTP_Net(t *testing.T) {
4545
t.Fatalf("MockSQL failed with unexpected error: %s", err)
4646
}
4747

48-
db := DB{DB: mockDB, options: core.CommenterOptions{EnableDBDriver: true, EnableRoute: true, EnableFramework: true}}
48+
db := DB{DB: mockDB, driverName: "mocksql", options: core.CommenterOptions{EnableDBDriver: true, EnableRoute: true, EnableFramework: true}}
4949
r, err := http.NewRequest("GET", "hello/1", nil)
5050
if err != nil {
5151
t.Errorf("http.NewRequest('GET', 'hello/1', nil) returned unexpected error: %v", err)
5252
}
5353

5454
ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestExtractor(r, nil))
5555
got := db.withComment(ctx, "Select 1")
56-
want := "Select 1/*driver=database%2Fsql,framework=net%2Fhttp,route=hello%2F1*/"
56+
want := "Select 1/*driver=database%2Fsql%3Amocksql,framework=net%2Fhttp,route=hello%2F1*/"
5757
if got != want {
5858
t.Errorf("db.withComment(ctx, 'Select 1') got %q, wanted %q", got, want)
5959
}
@@ -65,9 +65,9 @@ func TestQueryWithSemicolon(t *testing.T) {
6565
t.Fatalf("MockSQL failed with unexpected error: %s", err)
6666
}
6767

68-
db := DB{DB: mockDB, options: core.CommenterOptions{EnableDBDriver: true}}
68+
db := DB{DB: mockDB, driverName: "mocksql", options: core.CommenterOptions{EnableDBDriver: true}}
6969
got := db.withComment(context.Background(), "Select 1;")
70-
want := "Select 1/*driver=database%2Fsql*/;"
70+
want := "Select 1/*driver=database%2Fsql%3Amocksql*/;"
7171
if got != want {
7272
t.Errorf("db.withComment(context.Background(), 'Select 1;') got %q, wanted %q", got, want)
7373
}
@@ -79,7 +79,7 @@ func TestOtelIntegration(t *testing.T) {
7979
t.Fatalf("MockSQL failed with unexpected error: %s", err)
8080
}
8181

82-
db := DB{DB: mockDB, options: core.CommenterOptions{EnableTraceparent: true}}
82+
db := DB{DB: mockDB, driverName: "mocksql", options: core.CommenterOptions{EnableTraceparent: true}}
8383
exp, _ := stdouttrace.New(stdouttrace.WithPrettyPrint())
8484
bsp := sdktrace.NewSimpleSpanProcessor(exp) // You should use batch span processor in prod
8585
tp := sdktrace.NewTracerProvider(

0 commit comments

Comments
 (0)