-
Notifications
You must be signed in to change notification settings - Fork 406
[Proposal] Add token acquisition metrics that cover both successful and failed t… #5928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,7 @@ public async Task<AuthenticationResult> RunAsync(CancellationToken cancellationT | |
| AuthenticationRequestParameters.RequestContext.ApiEvent = apiEvent; | ||
| }); | ||
|
|
||
| var requestStopwatch = Stopwatch.StartNew(); | ||
| try | ||
| { | ||
| AuthenticationResult authenticationResult = null; | ||
|
|
@@ -106,44 +107,41 @@ public async Task<AuthenticationResult> RunAsync(CancellationToken cancellationT | |
| } | ||
| AuthenticationRequestParameters.RequestContext.Logger.ErrorPii(ex); | ||
|
|
||
| LogFailureTelemetryToOtel(ex.ErrorCode, apiEvent, apiEvent.CacheInfo); | ||
| int httpStatusCode = ex is MsalServiceException serviceEx ? serviceEx.StatusCode : 0; | ||
| LogFailureTelemetryToOtel(apiEvent, apiEvent.ApiErrorCode, httpStatusCode, requestStopwatch.ElapsedMilliseconds + measureTelemetryDurationResult.Milliseconds); | ||
|
Comment on lines
+110
to
+111
|
||
| throw; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| apiEvent.ApiErrorCode = ex.GetType().Name; | ||
| AuthenticationRequestParameters.RequestContext.Logger.ErrorPii(ex); | ||
|
|
||
| LogFailureTelemetryToOtel(ex.GetType().Name, apiEvent, apiEvent.CacheInfo); | ||
| LogFailureTelemetryToOtel(apiEvent, apiEvent.ApiErrorCode, httpStatusCode: 0, requestStopwatch.ElapsedMilliseconds + measureTelemetryDurationResult.Milliseconds); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| private void LogSuccessTelemetryToOtel(AuthenticationResult authenticationResult, ApiEvent apiEvent, long durationInUs) | ||
| { | ||
| // Log metrics | ||
| ServiceBundle.PlatformProxy.OtelInstrumentation.LogSuccessMetrics( | ||
| ServiceBundle.PlatformProxy.GetProductName(), | ||
| apiEvent.ApiId, | ||
| apiEvent.CallerSdkApiId, | ||
| apiEvent.CallerSdkVersion, | ||
| GetCacheLevel(authenticationResult), | ||
| durationInUs, | ||
| authenticationResult.AuthenticationResultMetadata, | ||
| AuthenticationRequestParameters.RequestContext.Logger); | ||
| ServiceBundle.PlatformProxy.GetProductName(), | ||
| apiEvent.ApiId, | ||
| apiEvent.CallerSdkApiId, | ||
| apiEvent.CallerSdkVersion, | ||
| GetCacheLevel(authenticationResult), | ||
| durationInUs, | ||
| authenticationResult.AuthenticationResultMetadata, | ||
| AuthenticationRequestParameters.RequestContext.Logger); | ||
| } | ||
|
|
||
| private void LogFailureTelemetryToOtel(string errorCodeToLog, ApiEvent apiEvent, CacheRefreshReason cacheRefreshReason) | ||
| private void LogFailureTelemetryToOtel(ApiEvent apiEvent, string errorCode, int httpStatusCode, long totalDurationInMs) | ||
| { | ||
| // Log metrics | ||
| ServiceBundle.PlatformProxy.OtelInstrumentation.LogFailureMetrics( | ||
| ServiceBundle.PlatformProxy.GetProductName(), | ||
| errorCodeToLog, | ||
| apiEvent.ApiId, | ||
| apiEvent.CallerSdkApiId, | ||
| apiEvent.CallerSdkVersion, | ||
| cacheRefreshReason, | ||
| apiEvent.TokenType); | ||
| ServiceBundle.PlatformProxy.GetProductName(), | ||
| apiEvent, | ||
| errorCode, | ||
| httpStatusCode, | ||
| totalDurationInMs); | ||
| } | ||
|
|
||
| private Tuple<string, string> ParseScopesForTelemetry() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,7 @@ protected override async Task<AuthenticationResult> ExecuteAsync(CancellationTok | |
| if (isBrokerConfigured && !isAccountSourceDeviceCodeFlow) | ||
| { | ||
| _logger.Info("Broker is configured and enabled, attempting to use broker instead."); | ||
| AuthenticationRequestParameters.RequestContext.ApiEvent.TokenSource = TokenSource.Broker; | ||
| var brokerResult = await _brokerStrategyLazy.Value.ExecuteAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| // fallback to local cache if broker fails | ||
|
Comment on lines
68
to
74
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
finally block might be a good place to place the duration here that will cover all the cases. Here and in the RequestBase.cs as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep it scoped to the network‑bound portion — putting it in finally would also include response body read.