Skip to content

Commit

Permalink
Updating extension telemetry (#4996)
Browse files Browse the repository at this point in the history
* Updating extension telemetry

* Refactroing
Removing TokenType and replacing the values with string

* Updating PR to revert new histogram logic.

* TODO

* Refactoring.
Addressing Feedback

* Apply suggestions from code review

Co-authored-by: Gladwin Johnson <[email protected]>

* Addressing PR Feedback

* Update

* Resolving test issues

* Removing External

* update

* Updating WAM header

* Resolving WAM test issues

* Fixing test

---------

Co-authored-by: trwalke <[email protected]>
Co-authored-by: Gladwin Johnson <[email protected]>
  • Loading branch information
3 people authored Dec 24, 2024
1 parent b54af18 commit 2ad7d8d
Show file tree
Hide file tree
Showing 30 changed files with 138 additions and 108 deletions.
6 changes: 5 additions & 1 deletion src/client/Microsoft.Identity.Client.Broker/WamAdapters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@ private static MsalTokenResponse ParseRuntimeResponse(
Scope = authResult.GrantedScopes,
ExpiresIn = (long)(DateTime.SpecifyKind(authResult.ExpiresOn, DateTimeKind.Utc) - DateTimeOffset.UtcNow).TotalSeconds,
ClientInfo = authResult.Account.ClientInfo,
TokenType = authResult.IsPopAuthorization ? Constants.PoPAuthHeaderPrefix: authenticationRequestParameters.RequestContext.ApiEvent.TokenType.ToString(),
TokenType = authResult.IsPopAuthorization ?
Constants.PoPAuthHeaderPrefix :
(authenticationRequestParameters.AuthenticationScheme.AccessTokenType == "ssh-cert" ?
Constants.SshCertAuthHeaderPrefix :
authenticationRequestParameters.AuthenticationScheme.AuthorizationHeaderPrefix),
WamAccountId = authResult.Account.AccountId,
TokenSource = TokenSource.Broker
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using Microsoft.Identity.Client.Cache.Items;
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.Utils;

namespace Microsoft.Identity.Client.AuthScheme.Bearer
Expand All @@ -11,7 +12,7 @@ internal class BearerAuthenticationOperation : IAuthenticationOperation
{
internal const string BearerTokenType = "bearer";

public int TelemetryTokenType => (int)TokenType.Bearer;
public int TelemetryTokenType => TelemetryTokenTypeConstants.Bearer;

public string AuthorizationHeaderPrefix => "Bearer";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface IAuthenticationOperation
string AuthorizationHeaderPrefix { get; }

/// <summary>
/// Extra parameters that are added to the request to the /token endpoint.
/// Extra parameters that are added to the request to the /token endpoint.
/// </summary>
/// <returns>Name and values of params</returns>
IReadOnlyDictionary<string, string> GetTokenRequestParams();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public PopAuthenticationOperation(PoPAuthenticationConfiguration popAuthenticati
KeyId = Base64UrlHelpers.Encode(keyThumbprint);
}

public int TelemetryTokenType => (int)TokenType.Pop;
public int TelemetryTokenType => TelemetryTokenTypeConstants.Pop;

public string AuthorizationHeaderPrefix => Constants.PoPAuthHeaderPrefix;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Identity.Client.AuthScheme.PoP
//This is because tokens are cached in the broker instead so MSAL will rely on the broker's cache for silent requests.
internal class PopBrokerAuthenticationOperation : IAuthenticationOperation
{
public int TelemetryTokenType => (int)TokenType.Pop;
public int TelemetryTokenType => TelemetryTokenTypeConstants.Pop;

public string AuthorizationHeaderPrefix => Constants.PoPAuthHeaderPrefix;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using Microsoft.Identity.Client.Cache.Items;
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.OAuth2;

namespace Microsoft.Identity.Client.AuthScheme.SSHCertificates
Expand All @@ -29,7 +30,7 @@ public SSHCertAuthenticationOperation(string keyId, string jwk)
_jwk = jwk;
}

public int TelemetryTokenType => (int)TokenType.SshCert;
public int TelemetryTokenType => TelemetryTokenTypeConstants.SshCert;

public string AuthorizationHeaderPrefix =>
throw new MsalClientException(
Expand Down
37 changes: 0 additions & 37 deletions src/client/Microsoft.Identity.Client/AuthScheme/TokenType.cs

This file was deleted.

6 changes: 2 additions & 4 deletions src/client/Microsoft.Identity.Client/AuthenticationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,8 @@ internal AuthenticationResult(
authenticationScheme.FormatResult(this);
});

if (authenticationScheme.TelemetryTokenType == 5)
{
AuthenticationResultMetadata.DurationCreatingExtendedTokenInUs = measuredResultDuration.Microseconds;
}
AuthenticationResultMetadata.DurationCreatingExtendedTokenInUs = measuredResultDuration.Microseconds;
AuthenticationResultMetadata.TelemetryTokenType = authenticationScheme.TelemetryTokenType;
}

//Default constructor for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public AuthenticationResultMetadata(TokenSource tokenSource)
/// </summary>
public RegionDetails RegionDetails { get; set; }

/// <summary>
/// Represents the token type used for client telemetry only.
/// It is separate from the other token types as it is set locally rather than coming from the server.
/// </summary>
public int TelemetryTokenType { get; set; }

/// <summary>
/// Time, in microseconds, spent in the token creation of the extended token.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using Microsoft.Identity.Client.AuthScheme;
using Microsoft.Identity.Client.Cache.Items;
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.Utils;

namespace Microsoft.Identity.Client.Extensibility
Expand All @@ -19,7 +20,7 @@ public ExternalBoundTokenScheme(string keyId, string expectedTokenTypeFromEsts =
_tokenType = expectedTokenTypeFromEsts;
}

public int TelemetryTokenType => (int)TokenType.External;
public int TelemetryTokenType => TelemetryTokenTypeConstants.AtPop;

public string AuthorizationHeaderPrefix => _tokenType;

Expand Down
1 change: 1 addition & 0 deletions src/client/Microsoft.Identity.Client/Internal/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal static class Constants
public const string PoPAuthHeaderPrefix = "PoP";
public const string RequestConfirmation = "req_cnf";
public const string BearerAuthHeaderPrefix = "Bearer";
public const string SshCertAuthHeaderPrefix = "SshCert";

public const string ManagedIdentityClientId = "client_id";
public const string ManagedIdentityObjectId = "object_id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected override async Task<AuthenticationResult> ExecuteAsync(CancellationTok
// Use a linked token source, in case the original cancellation token source is disposed before this background task completes.
using var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
return GetAccessTokenAsync(tokenSource.Token, logger);
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent.ApiId,
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected override async Task<AuthenticationResult> ExecuteAsync(CancellationTok
// Use a linked token source, in case the original cancellation token source is disposed before this background task completes.
using var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
return GetAccessTokenAsync(tokenSource.Token, logger);
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent.ApiId,
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected override async Task<AuthenticationResult> ExecuteAsync(CancellationTok
// Use a linked token source, in case the original cancellation token source is disposed before this background task completes.
using var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
return RefreshRtOrFetchNewAccessTokenAsync(tokenSource.Token);
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent.ApiId,
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ private void LogFailureTelemetryToOtel(string errorCodeToLog, ApiEvent apiEvent,
apiEvent.ApiId,
apiEvent.CallerSdkApiId,
apiEvent.CallerSdkVersion,
cacheRefreshReason);
cacheRefreshReason,
apiEvent.TokenType);
}

private Tuple<string, string> ParseScopesForTelemetry()
Expand Down Expand Up @@ -245,7 +246,7 @@ private ApiEvent InitializeApiEvent(string accountId)
AuthenticationRequestParameters.RequestContext.ServiceBundle.Config.LegacyCacheCompatibilityEnabled;

apiEvent.CacheInfo = CacheRefreshReason.NotApplicable;
apiEvent.TokenType = (TokenType)AuthenticationRequestParameters.AuthenticationScheme.TelemetryTokenType;
apiEvent.TokenType = AuthenticationRequestParameters.AuthenticationScheme.TelemetryTokenType;
apiEvent.AssertionType = GetAssertionType();

UpdateCallerSdkDetails(apiEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public async Task<AuthenticationResult> ExecuteAsync(CancellationToken cancellat
// Use a linked token source, in case the original cancellation token source is disposed before this background task completes.
using var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
return RefreshRtOrFailAsync(tokenSource.Token);
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent.ApiId,
}, logger, ServiceBundle, AuthenticationRequestParameters.RequestContext.ApiEvent,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal static void ProcessFetchInBackground(
Func<Task<AuthenticationResult>> fetchAction,
ILoggerAdapter logger,
IServiceBundle serviceBundle,
ApiEvent.ApiIds apiId,
ApiEvent apiEvent,
string callerSdkId,
string callerSdkVersion)
{
Expand All @@ -96,13 +96,14 @@ internal static void ProcessFetchInBackground(
var authResult = await fetchAction().ConfigureAwait(false);
serviceBundle.PlatformProxy.OtelInstrumentation.IncrementSuccessCounter(
serviceBundle.PlatformProxy.GetProductName(),
apiId,
apiEvent.ApiId,
callerSdkId,
callerSdkVersion,
TokenSource.IdentityProvider,
CacheRefreshReason.ProactivelyRefreshed,
Cache.CacheLevel.None,
logger);
logger,
apiEvent.TokenType);
}
catch (MsalServiceException ex)
{
Expand All @@ -119,32 +120,35 @@ internal static void ProcessFetchInBackground(
serviceBundle.PlatformProxy.OtelInstrumentation.LogFailureMetrics(
serviceBundle.PlatformProxy.GetProductName(),
ex.ErrorCode,
apiId,
apiEvent.ApiId,
callerSdkId,
callerSdkVersion,
CacheRefreshReason.ProactivelyRefreshed);
CacheRefreshReason.ProactivelyRefreshed,
apiEvent.TokenType);
}
catch (OperationCanceledException ex)
{
logger.WarningPiiWithPrefix(ex, ProactiveRefreshCancellationError);
serviceBundle.PlatformProxy.OtelInstrumentation.LogFailureMetrics(
serviceBundle.PlatformProxy.GetProductName(),
ex.GetType().Name,
apiId,
apiEvent.ApiId,
callerSdkId,
callerSdkVersion,
CacheRefreshReason.ProactivelyRefreshed);
CacheRefreshReason.ProactivelyRefreshed,
apiEvent.TokenType);
}
catch (Exception ex)
{
logger.ErrorPiiWithPrefix(ex, ProactiveRefreshGeneralError);
serviceBundle.PlatformProxy.OtelInstrumentation.LogFailureMetrics(
serviceBundle.PlatformProxy.GetProductName(),
ex.GetType().Name,
apiId,
apiEvent.ApiId,
callerSdkId,
callerSdkVersion,
CacheRefreshReason.ProactivelyRefreshed);
CacheRefreshReason.ProactivelyRefreshed,
apiEvent.TokenType);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Identity.Client.Internal
{
internal static class TelemetryTokenTypeConstants
{
/// Bearer token type for telemetry.
public const int Bearer = 1;

/// Pop token type for telemetry.
public const int Pop = 2;

/// Ssh-cert token type for telemetry.
public const int SshCert = 3;

/// Token type for legacy AT POP
public const int AtPop = 4;

/// Extension token type for telemetry. This is used for custom token types added to MSAL as extensions through IAuthenticationOperation.
public const int Extension = 5;
}
}
Loading

0 comments on commit 2ad7d8d

Please sign in to comment.