logging: fix SQL timing and upsert-conflict false alarms

Room's statement cache resets a statement when returning it to the cache, so starting the timer at reset counted idle time in the cache and reported normal queries as tens of seconds. Start timing on the first step instead, and finish on the completing step. Also downgrade Room @Upsert uniqueness-conflict errors (its insert-then-catch-then-update control flow) from ERROR to DEBUG, and log successful HTTP requests at VERBOSE so debug logs aren't flooded by cover-art requests.
This commit is contained in:
austinried
2026-10-09 07:58:55 +00:00
parent eb5019ea5e
commit c262e58a96
4 changed files with 62 additions and 11 deletions
@@ -62,6 +62,25 @@ class DbLoggingTest {
assertTrue(select.durationNanos > 0)
}
@Test
fun doesNotCountIdleTimeBetweenReuses() {
driver().open(":memory:").use { connection ->
connection.prepare("CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)").use { it.step() }
connection.prepare("INSERT INTO t (name) VALUES (?)").use { statement ->
statement.bindText(1, "a")
statement.step()
statement.reset()
Thread.sleep(250)
statement.bindText(1, "b")
statement.step()
statement.reset()
}
}
val inserts = executions.filter { it.sql.startsWith("INSERT") }
assertEquals(2, inserts.size)
inserts.forEach { assertTrue("${it.durationMillis}ms", it.durationNanos < 100_000_000) }
}
@Test
fun recordsPrepareError() {
driver().open(":memory:").use { connection ->
@@ -150,6 +150,14 @@ class LogFileStoreTest {
assertFalse(isReadStatement("INSERT INTO t VALUES (1)"))
}
@Test
fun detectsUpsertConflictErrors() {
assertTrue(isUpsertConflict(IllegalStateException("UNIQUE constraint failed: playback_cursor.id")))
assertTrue(isUpsertConflict(IllegalStateException("Error code: 1555, message: UNIQUE constraint failed")))
assertTrue(isUpsertConflict(IllegalStateException("SQLITE_CONSTRAINT_UNIQUE 2067")))
assertFalse(isUpsertConflict(IllegalStateException("no such table: songs")))
}
private fun execution(
milliseconds: Double,
sql: String,