Description
There is a duration unit inconsistency in the SQL datasource query logging. The console logger formats query execution latency with a µs (microsecond) suffix, but the underlying duration value is calculated in milliseconds.
Other datasources within GoFr (such as Redis, MongoDB, Cassandra, ClickHouse, and SurrealDB) correctly calculate query latency in microseconds and format them with µs.
Steps to Reproduce
- Configure any GoFr application with a SQL database (e.g. SQLite, PostgreSQL, or MySQL).
- Execute a fast database query (e.g.,
SELECT 1 or fetching a small row by ID).
- Check the output logs in the terminal.
Observed Logs:
(Even when the query takes hundreds of microseconds to execute).
If a query takes exactly 15 milliseconds, it is logged as:
Expected Behavior
The query latency logs should report accurate duration in microseconds, matching the rest of GoFr's datasources:
- A query taking 15 milliseconds should be logged as
15000µs (or 15.0ms if using milliseconds).
- A sub-millisecond query taking 500 microseconds should be logged as
500µs.
Actual Behavior
The SQL datasource logs milliseconds under the microsecond (µs) unit label, causing a 1000x under-reporting and truncating sub-millisecond queries to 0µs.
Root Cause
In pkg/gofr/datasource/sql/db.go, the helper function sendStats calculates query execution duration in milliseconds:
func sendStats(logger datasource.Logger, metrics Metrics, config *DBConfig, start time.Time, queryType, query string, args ...any) {
duration := time.Since(start).Milliseconds()
// ...
Description
There is a duration unit inconsistency in the SQL datasource query logging. The console logger formats query execution latency with a
µs(microsecond) suffix, but the underlying duration value is calculated in milliseconds.Other datasources within GoFr (such as Redis, MongoDB, Cassandra, ClickHouse, and SurrealDB) correctly calculate query latency in microseconds and format them with
µs.Steps to Reproduce
SELECT 1or fetching a small row by ID).Observed Logs:
(Even when the query takes hundreds of microseconds to execute).
If a query takes exactly 15 milliseconds, it is logged as:
Expected Behavior
The query latency logs should report accurate duration in microseconds, matching the rest of GoFr's datasources:
15000µs(or15.0msif using milliseconds).500µs.Actual Behavior
The SQL datasource logs milliseconds under the microsecond (
µs) unit label, causing a 1000x under-reporting and truncating sub-millisecond queries to0µs.Root Cause
In
pkg/gofr/datasource/sql/db.go, the helper functionsendStatscalculates query execution duration in milliseconds: