Skip to content

Commit 7459a03

Browse files
committed
wip
1 parent ff009af commit 7459a03

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

go/database/sql/go-sql.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@ import (
1818
"context"
1919
"database/sql"
2020
"fmt"
21+
"runtime/debug"
2122
"strings"
2223

2324
"github.com/google/sqlcommenter/go/core"
2425
)
2526

27+
var attemptedToAutosetApplication = false
28+
2629
type DB struct {
2730
*sql.DB
28-
driverName string
29-
options core.CommenterOptions
31+
driverName string
32+
options core.CommenterOptions
33+
application string
3034
}
3135

3236
func Open(driverName string, dataSourceName string, options core.CommenterOptions) (*DB, error) {
3337
db, err := sql.Open(driverName, dataSourceName)
34-
return &DB{DB: db, driverName: driverName, options: options}, err
38+
return &DB{DB: db, driverName: driverName, options: options, application: options.Application}, err
3539
}
3640

3741
// ***** Query Functions *****
@@ -91,6 +95,20 @@ func (db *DB) withComment(ctx context.Context, query string) string {
9195
commentsMap[core.Route] = ctx.Value(core.Route).(string)
9296
}
9397

98+
if db.options.EnableApplication {
99+
if !attemptedToAutosetApplication && db.application == "" {
100+
attemptedToAutosetApplication = true
101+
bi, ok := debug.ReadBuildInfo()
102+
if ok {
103+
db.application = bi.Path
104+
} else {
105+
db.application = ""
106+
}
107+
}
108+
109+
commentsMap[core.Application] = db.application
110+
}
111+
94112
if db.options.EnableTraceparent {
95113
carrier := core.ExtractTraceparent(ctx)
96114
if val, ok := carrier["traceparent"]; ok {

go/database/sql/go-sql_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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, driverName: "mocksql", 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, EnableApplication: true, Application: "app"}, application: "app"}
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%3Amocksql,framework=net%2Fhttp,route=hello%2F1*/"
56+
want := "Select 1/*application=app,db_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
}
@@ -67,7 +67,7 @@ func TestQueryWithSemicolon(t *testing.T) {
6767

6868
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%3Amocksql*/;"
70+
want := "Select 1/*db_driver=database%2Fsql%3Amocksql*/;"
7171
if got != want {
7272
t.Errorf("db.withComment(context.Background(), 'Select 1;') got %q, wanted %q", got, want)
7373
}

go/database/sql/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/google/sqlcommenter/go/database/sql
33
go 1.19
44

55
require (
6-
github.com/google/sqlcommenter/go/core v0.0.1-beta
6+
github.com/google/sqlcommenter/go/core v0.0.2-beta
77
go.opentelemetry.io/otel/sdk v1.10.0
88
)
99

@@ -16,7 +16,7 @@ require (
1616

1717
require (
1818
github.com/DATA-DOG/go-sqlmock v1.5.0
19-
github.com/google/sqlcommenter/go/net/http v0.0.1-beta
19+
github.com/google/sqlcommenter/go/net/http v0.0.2-beta
2020
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0
2121
go.opentelemetry.io/otel/trace v1.10.0 // indirect
2222
)

0 commit comments

Comments
 (0)