Skip to content

Commit 3cbc0ae

Browse files
committed
Fix QueryContext telemetry timing issue
Capture execution start time before runQuery and use BeforeExecuteWithTime to ensure telemetry accurately reflects actual query execution time. This completes the timing fix for both ExecContext and QueryContext.
1 parent bae0836 commit 3cbc0ae

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

connection.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
192192

193193
// first we try to get the results synchronously.
194194
// at any point in time that the context is done we must cancel and return
195+
196+
// Capture execution start time for telemetry before running the query
197+
executeStart := time.Now()
195198
exStmtResp, opStatusResp, err := c.runQuery(ctx, query, args)
196199
log, ctx = client.LoggerAndContext(ctx, exStmtResp)
197200
defer log.Duration(msg, start)
@@ -200,7 +203,8 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
200203
var statementID string
201204
if c.telemetry != nil && exStmtResp != nil && exStmtResp.OperationHandle != nil && exStmtResp.OperationHandle.OperationId != nil {
202205
statementID = client.SprintGuid(exStmtResp.OperationHandle.OperationId.GUID)
203-
ctx = c.telemetry.BeforeExecute(ctx, c.id, statementID)
206+
// Use BeforeExecuteWithTime to set the correct start time (before execution)
207+
ctx = c.telemetry.BeforeExecuteWithTime(ctx, c.id, statementID, executeStart)
204208
defer func() {
205209
c.telemetry.AfterExecute(ctx, err)
206210
c.telemetry.CompleteStatement(ctx, statementID, err != nil)

0 commit comments

Comments
 (0)