Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Copies the telemetry captured on <paramref name="apiEvent"/> onto a successful result's metadata.
/// Shared by the foreground path (<see cref="UpdateTelemetry"/>) and the proactive-refresh path in
/// <see cref="Microsoft.Identity.Client.Internal.SilentRequestHelper.ProcessFetchInBackground"/>, which
/// runs the fetch outside <see cref="RunAsync"/> and would otherwise leave TokenEndpoint / duration /
/// cache-refresh reason at their constructor defaults (Bug 3707191). The global
/// <see cref="Metrics.IncrementTotalDurationInMs"/> counter is intentionally left to the foreground
/// caller so background latency does not inflate the user-facing aggregate.
/// </summary>
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;
}

/// <summary>
/// Builds the subset of <see cref="AuthenticationResultMetadata"/> that is available when a
/// token request fails. Only values that were actually captured are populated; everything else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Loading