Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(injector: builtin: sql): driver is not registered from non-instrumented sql calls #411

Merged
merged 6 commits into from
Nov 22, 2024
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
42 changes: 30 additions & 12 deletions _integration-tests/tests/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,53 @@ import (

type TestCase struct {
*sql.DB
// untraced is the un-traced database connection, used to set up the test case
// without producing unnecessary spans. It is retained as a [TestCase] field
// and cleaned up using [t.Cleanup] to ensure the shared cache is not lost, as
// it stops existing once the last DB connection using it is closed.
untraced *sql.DB
}

func (tc *TestCase) Setup(t *testing.T) {
const (
dn = "sqlite3"
dsn = "file::memory:?cache=shared"
)

var err error
tc.DB, err = sql.Open("sqlite3", "file::memory:")

//orchestrion:ignore
tc.untraced, err = sql.Open(dn, dsn)
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, tc.DB.Close())
})
t.Cleanup(func() { require.NoError(t, tc.untraced.Close()) })

ctx := context.Background()

_, err = tc.DB.ExecContext(context.Background(),
_, err = tc.untraced.ExecContext(ctx,
`CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
userid INTEGER,
content STRING,
created STRING
)`)
id INTEGER PRIMARY KEY AUTOINCREMENT,
userid INTEGER,
content STRING,
created STRING
)`)
require.NoError(t, err)

_, err = tc.DB.ExecContext(context.Background(),
_, err = tc.untraced.ExecContext(ctx,
`INSERT OR REPLACE INTO notes(userid, content, created) VALUES
(1, 'Hello, John. This is John. You are leaving a note for yourself. You are welcome and thank you.', datetime('now')),
(1, 'Hey, remember to mow the lawn.', datetime('now')),
(2, 'Reminder to submit that report by Thursday.', datetime('now')),
(2, 'Opportunities don''t happen, you create them.', datetime('now')),
(3, 'Pick up cabbage from the store on the way home.', datetime('now')),
(3, 'Review PR #1138', datetime('now')
);`)
)`)
require.NoError(t, err)

tc.DB, err = sql.Open(dn, dsn)
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, tc.DB.Close())
})
}

func (tc *TestCase) Run(t *testing.T) {
Expand Down
11 changes: 9 additions & 2 deletions internal/injector/builtin/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion internal/injector/builtin/yaml/stdlib/database-sql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ aspects:
join-point:
function-call: database/sql.Register
advice:
- replace-function: gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.Register
- wrap-expression:
imports:
sqltrace: gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql
sql: database/sql
template: |-
func(driverName string, driver driver.Driver) {
sql.Register(driverName, driver)
sqltrace.Register(driverName, driver)
}({{ index .AST.Args 0 }}, {{ index .AST.Args 1 }})

- id: Open
join-point:
Expand Down
Loading