Skip to content

Fix inconsistent logging unit for SQL query duration reporting microseconds instead of milliseconds #3877

Description

@pritesh-4

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

  1. Configure any GoFr application with a SQL database (e.g. SQLite, PostgreSQL, or MySQL).
  2. Execute a fast database query (e.g., SELECT 1 or fetching a small row by ID).
  3. 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()
    // ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageThe issue needs triaging.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions