Add MsalRemainingTokenLifetime histogram for token expiry #5920
Add MsalRemainingTokenLifetime histogram for token expiry #5920neha-bhargava wants to merge 3 commits intomainfrom
Conversation
…vability Add new OTel histogram (MsalRemainingTokenLifetime.1A) that records the remaining lifetime of tokens in minutes at the time of acquisition. Tags (6/8): MsalVersionPlatform (combined), ApiId, TokenSource, CacheLevel, CacheRefreshReason, TokenType (human-readable string). Also adds TelemetryTokenTypeConstants.ToDisplayString() helper for int-to- string token type mapping used by the new histogram. Resolves: #5889 PBI: 3554274 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds a new OpenTelemetry histogram (MsalRemainingTokenLifetime.1A) to capture token time-to-expiry at acquisition time, and wires it into the successful token acquisition telemetry path. This supports the requested MSAL telemetry enhancement for remaining token lifetime.
Changes:
- Added
MsalRemainingTokenLifetime.1Ahistogram emission (with tags) inOtelInstrumentationand invoked it from the success telemetry path. - Added
TelemetryTokenTypeConstants.ToDisplayString(int)to emit a human-readable token type for the new metric. - Updated OTel unit tests to account for the additional exported metric and validate tags for the new histogram.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Microsoft.Identity.Test.Unit/TelemetryTests/OTelInstrumentationTests.cs | Updates expected metric counts and adds tag validation for the new remaining-lifetime histogram. |
| src/client/Microsoft.Identity.Client/TelemetryCore/TelemetryConstants.cs | Adds MsalVersionPlatform tag key constant used by the new metric. |
| src/client/Microsoft.Identity.Client/TelemetryCore/OpenTelemetry/IOtelInstrumentation.cs | Extends the instrumentation interface with LogRemainingTokenLifetime(...). |
| src/client/Microsoft.Identity.Client/TelemetryCore/OpenTelemetry/NullOtelInstrumentation.cs | Implements the new interface method as a no-op for null instrumentation. |
| src/client/Microsoft.Identity.Client/Platforms/Features/OpenTelemetry/OtelInstrumentation.cs | Defines and records the new MsalRemainingTokenLifetime.1A histogram. |
| src/client/Microsoft.Identity.Client/Internal/TelemetryTokenTypeConstants.cs | Adds an int→string token type mapping helper for telemetry display. |
| src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs | Calls LogRemainingTokenLifetime(...) during successful request telemetry logging. |
| expectedTags.Add(TelemetryConstants.ApiId); | ||
| expectedTags.Add(TelemetryConstants.TokenSource); | ||
| expectedTags.Add(TelemetryConstants.CacheLevel); | ||
| expectedTags.Add(TelemetryConstants.CacheRefreshReason); |
There was a problem hiding this comment.
Test validates tags but never asserts the recorded histogram value
Robbie-Microsoft
left a comment
There was a problem hiding this comment.
PR description says tags are "6/8" but only 6 are emitted. The description mentions 8 planned tags - it's unclear which 2 are missing and whether that's intentional or an oversight worth clarifying.
- Merge LogRemainingTokenLifetime into LogSuccessMetrics (ssmelov comment) - Remove duplicate GetCacheLevel call, compute once and reuse (Robbie +1) - Use int for TokenType tag instead of string for consistency with other metrics - Remove ToDisplayString helper (no longer needed with int TokenType) - Add histogram value assertions in test (count > 0, sum >= 0) - XML doc correctly says seconds (align PR description accordingly) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The new MsalRemainingTokenLifetime histogram adds one more metric to the export, bringing the total from 5 to 6. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| /// <summary> | ||
| /// Histogram to record the remaining lifetime of acquired tokens in seconds. | ||
| /// </summary> | ||
| internal static readonly Lazy<Histogram<long>> s_remainingTokenLifetime = new(() => Meter.CreateHistogram<long>( | ||
| RemainingTokenLifetimeHistogramName, | ||
| unit: "s", | ||
| description: "Remaining lifetime of acquired tokens at the time of acquisition.")); |
There was a problem hiding this comment.
PR description says MsalRemainingTokenLifetime.1A should record remaining token lifetime in minutes, but the instrument is defined/recorded in seconds (unit "s" and TotalSeconds). Either update the implementation/unit to minutes, or update the PR description and any downstream expectations to explicitly be seconds.
| s_remainingTokenLifetime.Value.Record(remainingSeconds, | ||
| new(TelemetryConstants.MsalVersionPlatform, $"{MsalIdHelper.GetMsalVersion()},{platform}"), | ||
| new(TelemetryConstants.ApiId, apiId), | ||
| new(TelemetryConstants.TokenSource, authResultMetadata.TokenSource), | ||
| new(TelemetryConstants.CacheLevel, cacheLevel), | ||
| new(TelemetryConstants.CacheRefreshReason, authResultMetadata.CacheRefreshReason), | ||
| new(TelemetryConstants.TokenType, authResultMetadata.TelemetryTokenType)); |
There was a problem hiding this comment.
PR description calls out TokenType tag as a human-readable string and mentions adding a TelemetryTokenTypeConstants.ToDisplayString() helper, but this metric records authResultMetadata.TelemetryTokenType (an int). If TokenType is intended to be a display string for this histogram, convert the int to the display string (e.g., via the helper) before tagging; otherwise, update the PR description to match the emitted tag value type.
Add new OTel histogram (MsalRemainingTokenLifetime.1A) that records the remaining lifetime of tokens in seconds at the time of acquisition.
Tags (6/8): MsalVersionPlatform (combined), ApiId, TokenSource, CacheLevel, CacheRefreshReason, TokenType.
Also adds TelemetryTokenTypeConstants.ToDisplayString() helper for int-to- string token type mapping used by the new histogram.
Resolves: #5889
PBI: 3554274
Fixes #
Changes proposed in this request
Testing
Performance impact
Documentation