-
Notifications
You must be signed in to change notification settings - Fork 689
feat(config): show non-default values on startup #11523
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
a80c95d
ae4df88
e930e1a
05d5210
4ff9cc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,10 +2,14 @@ | |||||||||
| // SPDX-License-Identifier: LGPL-3.0-only | ||||||||||
|
|
||||||||||
| using System; | ||||||||||
| using System.Collections; | ||||||||||
| using System.Collections.Concurrent; | ||||||||||
| using System.Collections.Generic; | ||||||||||
| using System.ComponentModel; | ||||||||||
| using System.Linq; | ||||||||||
| using System.Reflection; | ||||||||||
| using Nethermind.Core; | ||||||||||
| using Nethermind.Core.Extensions; | ||||||||||
|
|
||||||||||
| namespace Nethermind.Config; | ||||||||||
|
|
||||||||||
|
|
@@ -41,4 +45,97 @@ public static T GetDefaultValue<T>(this IConfig config, string propertyName) | |||||||||
| string? defaultValue = attribute.DefaultValue; | ||||||||||
| return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(defaultValue!)!; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// <summary> | ||||||||||
| /// Enumerates configuration properties whose current value differs from the | ||||||||||
| /// implementation's default. Useful for surfacing on startup which knobs the | ||||||||||
| /// operator has actually changed, rather than dumping every value. | ||||||||||
| /// </summary> | ||||||||||
| /// <remarks> | ||||||||||
| /// The default is taken from a freshly constructed instance of the | ||||||||||
| /// implementing type, so initializers and constructors are honoured exactly | ||||||||||
| /// as production wiring would do (no parsing of the <see cref="ConfigItemAttribute.DefaultValue"/> string). | ||||||||||
| /// </remarks> | ||||||||||
| /// <param name="configProvider">The provider to query.</param> | ||||||||||
| /// <returns> | ||||||||||
| /// Tuples of <c>(category, propertyName, currentValue, defaultValue)</c> for | ||||||||||
| /// every property whose current value is not equal to its default. | ||||||||||
| /// </returns> | ||||||||||
| public static IEnumerable<(string Category, string Name, object? CurrentValue, object? DefaultValue)> | ||||||||||
| GetNonDefaultValues(this IConfigProvider configProvider) | ||||||||||
| { | ||||||||||
| ArgumentNullException.ThrowIfNull(configProvider); | ||||||||||
|
|
||||||||||
| foreach (Type configInterface in TypeDiscovery | ||||||||||
| .FindNethermindBasedTypes(typeof(IConfig)) | ||||||||||
| .Where(static t => t.IsInterface)) | ||||||||||
| { | ||||||||||
| Type? implementation = configInterface.GetDirectInterfaceImplementation(); | ||||||||||
| if (implementation is null) continue; | ||||||||||
|
|
||||||||||
| IConfig current; | ||||||||||
| try | ||||||||||
| { | ||||||||||
| current = configProvider.GetConfig(configInterface); | ||||||||||
| } | ||||||||||
| catch (ArgumentException) | ||||||||||
| { | ||||||||||
| continue; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| IConfig fresh; | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Medium] Incomplete exception handling
Suggested change
|
||||||||||
| try | ||||||||||
| { | ||||||||||
| fresh = (IConfig)Activator.CreateInstance(implementation)!; | ||||||||||
| } | ||||||||||
| catch (MissingMethodException) | ||||||||||
| { | ||||||||||
| continue; | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [High] Sensitive data exposure Properties like Consider adding an
Suggested change
Or at a minimum filter out properties where the attribute is not present or |
||||||||||
| string category = GetCategoryName(configInterface) ?? string.Empty; | ||||||||||
|
|
||||||||||
| foreach (PropertyInfo property in configInterface.GetProperties()) | ||||||||||
| { | ||||||||||
| if (!property.CanRead) continue; | ||||||||||
|
|
||||||||||
| object? actual = property.GetValue(current); | ||||||||||
| object? defaultValue = property.GetValue(fresh); | ||||||||||
|
|
||||||||||
| if (!ValuesEqual(actual, defaultValue)) | ||||||||||
| { | ||||||||||
| yield return (category, property.Name, actual, defaultValue); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static bool ValuesEqual(object? a, object? b) | ||||||||||
| { | ||||||||||
| if (a is null) return b is null; | ||||||||||
| if (b is null) return false; | ||||||||||
| if (a is string || b is string) return a.Equals(b); | ||||||||||
| if (a is IEnumerable enumerableA && b is IEnumerable enumerableB) | ||||||||||
| { | ||||||||||
| IEnumerator iteratorA = enumerableA.GetEnumerator(); | ||||||||||
| IEnumerator iteratorB = enumerableB.GetEnumerator(); | ||||||||||
| try | ||||||||||
| { | ||||||||||
| while (true) | ||||||||||
| { | ||||||||||
| bool hasA = iteratorA.MoveNext(); | ||||||||||
| bool hasB = iteratorB.MoveNext(); | ||||||||||
| if (hasA != hasB) return false; | ||||||||||
| if (!hasA) return true; | ||||||||||
| if (!ValuesEqual(iteratorA.Current, iteratorB.Current)) return false; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| finally | ||||||||||
| { | ||||||||||
| (iteratorA as IDisposable)?.Dispose(); | ||||||||||
| (iteratorB as IDisposable)?.Dispose(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| return a.Equals(b); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,6 +186,24 @@ async Task<int> RunAsync(ParseResult parseResult, PluginLoader pluginLoader, Can | |
|
|
||
| EthereumJsonSerializer serializer = new(); | ||
|
|
||
| if (logger.IsInfo) | ||
| { | ||
| List<string> nonDefaultLines = []; | ||
| foreach ((string category, string name, object? currentValue, object? _) in configProvider.GetNonDefaultValues()) | ||
| { | ||
| nonDefaultLines.Add($" {category}.{name} = {serializer.Serialize(currentValue)}"); | ||
| } | ||
|
|
||
| if (nonDefaultLines.Count == 0) | ||
| { | ||
| logger.Info("Configuration: all values at defaults."); | ||
| } | ||
| else | ||
| { | ||
| logger.Info($"Configuration: {nonDefaultLines.Count} non-default value(s):\n{string.Join('\n', nonDefaultLines)}"); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because it is reflection based, it can slow down startup a bit, maybe worth it to run in |
||
| } | ||
|
|
||
| if (logger.IsDebug) | ||
| { | ||
| logger.Debug($"Nethermind configuration:\n{serializer.Serialize(initConfig, true)}"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Medium] LINQ where
foreachsufficesPer repo coding style (
coding-style.md): "No LINQ when a simple for/foreach works". Replace with an early-continue: