Repository navigation
Populate ExecutionResult.Exception for non-MSAL failures (Bug 3696306) - #6139
Merged
Neha Bhargava (neha-bhargava) merged 6 commits intoJul 22, 2026
Merged
Conversation
The OTel tags enricher received ExecutionResult.Exception = null when a token acquisition failed with a non-MsalException, because RequestBase's generic catch passed no exception to LogFailureTelemetryToOtel. It now hands the enricher a telemetry-only MsalException wrapper (originating type as ErrorCode, original message, original exception as InnerException, plus failure metadata), mirroring the MsalException path. The original exception is still re-thrown unchanged, so caller-observable behavior is unaffected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d296b5aa-33db-4934-a697-cdf1c1999e09
Contributor
There was a problem hiding this comment.
Pull request overview
This PR ensures OpenTelemetry tag enrichers receive a populated ExecutionResult.Exception even when token acquisition fails with a non-MsalException (e.g., exceptions thrown from client-assertion/federated-credential callbacks). It does this by synthesizing a telemetry-only MsalException wrapper (carrying failure metadata) while rethrowing the original exception unchanged to preserve caller-observable behavior.
Changes:
- Update
RequestBase.RunAsyncgenericcatch (Exception)path to pass a telemetry-onlyMsalExceptionwrapper intoLogFailureTelemetryToOtel, mirroring thecatch (MsalException)behavior. - Add a unit test verifying (1) the caller still receives the original non-MSAL exception and (2) the OTel enricher observes a wrapped
MsalExceptionwith metadata.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs |
Builds and passes a telemetry-only MsalException wrapper (with failure metadata) to the OTel enricher for non-MSAL failures. |
tests/Microsoft.Identity.Test.Unit/TelemetryTests/OTelInstrumentationTests.cs |
Adds a regression test covering the non-MSAL exception path and validating enricher + caller behaviors. |
Bogdan Gavril (bgavrilMS)
approved these changes
Jul 22, 2026
Gladwin Johnson VR (gladjohn)
approved these changes
Jul 22, 2026
β¦g 3696306) Address PR review: the non-MSAL enricher wrapper used ex.GetType().FullName and ex.Message directly, but the MsalException ctor rejects a null/whitespace errorCode or errorMessage. An original exception with an empty message (or a null Type.FullName for some generic/array types) would therefore raise an ArgumentNullException that replaced the original exception, breaking the rethrow-unchanged guarantee. Fall back to the type name in both cases and add a regression test for the empty-message path. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d296b5aa-33db-4934-a697-cdf1c1999e09
β¦on.Data (3696194) (#6140) Expose AuthenticationResultMetadata on non-MSAL failures via Exception.Data (Bug 3696194) When a token acquisition fails with a non-MsalException, MSAL now stashes the failure AuthenticationResultMetadata (durations, cache-refresh reason, token endpoint, region) on the original exception's Data bag under the new public MsalException.AuthenticationResultMetadataKey. Downstream header-creation providers that catch the raw exception can surface token-acquisition diagnostics without MSAL converting or wrapping the thrown exception. The original exception is re-thrown unchanged; the write is guarded so it never masks the caller's exception. Copilot-Session: d296b5aa-33db-4934-a697-cdf1c1999e09 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
β¦tstandard (3696194) The Exception.Data write added for 3696194 throws ArgumentException 'not serializable' on .NET Framework, whose ListDictionaryInternal rejects non-serializable values, replacing the caller's original exception (17 net48 test failures). .NET Core removed that check. Mark AuthenticationResultMetadata and its RegionDetails member [Serializable] only under NETFRAMEWORK || NETSTANDARD; the whole graph is serializable so the write succeeds on every target and metadata is delivered uniformly. No public-API or test changes needed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d296b5aa-33db-4934-a697-cdf1c1999e09
β¦-nonmsal-exception # Conflicts: # src/client/Microsoft.Identity.Client/PublicApi/net462/PublicAPI.Unshipped.txt # src/client/Microsoft.Identity.Client/PublicApi/net472/PublicAPI.Unshipped.txt # src/client/Microsoft.Identity.Client/PublicApi/net8.0-android/PublicAPI.Unshipped.txt # src/client/Microsoft.Identity.Client/PublicApi/net8.0-ios/PublicAPI.Unshipped.txt # src/client/Microsoft.Identity.Client/PublicApi/net8.0/PublicAPI.Unshipped.txt # src/client/Microsoft.Identity.Client/PublicApi/netstandard2.0/PublicAPI.Unshipped.txt
Gladwin Johnson VR (gladjohn)
approved these changes
Jul 22, 2026
Neha Bhargava (neha-bhargava)
deleted the
nebharg/otel-enricher-nonmsal-exception
branch
July 22, 2026 23:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug 3696306
WithOtelTagsEnricherreceivedExecutionResult.Exception == nullwhen a token acquisition failed with a non-MsalException(e.g. anHttpRequestExceptionfrom a federated-credential/client-assertion callback).RequestBase's genericcatch (Exception)passed noexceptiontoLogFailureTelemetryToOtel, so the enricher'sExecutionResult.Exceptionwas null β unlike thecatch (MsalException)path (#6096), which passes a populated exception.Fix
The non-MSAL catch now builds a telemetry-only
MsalExceptionwrapper and hands it to the enricher, mirroring theMsalExceptionpath:ErrorCode= originating exception type (FullName)Message= original messageInnerException= original exception (full fidelity)AuthenticationResultMetadata= failure metadata (durations, region, cache reason)The original exception is still re-thrown unchanged β this wrapper is never surfaced to callers, so caller-observable behavior (the exception type they catch) is unaffected.
Tests
Added
WithOtelTagsEnricher_NonMsalFailure_ReceivesWrappedExceptionWithMetadataAsync: a throwing client-assertion callback yields a rawHttpRequestException; asserts the enricher observes the populated wrapper (type/inner/message/metadata) and that the caller still receives the originalHttpRequestException. New test + all existing enricher tests pass.Related
MsalExceptionfailure path (this extends the same idea to non-MSAL failures for the enricher)CHANGELOG intentionally not updated (done post-release).