Skip to content

Update ClientSettingsFactory.cs #345

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

Merged
merged 1 commit into from
May 13, 2025
Merged
Changes from all commits
Commits
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 @@ -44,17 +44,17 @@ public class ClientSettingsFactory : IClientSettingsFactory
private readonly Counter _settingsRefreshCounter = Metrics.CreateCounter(
"rbx_client_settings_refresh",
"Number of times the client settings have been refreshed.",
"mount", "path"
"application"
);
private readonly Counter _settingsWriteCounter = Metrics.CreateCounter(
"rbx_client_settings_write",
"Number of times the client settings have been written.",
"mount", "path"
"application"
);
private readonly Counter _settingsReadCounter = Metrics.CreateCounter(
"rbx_client_settings_read",
"Number of times the client settings have been read.",
"mount", "path"
"application"
);


Expand Down Expand Up @@ -264,7 +264,7 @@ private Dictionary<string, object> ParseSecrets(Secrets secrets, MetaData metada
private void DoCommit(string applicationName, Secrets data, MetaData metadata)
{
var serializedData = data.ToDictionary(k => k.Key, v => v.Value.ToString());
_settingsWriteCounter.WithLabels(_mount, _path).Inc();
_settingsWriteCounter.WithLabels(applicationName).Inc();

if (_settings.ClientSettingsViaVault)
{
Expand Down Expand Up @@ -299,7 +299,6 @@ _client.V1.Secrets.KeyValue.V2 .WriteSecretAsync(
private IDictionary<string, Secrets> DoRefresh(IDictionary<string, Secrets> oldSettings)
{
_logger?.Debug("Refreshing settings, FromVault = {0}", _settings.ClientSettingsViaVault);
_settingsRefreshCounter.WithLabels(_mount, _path).Inc();

var settings = new Dictionary<string, Secrets>();

Expand All @@ -311,6 +310,8 @@ private IDictionary<string, Secrets> DoRefresh(IDictionary<string, Secrets> oldS
// For each key, read the secret
foreach (var (applicationName, applicationData, applicationMetaData) in data)
{
_settingsRefreshCounter.WithLabels(applicationName).Inc();

var parsedSecrets = ParseSecrets(applicationData, applicationMetaData);
settings.Add(applicationName, parsedSecrets);
}
Expand Down Expand Up @@ -348,16 +349,7 @@ public IDictionary<string, Secrets> RawSettings
/// <inheritdoc cref="IClientSettingsFactory.Refresh"/>
public void Refresh()
{
_settingsCacheLock.EnterWriteLock();

try
{
_settingsCacheRefreshAhead.LazyValue.Refresh();
}
finally
{
_settingsCacheLock.ExitWriteLock();
}
_settingsCacheRefreshAhead.LazyValue.Refresh();
}

/// <inheritdoc cref="IClientSettingsFactory.GetSettingsForApplication(string, bool)"/>
Expand All @@ -368,7 +360,7 @@ public Secrets GetSettingsForApplication(string application, bool withDependenci

_settingsCacheLock.EnterReadLock();

_settingsReadCounter.WithLabels(_mount, _path).Inc();
_settingsReadCounter.WithLabels(application).Inc();

try
{
Expand Down Expand Up @@ -438,13 +430,11 @@ public T GetSettingForApplication<T>(string application, string setting, bool wi

try
{
var str = ((JsonElement)value).GetString();

return typeof(T) switch
{
Type t when t == typeof(string) => (T)(object)str.ToString(),
Type t when t == typeof(bool) => (T)(object)bool.Parse(str),
Type t when t == typeof(int) => (T)(object)long.Parse(str),
Type t when t == typeof(string) => (T)(object)value.ToString(),
Type t when t == typeof(bool) => (T)value,
Type t when t == typeof(int) => (T)value,
_ => throw new ArgumentException(string.Format("'{0}' is not a supported type!", typeof(T).Name)),
};
}
Expand Down