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 12, 2024
1 parent 8fd3b88 commit d07e88b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 31 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 All @@ -357,21 +370,21 @@ public override void RequestRefresh()
/// <summary>
/// 12 hours is the default time interval that afterwards, <see cref="GetBaseConfigurationAsync(CancellationToken)"/> will obtain new configuration.
/// </summary>
public new static readonly TimeSpan DefaultAutomaticRefreshInterval = BaseConfigurationManager.DefaultAutomaticRefreshInterval;
public new static readonly TimeSpan DefaultAutomaticRefreshInterval = TimeSpan.FromMinutes(1);

/// <summary>
/// 5 minutes is the default time interval that must pass for <see cref="RequestRefresh"/> to obtain a new configuration.
/// </summary>
public new static readonly TimeSpan DefaultRefreshInterval = BaseConfigurationManager.DefaultRefreshInterval;
public new static readonly TimeSpan DefaultRefreshInterval = TimeSpan.FromMinutes(1);

/// <summary>
/// 5 minutes is the minimum value for automatic refresh. <see cref="MinimumAutomaticRefreshInterval"/> can not be set less than this value.
/// </summary>
public new static readonly TimeSpan MinimumAutomaticRefreshInterval = BaseConfigurationManager.MinimumAutomaticRefreshInterval;
public new static readonly TimeSpan MinimumAutomaticRefreshInterval = TimeSpan.FromMinutes(1);

/// <summary>
/// 1 second is the minimum time interval that must pass for <see cref="MinimumRefreshInterval"/> to obtain new configuration.
/// </summary>
public new static readonly TimeSpan MinimumRefreshInterval = BaseConfigurationManager.MinimumRefreshInterval;
public new static readonly TimeSpan MinimumRefreshInterval = TimeSpan.FromMinutes(1);
}
}
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 @@ -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
Loading

0 comments on commit d07e88b

Please sign in to comment.