From 32a87120d089ac24213bd3ada8236721d742c537 Mon Sep 17 00:00:00 2001 From: Neha Bhargava Date: Mon, 3 Aug 2026 16:32:16 -0700 Subject: [PATCH 1/3] Populate telemetry metadata on proactive-refresh results (3707191) Proactive (background) refresh runs the fetch outside RequestBase.RunAsync, so the AuthenticationResultMetadata that RunAsync normally fills in (TokenEndpoint, DurationTotalInMs, DurationInHttpInMs, CacheRefreshReason, RegionDetails, etc.) was left at its constructor defaults. Consumers of the OnBackgroundTokenRefreshCompleted callback, and the LogSuccessHttpDuration OTel emission, therefore saw stripped telemetry (null TokenEndpoint, 0 duration). Extract the metadata-population core of UpdateTelemetry into a shared static RequestBase.PopulateSuccessMetadata and call it from the background success path in SilentRequestHelper.ProcessFetchInBackground, backfilling from the shared apiEvent and using the background operation's own elapsed time as the total duration. Mirrors the existing failure-path treatment via CreateFailureMetadata. The global Metrics total-duration counter stays foreground-only so background latency does not inflate the user-facing aggregate. Strengthen the background-refresh success test to assert the metadata is populated (fails against the pre-fix implementation). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 766dbf9f-f979-44b5-9362-a1a1efae90ea --- .../Internal/Requests/RequestBase.cs | 40 ++++++++++++++----- .../Internal/Requests/SilentRequestHelper.cs | 15 +++++++ .../PublicApiTests/RefreshInTests.cs | 9 +++++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs b/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs index 222a63f48d..54687d81f7 100644 --- a/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs +++ b/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs @@ -313,19 +313,41 @@ 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 the telemetry captured on onto a successful result's metadata. + /// Shared by the foreground path () and the proactive-refresh path in + /// , which + /// runs the fetch outside and would otherwise leave TokenEndpoint / duration / + /// cache-refresh reason at their constructor defaults (Bug 3707191). The global + /// counter is intentionally left to the foreground + /// caller so background latency does not inflate the user-facing 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..62658b9f4b 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,21 @@ internal static void ProcessFetchInBackground( { try { + var stopwatch = Stopwatch.StartNew(); var authResult = await fetchAction().ConfigureAwait(false); + stopwatch.Stop(); + + // Proactive refresh runs the fetch outside RequestBase.RunAsync, so the telemetry that + // RunAsync normally copies onto the result (token endpoint, durations, cache-refresh reason) + // is missing. Backfill it from the shared apiEvent - which the background HTTP call has + // populated by now - using this background operation's own elapsed time as the total + // duration. TokenSource is IdentityProvider here, so no cache level applies (Bug 3707191). + 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/PublicApiTests/RefreshInTests.cs b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs index 4938966fc9..b6853e22e9 100644 --- a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs @@ -565,6 +565,15 @@ public async Task ClientCredentials_BackgroundRefresh_Success_InvokesCallback_As Assert.IsTrue(capturedResult.Successful); Assert.IsNotNull(capturedResult.Result); Assert.IsNotNull(capturedResult.Result.AuthenticationResultMetadata); + + // Regression (Bug 3707191): proactive refresh runs outside RequestBase.RunAsync, which used to + // leave the result's telemetry metadata stripped. The result must now carry the same metadata a + // foreground refresh would - populated from the shared apiEvent. Assert the deterministically + // populated fields (a sub-millisecond mock round-trip makes DurationTotalInMs an unreliable check). + 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); } } From 11e9e8f46d57f678a1314fe117502b31d7f998c9 Mon Sep 17 00:00:00 2001 From: Neha Bhargava Date: Mon, 3 Aug 2026 23:32:50 -0700 Subject: [PATCH 2/3] Trim comments; strengthen MI proactive-refresh metadata test (3707191) Make the added comments concise. Also strengthen the managed identity background-refresh test to assert the proactive-refresh result carries backfilled telemetry (CacheRefreshReason, TokenSource) - MI is the other primary OnBackgroundTokenRefreshCompleted consumer and flows through the same ProcessFetchInBackground path. TokenEndpoint stays null for MI (no TokenClient), so only the applicable fields are asserted. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 766dbf9f-f979-44b5-9362-a1a1efae90ea --- .../Internal/Requests/RequestBase.cs | 11 ++++------- .../Internal/Requests/SilentRequestHelper.cs | 7 ++----- .../ManagedIdentityTests/ManagedIdentityTests.cs | 7 +++++++ .../PublicApiTests/RefreshInTests.cs | 7 +++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs b/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs index 54687d81f7..9968841a33 100644 --- a/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs +++ b/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs @@ -323,13 +323,10 @@ private void UpdateTelemetry(long elapsedMilliseconds, ApiEvent apiEvent, Authen } /// - /// Copies the telemetry captured on onto a successful result's metadata. - /// Shared by the foreground path () and the proactive-refresh path in - /// , which - /// runs the fetch outside and would otherwise leave TokenEndpoint / duration / - /// cache-refresh reason at their constructor defaults (Bug 3707191). The global - /// counter is intentionally left to the foreground - /// caller so background latency does not inflate the user-facing aggregate. + /// Copies telemetry from onto a successful result's metadata. Shared by the + /// foreground path () and proactive refresh, which runs outside + /// (Bug 3707191). Callers own the + /// increment so background latency does not inflate the foreground aggregate. /// internal static void PopulateSuccessMetadata( AuthenticationResultMetadata metadata, diff --git a/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs b/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs index 62658b9f4b..53fdf4f04d 100644 --- a/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs +++ b/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs @@ -102,11 +102,8 @@ internal static void ProcessFetchInBackground( var authResult = await fetchAction().ConfigureAwait(false); stopwatch.Stop(); - // Proactive refresh runs the fetch outside RequestBase.RunAsync, so the telemetry that - // RunAsync normally copies onto the result (token endpoint, durations, cache-refresh reason) - // is missing. Backfill it from the shared apiEvent - which the background HTTP call has - // populated by now - using this background operation's own elapsed time as the total - // duration. TokenSource is IdentityProvider here, so no cache level applies (Bug 3707191). + // Proactive refresh runs outside RequestBase.RunAsync, so backfill the result's telemetry + // from the shared apiEvent using this operation's own elapsed time (Bug 3707191). RequestBase.PopulateSuccessMetadata( authResult.AuthenticationResultMetadata, apiEvent, diff --git a/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs b/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs index 8c650f3b9e..e0ac66a7fa 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); + + // Regression (Bug 3707191): 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 b6853e22e9..03f5a49eba 100644 --- a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs @@ -566,10 +566,9 @@ public async Task ClientCredentials_BackgroundRefresh_Success_InvokesCallback_As Assert.IsNotNull(capturedResult.Result); Assert.IsNotNull(capturedResult.Result.AuthenticationResultMetadata); - // Regression (Bug 3707191): proactive refresh runs outside RequestBase.RunAsync, which used to - // leave the result's telemetry metadata stripped. The result must now carry the same metadata a - // foreground refresh would - populated from the shared apiEvent. Assert the deterministically - // populated fields (a sub-millisecond mock round-trip makes DurationTotalInMs an unreliable check). + // Regression (Bug 3707191): proactive refresh 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."); From a59c3c3be4ad990a766d0f9dc416f345eccc9013 Mon Sep 17 00:00:00 2001 From: Neha Bhargava Date: Mon, 3 Aug 2026 23:35:58 -0700 Subject: [PATCH 3/3] Remove bug number from comments; drop regression framing Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 766dbf9f-f979-44b5-9362-a1a1efae90ea --- .../Microsoft.Identity.Client/Internal/Requests/RequestBase.cs | 2 +- .../Internal/Requests/SilentRequestHelper.cs | 2 +- .../ManagedIdentityTests/ManagedIdentityTests.cs | 2 +- .../PublicApiTests/RefreshInTests.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs b/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs index 9968841a33..85010d7f30 100644 --- a/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs +++ b/src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs @@ -325,7 +325,7 @@ private void UpdateTelemetry(long elapsedMilliseconds, ApiEvent apiEvent, Authen /// /// Copies telemetry from onto a successful result's metadata. Shared by the /// foreground path () and proactive refresh, which runs outside - /// (Bug 3707191). Callers own the + /// . Callers own the /// increment so background latency does not inflate the foreground aggregate. /// internal static void PopulateSuccessMetadata( diff --git a/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs b/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs index 53fdf4f04d..4a868431f5 100644 --- a/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs +++ b/src/client/Microsoft.Identity.Client/Internal/Requests/SilentRequestHelper.cs @@ -103,7 +103,7 @@ internal static void ProcessFetchInBackground( 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 (Bug 3707191). + // from the shared apiEvent using this operation's own elapsed time. RequestBase.PopulateSuccessMetadata( authResult.AuthenticationResultMetadata, apiEvent, diff --git a/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs b/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs index e0ac66a7fa..1fbfa9c6c3 100644 --- a/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs @@ -1039,7 +1039,7 @@ public async Task ManagedIdentity_BackgroundRefresh_InvokesCallback_Async() Assert.IsNotNull(capturedResult.Result); Assert.IsNotNull(capturedResult.Result.AuthenticationResultMetadata); - // Regression (Bug 3707191): the proactive-refresh result must carry telemetry backfilled from the + // 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; diff --git a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs index 03f5a49eba..e41dac2bd5 100644 --- a/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/PublicApiTests/RefreshInTests.cs @@ -566,7 +566,7 @@ public async Task ClientCredentials_BackgroundRefresh_Success_InvokesCallback_As Assert.IsNotNull(capturedResult.Result); Assert.IsNotNull(capturedResult.Result.AuthenticationResultMetadata); - // Regression (Bug 3707191): proactive refresh must carry the same metadata a foreground refresh + // 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;