Skip to content

Commit 5f9889f

Browse files
Compute failure total-duration once for metadata and telemetry
Addresses PR review: the stopwatch keeps running, so re-reading it separately for the exception metadata and the OTEL log produced slightly different totals. Compute totalDurationInMs once in the catch and reuse it for both. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0289ce2 commit 5f9889f

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ public async Task<AuthenticationResult> RunAsync(CancellationToken cancellationT
115115
}
116116
AuthenticationRequestParameters.RequestContext.Logger.ErrorPii(ex);
117117

118+
// Compute the total duration once so the value stored on the exception metadata matches
119+
// the value logged to OpenTelemetry (the stopwatch keeps running, so re-reading it drifts).
120+
long totalDurationInMs = requestStopwatch.ElapsedMilliseconds + measureTelemetryDurationResult.Milliseconds;
121+
118122
if (ex.AuthenticationResultMetadata == null)
119123
{
120-
ex.AuthenticationResultMetadata = CreateFailureMetadata(
121-
apiEvent,
122-
requestStopwatch.ElapsedMilliseconds + measureTelemetryDurationResult.Milliseconds);
124+
ex.AuthenticationResultMetadata = CreateFailureMetadata(apiEvent, totalDurationInMs);
123125
}
124126

125127
MsalServiceException serviceException = ex as MsalServiceException;
@@ -130,7 +132,7 @@ public async Task<AuthenticationResult> RunAsync(CancellationToken cancellationT
130132
apiEvent,
131133
apiEvent.CacheInfo,
132134
httpStatusCode,
133-
requestStopwatch.ElapsedMilliseconds + measureTelemetryDurationResult.Milliseconds,
135+
totalDurationInMs,
134136
exception: ex,
135137
rawStsErrorCode: serviceException?.ErrorCodes?.FirstOrDefault());
136138
throw;

0 commit comments

Comments
 (0)