Skip to content
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

Add caller sdk id and version to telemetry server side and client side #4864

Merged
merged 24 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
83bf5c3
Add caller sdk id and version to telemetry server side and client side
neha-bhargava Jul 29, 2024
7d47da0
Merge branch 'main' into nebharg/ztaSupportSdkInfo
neha-bhargava Aug 5, 2024
a9f6234
Merge branch 'main' into nebharg/ztaSupportSdkInfo
neha-bhargava Aug 7, 2024
36a5709
Address comments
neha-bhargava Aug 7, 2024
92d495a
Address comments
neha-bhargava Aug 7, 2024
6f4fa3c
Add constraints and use WithClientName and WithClientVersion APIs
neha-bhargava Aug 12, 2024
89a52c8
Merge branch 'main' into nebharg/ztaSupportSdkInfo
neha-bhargava Aug 12, 2024
fe3f4e1
Update tests to expect null for client name and client version
neha-bhargava Aug 12, 2024
2581480
Merge branch 'main' into nebharg/ztaSupportSdkInfo
neha-bhargava Aug 13, 2024
def4dce
Update tests for telemetry
neha-bhargava Aug 13, 2024
b30ce09
Add tests
neha-bhargava Aug 14, 2024
f13b88d
Update src/client/Microsoft.Identity.Client/Platforms/Features/OpenTe…
neha-bhargava Aug 17, 2024
747b5e2
Update src/client/Microsoft.Identity.Client/Platforms/Features/OpenTe…
neha-bhargava Aug 17, 2024
476d338
Update src/client/Microsoft.Identity.Client/AppConfig/ApplicationConf…
neha-bhargava Aug 17, 2024
b0ebbff
Update src/client/Microsoft.Identity.Client/AppConfig/ApplicationConf…
neha-bhargava Aug 17, 2024
7366a19
Merge branch 'main' into nebharg/ztaSupportSdkInfo
neha-bhargava Sep 10, 2024
1e27ca2
Add caller sdk id and version tags to failed metrics as well
neha-bhargava Sep 11, 2024
331a6d2
Merge branch 'nebharg/ztaSupportSdkInfo' of https://github.com/AzureA…
neha-bhargava Sep 11, 2024
bd002f6
Merge branch 'main' into nebharg/ztaSupportSdkInfo
neha-bhargava Sep 11, 2024
b980895
Update src/client/Microsoft.Identity.Client/AppConfig/BaseAbstractApp…
neha-bhargava Sep 12, 2024
c1e38c2
Update src/client/Microsoft.Identity.Client/AppConfig/BaseAbstractApp…
neha-bhargava Sep 12, 2024
c93c8ce
Address comments
neha-bhargava Sep 12, 2024
7c8fc52
Merge branch 'main' into nebharg/ztaSupportSdkInfo
neha-bhargava Sep 12, 2024
ea52b17
Merge branch 'main' into nebharg/ztaSupportSdkInfo
neha-bhargava Sep 13, 2024
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 @@ -25,7 +25,7 @@ internal class AcquireTokenCommonParameters
public string Claims { get; set; }
public AuthorityInfo AuthorityOverride { get; set; }
public IAuthenticationScheme AuthenticationScheme { get; set; } = new BearerAuthenticationScheme();
public IDictionary<string, string> ExtraHttpHeaders { get; set; }
public IDictionary<string, string> ExtraHttpHeaders { get; } = new Dictionary<string, string>();
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
public PoPAuthenticationConfiguration PopAuthenticationConfiguration { get; set; }
public Func<OnBeforeTokenRequestData, Task> OnBeforeTokenRequestHandler { get; internal set; }
public X509Certificate2 MtlsCertificate { get; internal set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public static T WithExtraHttpHeaders<T>(
IDictionary<string, string> extraHttpHeaders)
where T : AbstractAcquireTokenParameterBuilder<T>
{
builder.CommonParameters.ExtraHttpHeaders = extraHttpHeaders;
foreach (var kvp in extraHttpHeaders)
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
{
builder.CommonParameters.ExtraHttpHeaders.Add(kvp.Key, kvp.Value);
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
}
return (T)builder;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ 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.ApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
}
}
catch (MsalServiceException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ 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.ApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
}
}
catch (MsalServiceException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ 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.ApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkApiId,
AuthenticationRequestParameters.RequestContext.ApiEvent.CallerSdkVersion);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public async Task<AuthenticationResult> RunAsync(CancellationToken cancellationT

UpdateTelemetry(measureDurationResult.Milliseconds + measureTelemetryDurationResult.Milliseconds, apiEvent, authenticationResult);
LogMetricsFromAuthResult(authenticationResult, AuthenticationRequestParameters.RequestContext.Logger);
LogSuccessTelemetryToOtel(authenticationResult, apiEvent.ApiId, measureDurationResult.Microseconds);
LogSuccessTelemetryToOtel(authenticationResult, apiEvent, measureDurationResult.Microseconds);

return authenticationResult;
}
Expand All @@ -117,12 +117,14 @@ public async Task<AuthenticationResult> RunAsync(CancellationToken cancellationT
}
}

private void LogSuccessTelemetryToOtel(AuthenticationResult authenticationResult, ApiEvent.ApiIds apiId, long durationInUs)
private void LogSuccessTelemetryToOtel(AuthenticationResult authenticationResult, ApiEvent apiEvent, long durationInUs)
{
// Log metrics
ServiceBundle.PlatformProxy.OtelInstrumentation.LogSuccessMetrics(
ServiceBundle.PlatformProxy.GetProductName(),
apiId,
apiEvent.ApiId,
apiEvent.CallerSdkApiId,
apiEvent.CallerSdkVersion,
GetCacheLevel(authenticationResult),
durationInUs,
authenticationResult.AuthenticationResultMetadata,
Expand Down Expand Up @@ -242,6 +244,18 @@ private ApiEvent InitializeApiEvent(string accountId)
apiEvent.TokenType = AuthenticationRequestParameters.AuthenticationScheme.TelemetryTokenType;
apiEvent.AssertionType = GetAssertionType();

if (AuthenticationRequestParameters.ExtraQueryParameters.ContainsKey("caller-sdk-id"))
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
{
apiEvent.CallerSdkApiId = AuthenticationRequestParameters.ExtraQueryParameters["caller-sdk-id"];
AuthenticationRequestParameters.ExtraQueryParameters.Remove("caller-sdk-id");
}

if (AuthenticationRequestParameters.ExtraQueryParameters.ContainsKey("caller-sdk-ver"))
{
apiEvent.CallerSdkVersion = AuthenticationRequestParameters.ExtraQueryParameters["caller-sdk-ver"];
AuthenticationRequestParameters.ExtraQueryParameters.Remove("caller-sdk-ver");
}

// Give derived classes the ability to add or modify fields in the telemetry as needed.
EnrichTelemetryApiEvent(apiEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ 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.ApiId,
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,9 @@ internal static void ProcessFetchInBackground(
Func<Task<AuthenticationResult>> fetchAction,
ILoggerAdapter logger,
IServiceBundle serviceBundle,
ApiEvent.ApiIds apiId)
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion)
{
_ = Task.Run(async () =>
{
Expand All @@ -95,6 +97,8 @@ internal static void ProcessFetchInBackground(
serviceBundle.PlatformProxy.OtelInstrumentation.IncrementSuccessCounter(
serviceBundle.PlatformProxy.GetProductName(),
apiId,
callerSdkId,
callerSdkVersion,
TokenSource.IdentityProvider,
CacheRefreshReason.ProactivelyRefreshed,
Cache.CacheLevel.None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public OtelInstrumentation()
public void LogSuccessMetrics(
string platform,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
CacheLevel cacheLevel,
long totalDurationInUs,
AuthenticationResultMetadata authResultMetadata,
Expand All @@ -100,6 +102,8 @@ public void LogSuccessMetrics(
IncrementSuccessCounter(
platform,
apiId,
callerSdkId,
callerSdkVersion,
authResultMetadata.TokenSource,
authResultMetadata.CacheRefreshReason,
cacheLevel,
Expand Down Expand Up @@ -151,6 +155,8 @@ public void LogSuccessMetrics(

public void IncrementSuccessCounter(string platform,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
TokenSource tokenSource,
CacheRefreshReason cacheRefreshReason,
CacheLevel cacheLevel,
Expand All @@ -162,6 +168,8 @@ public void IncrementSuccessCounter(string platform,
new(TelemetryConstants.MsalVersion, MsalIdHelper.GetMsalVersion()),
new(TelemetryConstants.Platform, platform),
new(TelemetryConstants.ApiId, apiId),
new(TelemetryConstants.CallerSdkId, callerSdkId),
new(TelemetryConstants.CallerSdkVersion, callerSdkVersion),
new(TelemetryConstants.TokenSource, tokenSource),
new(TelemetryConstants.CacheRefreshReason, cacheRefreshReason),
new(TelemetryConstants.CacheLevel, cacheLevel));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class HttpTelemetryManager : IHttpTelemetryManager
{
/// <summary>
/// Expected format: 5|api_id,cache_info,region_used,region_autodetection,region_outcome|platform_config
/// platform_config: is_token_cache_serialized,is_legacy_cache_enabled, token_type
/// platform_config: is_token_cache_serialized,is_legacy_cache_enabled, token_type, caller_sdk_id, caller_sdk_version
/// </summary>
public string GetCurrentRequestHeader(ApiEvent eventInProgress)
{
Expand Down Expand Up @@ -51,6 +51,10 @@ public string GetCurrentRequestHeader(ApiEvent eventInProgress)
sb.Append(TelemetryConstants.CommaDelimiter);
// Token type is to indicate 1 - bearer, 2 - pop, 3 - ssh-cert, 4 - external.
sb.Append(eventInProgress.TokenTypeString);
sb.Append(TelemetryConstants.CommaDelimiter);
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
sb.Append(eventInProgress.CallerSdkApiId);
sb.Append(TelemetryConstants.CommaDelimiter);
sb.Append(eventInProgress.CallerSdkVersion);

return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ public string TokenTypeString
public CacheLevel CacheLevel { get; set; }

public string MsalRuntimeTelemetry { get; set; }


public string CallerSdkApiId { get; set; } = "";
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved

public string CallerSdkVersion { get; set; } = "";

public static bool IsLongRunningObo(ApiIds apiId) => apiId == ApiIds.InitiateLongRunningObo || apiId == ApiIds.AcquireTokenInLongRunningObo;

public static bool IsOnBehalfOfRequest(ApiIds apiId) => apiId == ApiIds.AcquireTokenOnBehalfOf || IsLongRunningObo(apiId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ internal interface IOtelInstrumentation
internal void LogSuccessMetrics(
string platform,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
CacheLevel cacheLevel,
long totalDurationInUs,
AuthenticationResultMetadata authResultMetadata,
ILoggerAdapter logger);

internal void IncrementSuccessCounter(string platform,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
TokenSource tokenSource,
CacheRefreshReason cacheRefreshReason,
CacheLevel cacheLevel,
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal class NullOtelInstrumentation : IOtelInstrumentation
public void LogSuccessMetrics(
string platform,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
CacheLevel cacheLevel,
long totalDurationInUs,
AuthenticationResultMetadata authResultMetadata,
Expand All @@ -34,7 +36,9 @@ public void LogFailureMetrics(string platform,
}

void IOtelInstrumentation.IncrementSuccessCounter(string platform,
ApiEvent.ApiIds apiId,
ApiEvent.ApiIds apiId,
string callerSdkId,
string callerSdkVersion,
TokenSource tokenSource,
CacheRefreshReason cacheRefreshReason,
CacheLevel cacheLevel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ internal static class TelemetryConstants
public const string Platform = "Platform";
public const string ApiId = "ApiId";
public const string IsProactiveRefresh = "IsProactiveRefresh";
public const string CallerSdkId = "CallerSdkId";
neha-bhargava marked this conversation as resolved.
Show resolved Hide resolved
public const string CallerSdkVersion = "CallerSdkVersion";

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ private void AssertCurrentTelemetry(
Assert.AreEqual(3, telemetryCategories.Length);
Assert.AreEqual(1, telemetryCategories[0].Split(',').Length); // version
Assert.AreEqual(5, telemetryCategories[1].Split(',').Length); // api_id, cache_info, region_used, region_source, region_outcome
Assert.AreEqual(3, telemetryCategories[2].Split(',').Length); // platform_fields
Assert.AreEqual(5, telemetryCategories[2].Split(',').Length); // platform_fields

Assert.AreEqual(TelemetryConstants.HttpTelemetrySchemaVersion.ToString(), telemetryCategories[0]); // version

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ private void VerifyMetrics(int expectedMetricCount, List<Metric> exportedMetrics
expectedTags.Add(TelemetryConstants.MsalVersion);
expectedTags.Add(TelemetryConstants.Platform);
expectedTags.Add(TelemetryConstants.ApiId);
expectedTags.Add(TelemetryConstants.CallerSdkId);
expectedTags.Add(TelemetryConstants.CallerSdkVersion);
expectedTags.Add(TelemetryConstants.TokenSource);
expectedTags.Add(TelemetryConstants.CacheRefreshReason);
expectedTags.Add(TelemetryConstants.CacheLevel);
Expand Down Expand Up @@ -472,8 +474,6 @@ private void VerifyMetrics(int expectedMetricCount, List<Metric> exportedMetrics
Assert.Fail("Unexpected metrics logged.");
break;
}


}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private static void AssertCurrentTelemetry(
Assert.AreEqual(3, telemetryCategories.Length);
Assert.AreEqual(1, telemetryCategories[0].Split(',').Length); // version
Assert.AreEqual(5, telemetryCategories[1].Split(',').Length); // api_id, cache_info, region_used, region_source, region_outcome
Assert.AreEqual(3, telemetryCategories[2].Split(',').Length); // platform_fields
Assert.AreEqual(5, telemetryCategories[2].Split(',').Length); // platform_fields

Assert.AreEqual(TelemetryConstants.HttpTelemetrySchemaVersion.ToString(), telemetryCategories[0]); // version

Expand Down
Loading