diff --git a/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs b/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs
index 222a63f48d..85010d7f30 100644
--- a/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs
+++ b/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs
@@ -313,19 +313,38 @@ private static void LogMetricsFromAuthResult(AuthenticationResult authentication
private void UpdateTelemetry(long elapsedMilliseconds, ApiEvent apiEvent, AuthenticationResult authenticationResult)
{
- authenticationResult.AuthenticationResultMetadata.DurationTotalInMs = elapsedMilliseconds;
- authenticationResult.AuthenticationResultMetadata.DurationInHttpInMs = apiEvent.DurationInHttpInMs;
- authenticationResult.AuthenticationResultMetadata.DurationInCacheInMs = apiEvent.DurationInCacheInMs;
- authenticationResult.AuthenticationResultMetadata.TokenEndpoint = apiEvent.TokenEndpoint;
- authenticationResult.AuthenticationResultMetadata.CacheRefreshReason = apiEvent.CacheInfo;
- authenticationResult.AuthenticationResultMetadata.CacheLevel = GetCacheLevel(authenticationResult);
- authenticationResult.AuthenticationResultMetadata.Telemetry = apiEvent.MsalRuntimeTelemetry;
- authenticationResult.AuthenticationResultMetadata.RegionDetails = CreateRegionDetails(apiEvent);
- authenticationResult.AuthenticationResultMetadata.CachedAccessTokenCount = apiEvent.CachedAccessTokenCount;
+ PopulateSuccessMetadata(
+ authenticationResult.AuthenticationResultMetadata,
+ apiEvent,
+ elapsedMilliseconds,
+ GetCacheLevel(authenticationResult));
Metrics.IncrementTotalDurationInMs(authenticationResult.AuthenticationResultMetadata.DurationTotalInMs);
}
+ ///
+ /// Copies telemetry from onto a successful result's metadata. Shared by the
+ /// foreground path () and proactive refresh, which runs outside
+ /// . Callers own the
+ /// increment so background latency does not inflate the foreground aggregate.
+ ///
+ internal static void PopulateSuccessMetadata(
+ AuthenticationResultMetadata metadata,
+ ApiEvent apiEvent,
+ long totalDurationInMs,
+ CacheLevel cacheLevel)
+ {
+ metadata.DurationTotalInMs = totalDurationInMs;
+ metadata.DurationInHttpInMs = apiEvent.DurationInHttpInMs;
+ metadata.DurationInCacheInMs = apiEvent.DurationInCacheInMs;
+ metadata.TokenEndpoint = apiEvent.TokenEndpoint;
+ metadata.CacheRefreshReason = apiEvent.CacheInfo;
+ metadata.CacheLevel = cacheLevel;
+ metadata.Telemetry = apiEvent.MsalRuntimeTelemetry;
+ metadata.RegionDetails = CreateRegionDetails(apiEvent);
+ metadata.CachedAccessTokenCount = apiEvent.CachedAccessTokenCount;
+ }
+
///
/// Builds the subset of that is available when a
/// token request fails. Only values that were actually captured are populated; everything else
diff --git a/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs b/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs
index 199a530a5c..4a868431f5 100644
--- a/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs
+++ b/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs
@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -97,7 +98,18 @@ internal static void ProcessFetchInBackground(
{
try
{
+ var stopwatch = Stopwatch.StartNew();
var authResult = await fetchAction().ConfigureAwait(false);
+ stopwatch.Stop();
+
+ // Proactive refresh runs outside RequestBase.RunAsync, so backfill the result's telemetry
+ // from the shared apiEvent using this operation's own elapsed time.
+ RequestBase.PopulateSuccessMetadata(
+ authResult.AuthenticationResultMetadata,
+ apiEvent,
+ stopwatch.ElapsedMilliseconds,
+ Cache.CacheLevel.None);
+
var executionResult = new ExecutionResult { Successful = true, Result = authResult };
// Invoke the enricher once for this background refresh and reuse the materialized
diff --git a/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs b/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs
index 8c650f3b9e..1fbfa9c6c3 100644
--- a/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs
+++ b/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs
@@ -1038,6 +1038,13 @@ public async Task ManagedIdentity_BackgroundRefresh_InvokesCallback_Async()
Assert.IsTrue(capturedResult.Successful);
Assert.IsNotNull(capturedResult.Result);
Assert.IsNotNull(capturedResult.Result.AuthenticationResultMetadata);
+
+ // The proactive-refresh result must carry telemetry backfilled from the
+ // apiEvent. Managed identity does not flow through TokenClient, so TokenEndpoint stays null even in
+ // the foreground; assert the fields that do apply here.
+ AuthenticationResultMetadata metadata = capturedResult.Result.AuthenticationResultMetadata;
+ Assert.AreEqual(CacheRefreshReason.ProactivelyRefreshed, metadata.CacheRefreshReason);
+ Assert.AreEqual(TokenSource.IdentityProvider, metadata.TokenSource);
}
}
diff --git a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs
index 4938966fc9..e41dac2bd5 100644
--- a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs
+++ b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs
@@ -565,6 +565,14 @@ public async Task ClientCredentials_BackgroundRefresh_Success_InvokesCallback_As
Assert.IsTrue(capturedResult.Successful);
Assert.IsNotNull(capturedResult.Result);
Assert.IsNotNull(capturedResult.Result.AuthenticationResultMetadata);
+
+ // The proactive-refresh result must carry the same metadata a foreground refresh
+ // would. Assert the deterministically backfilled fields (DurationTotalInMs is flaky under a
+ // sub-millisecond mock round-trip).
+ AuthenticationResultMetadata metadata = capturedResult.Result.AuthenticationResultMetadata;
+ Assert.AreEqual(CacheRefreshReason.ProactivelyRefreshed, metadata.CacheRefreshReason);
+ Assert.IsFalse(string.IsNullOrEmpty(metadata.TokenEndpoint), "TokenEndpoint must be populated on a proactive-refresh result.");
+ Assert.AreEqual(TokenSource.IdentityProvider, metadata.TokenSource);
}
}