-
Notifications
You must be signed in to change notification settings - Fork 190
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
Ignore sql.ErrNoRows #645
Ignore sql.ErrNoRows #645
Conversation
Codecov Report
@@ Coverage Diff @@
## master #645 +/- ##
=========================================
- Coverage 88.36% 87.9% -0.47%
=========================================
Files 123 123
Lines 7900 7640 -260
=========================================
- Hits 6981 6716 -265
- Misses 798 821 +23
+ Partials 121 103 -18
Continue to review full report at Codecov.
|
@vutung2311 thanks for the PR! The Gorm change seems reasonable, but can you please add a minimal test that fails before the change? The Redis change should be made separately, if at all - please remove that from this PR so we can focus on one change at a time. If we do make the Redis change, it should be made across all of the agents - so please open a discussion at https://discuss.elastic.co/c/apm first. |
Understood. I'm gonna work on it. |
25444c6
to
b463253
Compare
f03e7f5
to
d7f98bd
Compare
I tried quite a few ways for sqlite3 but making that test to have sql.ErrNoRows was not possible for me. I modified the test to use Postgres. I hope that won't hurt anything. |
@vutung2311 thanks for the updates. I'd like to keep running the test against sqlite3, so it can be run without starting up PostgreSQL. How about this? func TestCaptureErrors(t *testing.T) {
t.Run("sqlite3", func(t *testing.T) {
db, err := apmgorm.Open("sqlite3", ":memory:")
require.NoError(t, err)
defer db.Close()
})
if os.Getenv("PGHOST") == "" {
t.Logf("PGHOST not specified, skipping")
} else {
t.Run("postgres", func(t *testing.T) {
db, err := apmgorm.Open("postgres", "user=postgres password=hunter2 dbname=test_db sslmode=disable")
require.NoError(t, err)
defer db.Close()
})
}
}
func testCaptureErrors(t *testing.T, db *gorm.DB, f func(ctx context.Context, db *gorm.DB)) {
db.SetLogger(nopLogger{})
db.AutoMigrate(&Product{})
require.NoError(t, db.Unscoped().Delete(&Product{}).Error)
_, spans, errors := apmtest.WithTransaction(func(ctx context.Context) {
db = apmgorm.WithContext(ctx, db)
// gorm.ErrRecordNotFound should not cause an error
db.Where("code=?", "L1212").First(&Product{})
// The postgres dialect will use QueryRow when inserting, in order to fetch
// the inserted row ID. By using "ON CONFLICT (id) DO NOTHING", no row will
// be inserted, and QueryRow will return sql.ErrNoRows. This should not be
// reported as an error.
product := Product{
Model: gorm.Model{
ID: 1001,
},
Code: "1001",
Price: 1001,
}
require.NoError(t, db.Create(&product).Error)
db.Set("gorm:insert_option", "ON CONFLICT (id) DO NOTHING").Create(&product)
// invalid SQL should
db.Where("bananas").First(&Product{})
})
assert.Len(t, spans, 4)
require.Len(t, errors, 1)
assert.Regexp(t, `.*bananas.*`, errors[0].Exception.Message)
} |
Thank you for your suggestion. I changed the file with few modifications to make sure that tests are independent of each others. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @vutung2311, looks good! Just a couple of minor issues left, then it should be good to merge.
jenkins, run the tests please |
Thanks again for your contribution @vutung2311! |
There are some cases that gorm can return sql.ErrNoRows when using with postgres and ON CONFLICT DO NOTHING RETURNING *. I think we can safely ignore this error as we're ignoring gorm.ErrRecordNotFound
Also add second argument of redis command trace to understand more clearly about the trace.