Skip to content

Commit

Permalink
add metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
westin-m committed Dec 13, 2024
1 parent 8fd3b88 commit 5e917c9
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration)
if (currentConfiguration != null)
{
_telemetryClient.IncrementConfigurationRefreshRequestCounter(
validationParameters.ConfigurationManager.MetadataAddress,
TelemetryConstants.Protocols.LKG);

validationParameters.ConfigurationManager.RequestRefresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,25 @@ public virtual async Task<T> GetConfigurationAsync(CancellationToken cancel)
result.ErrorMessage));

_telemetryClient.IncrementConfigurationRefreshRequestCounter(
MetadataAddress,
TelemetryConstants.Protocols.FirstRefresh,
ex);

throw LogHelper.LogExceptionMessage(ex);
}
}

_telemetryClient.IncrementConfigurationRefreshRequestCounter(TelemetryConstants.Protocols.FirstRefresh);
_telemetryClient.IncrementConfigurationRefreshRequestCounter(
MetadataAddress,
TelemetryConstants.Protocols.FirstRefresh);

UpdateConfiguration(configuration);
}
catch (Exception ex)
{
// counter for failure first time
fetchMetadataFailure = ex;
_telemetryClient.IncrementConfigurationRefreshRequestCounter(
MetadataAddress,
TelemetryConstants.Protocols.FirstRefresh,
ex);

Expand All @@ -232,7 +236,10 @@ public virtual async Task<T> GetConfigurationAsync(CancellationToken cancel)
{
if (Interlocked.CompareExchange(ref _configurationRetrieverState, ConfigurationRetrieverRunning, ConfigurationRetrieverIdle) == ConfigurationRetrieverIdle)
{
_telemetryClient.IncrementConfigurationRefreshRequestCounter(TelemetryConstants.Protocols.Automatic);
_telemetryClient.IncrementConfigurationRefreshRequestCounter(
MetadataAddress,
TelemetryConstants.Protocols.Automatic);

_ = Task.Run(UpdateCurrentConfiguration, CancellationToken.None);
}
}
Expand Down Expand Up @@ -271,7 +278,9 @@ private void UpdateCurrentConfiguration()
CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();

var elapsedTime = _timeProvider.GetElapsedTime(startTimestamp);
_telemetryClient.LogConfigurationRetrievalDuration(elapsedTime);
_telemetryClient.LogConfigurationRetrievalDuration(
MetadataAddress,
elapsedTime);

if (_configValidator == null)
{
Expand All @@ -295,6 +304,7 @@ private void UpdateCurrentConfiguration()
{
var elapsedTime = _timeProvider.GetElapsedTime(startTimestamp);
_telemetryClient.LogConfigurationRetrievalDuration(
MetadataAddress,
elapsedTime,
ex);

Expand Down Expand Up @@ -342,7 +352,10 @@ public override void RequestRefresh()
{
DateTimeOffset now = DateTimeOffset.UtcNow;

_telemetryClient.IncrementConfigurationRefreshRequestCounter(TelemetryConstants.Protocols.Direct);
_telemetryClient.IncrementConfigurationRefreshRequestCounter(
MetadataAddress,
TelemetryConstants.Protocols.Direct);

if (now >= DateTimeUtil.Add(_lastRequestRefresh.UtcDateTime, RefreshInterval) || _isFirstRefreshRequest)
{
_isFirstRefreshRequest = false;
Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.IdentityModel.Tokens/InternalAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ Microsoft.IdentityModel.Tokens.SecurityTokenInvalidOperationException.SecurityTo
Microsoft.IdentityModel.Tokens.SignatureValidationError
Microsoft.IdentityModel.Tokens.SignatureValidationError.InnerValidationError.get -> Microsoft.IdentityModel.Tokens.ValidationError
Microsoft.IdentityModel.Tokens.SignatureValidationError.SignatureValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType validationFailureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, Microsoft.IdentityModel.Tokens.ValidationError innerValidationError = null, System.Exception innerException = null) -> void
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation.IncrementConfigurationRefreshRequestCounter(string metadataAddress, string operationStatus) -> void
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation.IncrementConfigurationRefreshRequestCounter(string metadataAddress, string operationStatus, System.Exception exception) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.IncrementConfigurationRefreshRequestCounter(string metadataAddress, string operationStatus) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.IncrementConfigurationRefreshRequestCounter(string metadataAddress, string operationStatus, System.Exception exception) -> void
Microsoft.IdentityModel.Tokens.TokenReplayValidationError
Microsoft.IdentityModel.Tokens.TokenReplayValidationError.ExpirationTime.get -> System.DateTime?
Microsoft.IdentityModel.Tokens.TokenReplayValidationError.TokenReplayValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType validationFailureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, System.DateTime? expirationTime, System.Exception innerException = null) -> void
Microsoft.IdentityModel.Tokens.Telemetry.IdentityModelTelemetry
Microsoft.IdentityModel.Tokens.Telemetry.IdentityModelTelemetry.IdentityModelTelemetry() -> void
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation.IncrementConfigurationRefreshRequestCounter(string operationStatus) -> void
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation.IncrementConfigurationRefreshRequestCounter(string operationStatus, System.Exception exception) -> void
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation.LogConfigurationRetrievalDuration(System.TimeSpan operationDuration) -> void
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation.LogConfigurationRetrievalDuration(System.TimeSpan operationDuration, System.Exception exception) -> void
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation.LogConfigurationRetrievalDuration(string metadataAddress, System.TimeSpan operationDuration) -> void
Microsoft.IdentityModel.Tokens.Telemetry.ITelemetryInstrumentation.LogConfigurationRetrievalDuration(string metadataAddress, System.TimeSpan operationDuration, System.Exception exception) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryConstants
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryConstants.Protocols
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.IncrementConfigurationRefreshRequestCounter(string operationStatus) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.IncrementConfigurationRefreshRequestCounter(string operationStatus, System.Exception exception) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.LogConfigurationRetrievalDuration(System.TimeSpan operationDuration) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.LogConfigurationRetrievalDuration(System.TimeSpan operationDuration, System.Exception exception) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.LogConfigurationRetrievalDuration(string metadataAddress, System.TimeSpan operationDuration) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.LogConfigurationRetrievalDuration(string metadataAddress, System.TimeSpan operationDuration, System.Exception exception) -> void
Microsoft.IdentityModel.Tokens.Telemetry.TelemetryInstrumentation.TelemetryInstrumentation() -> void
Microsoft.IdentityModel.Tokens.TokenTypeValidationError
Microsoft.IdentityModel.Tokens.TokenTypeValidationError.InvalidTokenType.get -> string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ namespace Microsoft.IdentityModel.Tokens.Telemetry
internal interface ITelemetryInstrumentation
{
internal void LogConfigurationRetrievalDuration(
string metadataAddress,
TimeSpan operationDuration);

internal void LogConfigurationRetrievalDuration(
string metadataAddress,
TimeSpan operationDuration,
Exception exception);

internal void IncrementConfigurationRefreshRequestCounter(
string metadataAddress,
string operationStatus);

internal void IncrementConfigurationRefreshRequestCounter(
string metadataAddress,
string operationStatus,
Exception exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,51 @@ namespace Microsoft.IdentityModel.Tokens.Telemetry
{
internal class TelemetryInstrumentation : ITelemetryInstrumentation
{
static TagList ClientVerTagList = new()
{
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer }
};

public void IncrementConfigurationRefreshRequestCounter(string operationStatus)
public void IncrementConfigurationRefreshRequestCounter(string metadataAddress, string operationStatus)
{
var tagList = new TagList()
{
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.MetadataAddressTag, metadataAddress },
{ TelemetryConstants.OperationStatusTag, operationStatus }
};

IdentityModelTelemetry.IncrementConfigurationRefreshRequestCounter(tagList);
}

public void IncrementConfigurationRefreshRequestCounter(string operationStatus, Exception exception)
public void IncrementConfigurationRefreshRequestCounter(string metadataAddress, string operationStatus, Exception exception)
{
var tagList = new TagList()
{
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.MetadataAddressTag, metadataAddress },
{ TelemetryConstants.OperationStatusTag, operationStatus },
{ TelemetryConstants.ExceptionTypeTag, exception.GetType().ToString() }
};

IdentityModelTelemetry.IncrementConfigurationRefreshRequestCounter(tagList);
}

public void LogConfigurationRetrievalDuration(TimeSpan operationDuration)
public void LogConfigurationRetrievalDuration(string metadataAddress, TimeSpan operationDuration)
{

var tagList = new TagList()
{
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.MetadataAddressTag, metadataAddress },
};

long durationInMilliseconds = (long)operationDuration.TotalMilliseconds;
IdentityModelTelemetry.RecordConfigurationRetrievalDurationHistogram(durationInMilliseconds, ClientVerTagList);
IdentityModelTelemetry.RecordConfigurationRetrievalDurationHistogram(durationInMilliseconds, tagList);
}

public void LogConfigurationRetrievalDuration(TimeSpan operationDuration, Exception exception)
public void LogConfigurationRetrievalDuration(string metadataAddress, TimeSpan operationDuration, Exception exception)
{
var tagList = new TagList()
{
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.MetadataAddressTag, metadataAddress },
{ TelemetryConstants.ExceptionTypeTag, exception.GetType().ToString() }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ private ClaimsPrincipal ValidateToken(string token, JwtSecurityToken outerToken,
if (currentConfiguration != null)
{
_telemetryClient.IncrementConfigurationRefreshRequestCounter(
validationParameters.ConfigurationManager.MetadataAddress,
TelemetryConstants.Protocols.LKG);

validationParameters.ConfigurationManager.RequestRefresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.IdentityModel.JsonWebTokens.Tests
public class JsonWebTokenHandlerTelemetryTests
{
[Fact]
public async Task ValidateJWSWithConfigAsync()
public async Task ValidateJWSWithConfigAsync_ExpectedTagsExist()
{
var invalidIssuerConfig = new OpenIdConnectConfiguration()
{
Expand Down Expand Up @@ -57,7 +57,9 @@ public async Task ValidateJWSWithConfigAsync()

var expectedCounterTagList = new Dictionary<string, object>
{
// metadata address is null because the configuration manager is made using an invalid config to trigger an exception
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.MetadataAddressTag, null },
{ TelemetryConstants.OperationStatusTag, TelemetryConstants.Protocols.LKG }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public async Task RequestRefresh_ExpectedTagsExist()
{
// arrange
var testTelemetryClient = new MockTelemetryInstrumentation();
// mock up retirever to return something quickly
var configurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(
OpenIdConfigData.AccountsGoogle,
new OpenIdConnectConfigurationRetriever(),
Expand All @@ -43,12 +42,14 @@ public async Task RequestRefresh_ExpectedTagsExist()
// assert
var expectedCounterTagList = new Dictionary<string, object>
{
{ TelemetryConstants.MetadataAddressTag, OpenIdConfigData.AccountsGoogle },
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.OperationStatusTag, TelemetryConstants.Protocols.Direct },
};

var expectedHistogramTagList = new Dictionary<string, object>
{
{ TelemetryConstants.MetadataAddressTag, OpenIdConfigData.AccountsGoogle },
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer }
};

Expand Down Expand Up @@ -99,6 +100,7 @@ public static TheoryData<ConfigurationManagerTelemetryTheoryData<OpenIdConnectCo
ConfigurationValidator = new OpenIdConnectConfigurationValidator(),
ExpectedTagList = new Dictionary<string, object>
{
{ TelemetryConstants.MetadataAddressTag, OpenIdConfigData.AccountsGoogle },
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.OperationStatusTag, TelemetryConstants.Protocols.FirstRefresh },
}
Expand All @@ -109,6 +111,7 @@ public static TheoryData<ConfigurationManagerTelemetryTheoryData<OpenIdConnectCo
ConfigurationValidator = new OpenIdConnectConfigurationValidator(),
ExpectedTagList = new Dictionary<string, object>
{
{ TelemetryConstants.MetadataAddressTag, OpenIdConfigData.HttpsBadUri },
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.OperationStatusTag, TelemetryConstants.Protocols.FirstRefresh },
{ TelemetryConstants.ExceptionTypeTag, new IOException().GetType().ToString() },
Expand All @@ -122,6 +125,7 @@ public static TheoryData<ConfigurationManagerTelemetryTheoryData<OpenIdConnectCo
ConfigurationValidator = new OpenIdConnectConfigurationValidator() { MinimumNumberOfKeys = 3 },
ExpectedTagList = new Dictionary<string, object>
{
{ TelemetryConstants.MetadataAddressTag, OpenIdConfigData.JsonFile },
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.OperationStatusTag, TelemetryConstants.Protocols.FirstRefresh },
{ TelemetryConstants.ExceptionTypeTag, new InvalidConfigurationException().GetType().ToString() },
Expand All @@ -134,6 +138,7 @@ public static TheoryData<ConfigurationManagerTelemetryTheoryData<OpenIdConnectCo
SyncAfter = DateTime.UtcNow - TimeSpan.FromDays(2),
ExpectedTagList = new Dictionary<string, object>
{
{ TelemetryConstants.MetadataAddressTag, OpenIdConfigData.AADCommonUrl },
{ TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer },
{ TelemetryConstants.OperationStatusTag, TelemetryConstants.Protocols.Automatic },
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,31 @@ public void ClearExportedItems()
ExportedItems.Clear();
}

public void IncrementConfigurationRefreshRequestCounter(string operationStatus)
public void IncrementConfigurationRefreshRequestCounter(string metadataAddress, string operationStatus)
{
ExportedItems.Add(TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer);
ExportedItems.Add(TelemetryConstants.MetadataAddressTag, metadataAddress);
ExportedItems.Add(TelemetryConstants.OperationStatusTag, operationStatus);
}

public void IncrementConfigurationRefreshRequestCounter(string operationStatus, Exception exception)
public void IncrementConfigurationRefreshRequestCounter(string metadataAddress, string operationStatus, Exception exception)
{
ExportedItems.Add(TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer);
ExportedItems.Add(TelemetryConstants.MetadataAddressTag, metadataAddress);
ExportedItems.Add(TelemetryConstants.OperationStatusTag, operationStatus);
ExportedItems.Add(TelemetryConstants.ExceptionTypeTag, exception.GetType().ToString());
}

public void LogConfigurationRetrievalDuration(TimeSpan operationDuration)
public void LogConfigurationRetrievalDuration(string metadataAddress, TimeSpan operationDuration)
{
ExportedHistogramItems.Add(TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer);
ExportedHistogramItems.Add(TelemetryConstants.MetadataAddressTag, metadataAddress);
}

public void LogConfigurationRetrievalDuration(TimeSpan operationDuration, Exception exception)
public void LogConfigurationRetrievalDuration(string metadataAddress, TimeSpan operationDuration, Exception exception)
{
ExportedHistogramItems.Add(TelemetryConstants.IdentityModelVersionTag, IdentityModelTelemetryUtil.ClientVer);
ExportedHistogramItems.Add(TelemetryConstants.MetadataAddressTag, metadataAddress);
ExportedHistogramItems.Add(TelemetryConstants.ExceptionTypeTag, exception.GetType().ToString());
}
}
Expand Down
Loading

0 comments on commit 5e917c9

Please sign in to comment.