Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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,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);
}

/// <summary>
/// Copies telemetry from <paramref name="apiEvent"/> onto a successful result's metadata. Shared by the
/// foreground path (<see cref="UpdateTelemetry"/>) and proactive refresh, which runs outside
/// <see cref="RunAsync"/> (Bug 3707191). Callers own the <see cref="Metrics.IncrementTotalDurationInMs"/>
/// increment so background latency does not inflate the foreground 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,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 (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 @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// 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.");
Assert.AreEqual(TokenSource.IdentityProvider, metadata.TokenSource);
}
}

Expand Down
Loading