From 704ad9abaeeb92ccbbe90168e9da5042d550124e Mon Sep 17 00:00:00 2001 From: Ruben Buniatyan Date: Thu, 31 Oct 2024 20:53:20 +0100 Subject: [PATCH 1/7] Remove `NETHERMIND_URL` --- ...figProvider_FindIncorrectSettings_Tests.cs | 1 - .../Nethermind.Config/EnvConfigSource.cs | 2 +- .../Nethermind.Config/INoCategoryConfig.cs | 3 --- .../Nethermind.Config/NoCategoryConfig.cs | 1 - .../JsonRpcUrlCollectionTests.cs | 25 ------------------- .../JsonRpcUrlCollection.cs | 16 ------------ 6 files changed, 1 insertion(+), 47 deletions(-) diff --git a/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs b/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs index d6c489d6eb9..98e96d0791a 100644 --- a/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs +++ b/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs @@ -47,7 +47,6 @@ public void NoCategorySettings() { "NETHERMIND_MONITORING_JOB", "nethermindJob" }, { "NETHERMIND_MONITORING_GROUP", "nethermindGroup" }, { "NETHERMIND_ENODE_IPADDRESS", "1.2.3.4" }, - { "NETHERMIND_URL", "http://test:80" }, { "NETHERMIND_CORS_ORIGINS", "*" }, { "NETHERMIND_CONFIG", "test2.json" }, { "NETHERMIND_XYZ", "xyz" }, // not existing, should get error diff --git a/src/Nethermind/Nethermind.Config/EnvConfigSource.cs b/src/Nethermind/Nethermind.Config/EnvConfigSource.cs index fd28c23b195..88443b89f57 100644 --- a/src/Nethermind/Nethermind.Config/EnvConfigSource.cs +++ b/src/Nethermind/Nethermind.Config/EnvConfigSource.cs @@ -43,7 +43,7 @@ public EnvConfigSource(IEnvironment environmentWrapper) return (null, null); } - // variables like "NETHERMIND_URL", "NETHERMIND_CONFIG" ... + // variables like "NETHERMIND_CONFIG" if (a.Length == 2) { return (null, a[1]); diff --git a/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs b/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs index d1b038857f9..ec3eae7971d 100644 --- a/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs +++ b/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs @@ -18,9 +18,6 @@ public interface INoCategoryConfig : IConfig [ConfigItem(Description = "Sets the external IP for the node.", EnvironmentVariable = "NETHERMIND_ENODE_IPADDRESS")] public string EnodeIpAddress { get; set; } - [ConfigItem(Description = "Defines default URL for JSON RPC.", EnvironmentVariable = "NETHERMIND_URL")] - public string Url { get; set; } - [ConfigItem(Description = "Defines CORS origins for JSON RPC.", DefaultValue = "*", EnvironmentVariable = "NETHERMIND_CORS_ORIGINS")] public string CorsOrigins { get; set; } diff --git a/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs b/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs index e231c770f24..e3fb04cc7ad 100644 --- a/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs +++ b/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs @@ -9,7 +9,6 @@ public class NoCategoryConfig : INoCategoryConfig public string MonitoringJob { get; set; } public string MonitoringGroup { get; set; } public string EnodeIpAddress { get; set; } - public string Url { get; set; } public string CorsOrigins { get; set; } public string CliSwitchLocal { get; set; } } diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcUrlCollectionTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcUrlCollectionTests.cs index 1da47282ba1..7ace8f6392f 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcUrlCollectionTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcUrlCollectionTests.cs @@ -1,7 +1,6 @@ // SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only -using System; using System.Collections.Generic; using Nethermind.Logging; using Nethermind.JsonRpc.Modules; @@ -20,12 +19,6 @@ public void Initialize() private string[] _enabledModules = null!; - [TearDown] - public void TearDown() - { - Environment.SetEnvironmentVariable("NETHERMIND_URL", null, EnvironmentVariableTarget.Process); - } - [Test] public void Empty_when_disabled() { @@ -50,24 +43,6 @@ public void Contains_single_default_url() }, Is.EquivalentTo(urlCollection)); } - [Test] - public void Contains_single_default_url_overridden_by_environment_variable() - { - Environment.SetEnvironmentVariable("NETHERMIND_URL", "http://localhost:1234", EnvironmentVariableTarget.Process); - - JsonRpcConfig jsonRpcConfig = new JsonRpcConfig() - { - Enabled = true, - EnabledModules = _enabledModules - }; - - JsonRpcUrlCollection urlCollection = new JsonRpcUrlCollection(Substitute.For(), jsonRpcConfig, true); - Assert.That(new Dictionary() - { - { 1234, new JsonRpcUrl("http", "localhost", 1234, RpcEndpoint.Http | RpcEndpoint.Ws, false, _enabledModules) } - }, Is.EquivalentTo(urlCollection)); - } - [Test] public void Contains_multiple_default_urls_with_different_ws_port() { diff --git a/src/Nethermind/Nethermind.JsonRpc/JsonRpcUrlCollection.cs b/src/Nethermind/Nethermind.JsonRpc/JsonRpcUrlCollection.cs index 90d85e6c844..7b898182758 100644 --- a/src/Nethermind/Nethermind.JsonRpc/JsonRpcUrlCollection.cs +++ b/src/Nethermind/Nethermind.JsonRpc/JsonRpcUrlCollection.cs @@ -12,8 +12,6 @@ namespace Nethermind.JsonRpc { public class JsonRpcUrlCollection : Dictionary, IJsonRpcUrlCollection { - private const string NethermindUrlVariable = "NETHERMIND_URL"; - private readonly ILogger _logger; private readonly IJsonRpcConfig _jsonRpcConfig; @@ -34,20 +32,6 @@ private void BuildUrls(bool includeWebSockets) { bool HasEngineApi = _jsonRpcConfig.EnabledModules.Any(m => m.Equals(ModuleType.Engine, StringComparison.InvariantCultureIgnoreCase)); JsonRpcUrl defaultUrl = new(Uri.UriSchemeHttp, _jsonRpcConfig.Host, _jsonRpcConfig.Port, RpcEndpoint.Http, HasEngineApi, _jsonRpcConfig.EnabledModules, HasEngineApi ? SocketClient.MAX_REQUEST_BODY_SIZE_FOR_ENGINE_API : _jsonRpcConfig.MaxRequestBodySize); - string environmentVariableUrl = Environment.GetEnvironmentVariable(NethermindUrlVariable); - if (!string.IsNullOrWhiteSpace(environmentVariableUrl)) - { - if (Uri.TryCreate(environmentVariableUrl, UriKind.Absolute, out Uri? uri)) - { - defaultUrl.Scheme = uri.Scheme; - defaultUrl.Host = uri.Host; - defaultUrl.Port = !uri.IsDefaultPort ? uri.Port : defaultUrl.Port; - } - else - { - if (_logger.IsWarn) _logger.Warn($"Environment variable '{NethermindUrlVariable}' value '{environmentVariableUrl}' is not valid JSON RPC URL, using default url : '{defaultUrl}'"); - } - } Add(defaultUrl.Port, defaultUrl); From 29368c8e732ed8d46e4c9a8ce76869a01fdb801c Mon Sep 17 00:00:00 2001 From: Ruben Buniatyan Date: Thu, 31 Oct 2024 20:59:45 +0100 Subject: [PATCH 2/7] Remove `NETHERMIND_ENODE_IPADDRESS` --- ...figProvider_FindIncorrectSettings_Tests.cs | 1 - .../Nethermind.Config/INoCategoryConfig.cs | 3 --- .../Nethermind.Config/NoCategoryConfig.cs | 1 - .../IP/EnvironmentVariableIPSource.cs | 19 ------------------- .../Nethermind.Network/IPResolver.cs | 1 - 5 files changed, 25 deletions(-) delete mode 100644 src/Nethermind/Nethermind.Network/IP/EnvironmentVariableIPSource.cs diff --git a/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs b/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs index 98e96d0791a..ea8fc1d9a4d 100644 --- a/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs +++ b/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs @@ -46,7 +46,6 @@ public void NoCategorySettings() { "NETHERMIND_CLI_SWITCH_LOCAL", "http://localhost:80" }, { "NETHERMIND_MONITORING_JOB", "nethermindJob" }, { "NETHERMIND_MONITORING_GROUP", "nethermindGroup" }, - { "NETHERMIND_ENODE_IPADDRESS", "1.2.3.4" }, { "NETHERMIND_CORS_ORIGINS", "*" }, { "NETHERMIND_CONFIG", "test2.json" }, { "NETHERMIND_XYZ", "xyz" }, // not existing, should get error diff --git a/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs b/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs index ec3eae7971d..2075eb5c5ab 100644 --- a/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs +++ b/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs @@ -15,9 +15,6 @@ public interface INoCategoryConfig : IConfig [ConfigItem(Description = "Sets the default group name for metrics monitoring.", EnvironmentVariable = "NETHERMIND_MONITORING_GROUP")] public string MonitoringGroup { get; set; } - [ConfigItem(Description = "Sets the external IP for the node.", EnvironmentVariable = "NETHERMIND_ENODE_IPADDRESS")] - public string EnodeIpAddress { get; set; } - [ConfigItem(Description = "Defines CORS origins for JSON RPC.", DefaultValue = "*", EnvironmentVariable = "NETHERMIND_CORS_ORIGINS")] public string CorsOrigins { get; set; } diff --git a/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs b/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs index e3fb04cc7ad..56d20af3d2b 100644 --- a/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs +++ b/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs @@ -8,7 +8,6 @@ public class NoCategoryConfig : INoCategoryConfig public string Config { get; set; } = null; public string MonitoringJob { get; set; } public string MonitoringGroup { get; set; } - public string EnodeIpAddress { get; set; } public string CorsOrigins { get; set; } public string CliSwitchLocal { get; set; } } diff --git a/src/Nethermind/Nethermind.Network/IP/EnvironmentVariableIPSource.cs b/src/Nethermind/Nethermind.Network/IP/EnvironmentVariableIPSource.cs deleted file mode 100644 index 26fbeaaf165..00000000000 --- a/src/Nethermind/Nethermind.Network/IP/EnvironmentVariableIPSource.cs +++ /dev/null @@ -1,19 +0,0 @@ -// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited -// SPDX-License-Identifier: LGPL-3.0-only - -using System; -using System.Net; -using System.Threading.Tasks; - -namespace Nethermind.Network.IP -{ - class EnvironmentVariableIPSource : IIPSource - { - public Task<(bool, IPAddress)> TryGetIP() - { - string externalIpSetInEnv = Environment.GetEnvironmentVariable("NETHERMIND_ENODE_IPADDRESS"); - bool success = IPAddress.TryParse(externalIpSetInEnv, out IPAddress ipAddress); - return Task.FromResult((success, ipAddress)); - } - } -} diff --git a/src/Nethermind/Nethermind.Network/IPResolver.cs b/src/Nethermind/Nethermind.Network/IPResolver.cs index 7aee676aa3e..87779bedfa2 100644 --- a/src/Nethermind/Nethermind.Network/IPResolver.cs +++ b/src/Nethermind/Nethermind.Network/IPResolver.cs @@ -54,7 +54,6 @@ private async Task InitializeExternalIp() { IEnumerable GetIPSources() { - yield return new EnvironmentVariableIPSource(); yield return new NetworkConfigExternalIPSource(_networkConfig, _logManager); yield return new WebIPSource("http://ipv4.icanhazip.com", _logManager); yield return new WebIPSource("http://ipv4bot.whatismyipaddress.com", _logManager); From 22d36b7e627851bb51a69c0defc994bf5c1a1f8e Mon Sep 17 00:00:00 2001 From: Ruben Buniatyan Date: Thu, 31 Oct 2024 22:15:23 +0100 Subject: [PATCH 3/7] Replace `NETHERMIND_CORS_ORIGINS` with `IJsonRpc.CorsOrigins` --- .../ConfigProvider_FindIncorrectSettings_Tests.cs | 1 - src/Nethermind/Nethermind.Config/INoCategoryConfig.cs | 3 --- src/Nethermind/Nethermind.Config/NoCategoryConfig.cs | 1 - src/Nethermind/Nethermind.JsonRpc/IJsonRpcConfig.cs | 3 +++ src/Nethermind/Nethermind.JsonRpc/JsonRpcConfig.cs | 2 ++ src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs | 10 +++++----- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs b/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs index ea8fc1d9a4d..63d06109011 100644 --- a/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs +++ b/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs @@ -46,7 +46,6 @@ public void NoCategorySettings() { "NETHERMIND_CLI_SWITCH_LOCAL", "http://localhost:80" }, { "NETHERMIND_MONITORING_JOB", "nethermindJob" }, { "NETHERMIND_MONITORING_GROUP", "nethermindGroup" }, - { "NETHERMIND_CORS_ORIGINS", "*" }, { "NETHERMIND_CONFIG", "test2.json" }, { "NETHERMIND_XYZ", "xyz" }, // not existing, should get error { "QWER", "qwerty" } // not Nethermind setting, no error diff --git a/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs b/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs index 2075eb5c5ab..e50dcb39cd1 100644 --- a/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs +++ b/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs @@ -15,9 +15,6 @@ public interface INoCategoryConfig : IConfig [ConfigItem(Description = "Sets the default group name for metrics monitoring.", EnvironmentVariable = "NETHERMIND_MONITORING_GROUP")] public string MonitoringGroup { get; set; } - [ConfigItem(Description = "Defines CORS origins for JSON RPC.", DefaultValue = "*", EnvironmentVariable = "NETHERMIND_CORS_ORIGINS")] - public string CorsOrigins { get; set; } - [ConfigItem(Description = "Defines host value for CLI function \"switchLocal\".", DefaultValue = "http://localhost", EnvironmentVariable = "NETHERMIND_CLI_SWITCH_LOCAL")] public string CliSwitchLocal { get; set; } } diff --git a/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs b/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs index 56d20af3d2b..c3f15bb98a0 100644 --- a/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs +++ b/src/Nethermind/Nethermind.Config/NoCategoryConfig.cs @@ -8,6 +8,5 @@ public class NoCategoryConfig : INoCategoryConfig public string Config { get; set; } = null; public string MonitoringJob { get; set; } public string MonitoringGroup { get; set; } - public string CorsOrigins { get; set; } public string CliSwitchLocal { get; set; } } diff --git a/src/Nethermind/Nethermind.JsonRpc/IJsonRpcConfig.cs b/src/Nethermind/Nethermind.JsonRpc/IJsonRpcConfig.cs index 301b21f850e..a202fcf5986 100644 --- a/src/Nethermind/Nethermind.JsonRpc/IJsonRpcConfig.cs +++ b/src/Nethermind/Nethermind.JsonRpc/IJsonRpcConfig.cs @@ -166,4 +166,7 @@ public interface IJsonRpcConfig : IConfig [ConfigItem(Description = "The error margin used in the `eth_estimateGas` JSON-RPC method, in basis points.", DefaultValue = "150")] int EstimateErrorMargin { get; set; } + + [ConfigItem(Description = "The JSON-RPC server CORS origins.", DefaultValue = "*")] + string[] CorsOrigins { get; set; } } diff --git a/src/Nethermind/Nethermind.JsonRpc/JsonRpcConfig.cs b/src/Nethermind/Nethermind.JsonRpc/JsonRpcConfig.cs index 26d3d32760e..99260c72927 100644 --- a/src/Nethermind/Nethermind.JsonRpc/JsonRpcConfig.cs +++ b/src/Nethermind/Nethermind.JsonRpc/JsonRpcConfig.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Collections.Generic; using System.Linq; using Nethermind.Core.Extensions; using Nethermind.JsonRpc.Modules; @@ -57,6 +58,7 @@ public int WebSocketsPort public long? MaxBatchResponseBodySize { get; set; } = 32.MiB(); public long? MaxSimulateBlocksCap { get; set; } = 256; public int EstimateErrorMargin { get; set; } = 150; + public string[] CorsOrigins { get; set; } = ["*"]; }; }; diff --git a/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs b/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs index 7e970cda02d..c69e0992dc3 100644 --- a/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs +++ b/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs @@ -22,7 +22,6 @@ using Nethermind.Api; using Nethermind.Config; using Nethermind.Core.Authentication; -using Nethermind.Core.Extensions; using Nethermind.Core.Resettables; using Nethermind.HealthChecks; using Nethermind.JsonRpc; @@ -57,9 +56,10 @@ public void ConfigureServices(IServiceCollection services) }); Bootstrap.Instance.RegisterJsonRpcServices(services); - string corsOrigins = Environment.GetEnvironmentVariable("NETHERMIND_CORS_ORIGINS") ?? "*"; - services.AddCors(c => c.AddPolicy("Cors", - p => p.AllowAnyMethod().AllowAnyHeader().WithOrigins(corsOrigins))); + services.AddCors(options => options.AddDefaultPolicy(builder => builder + .AllowAnyMethod() + .AllowAnyHeader() + .WithOrigins(jsonRpcConfig.CorsOrigins))); services.AddResponseCompression(options => { @@ -79,8 +79,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJsonRpc app.UseDeveloperExceptionPage(); } - app.UseCors("Cors"); app.UseRouting(); + app.UseCors(); app.UseResponseCompression(); IConfigProvider? configProvider = app.ApplicationServices.GetService(); From cdc6a24b2ebc02eed934dfd3d758dcbdcc3baf21 Mon Sep 17 00:00:00 2001 From: Ruben Buniatyan Date: Fri, 1 Nov 2024 20:05:33 +0100 Subject: [PATCH 4/7] Move `NETHERMIND_MONITORING_GROUP` and `NETHERMIND_MONITORING_JOB` to `IMetricsConfig` --- ...figProvider_FindIncorrectSettings_Tests.cs | 2 - .../Nethermind.Config/EnvConfigSource.cs | 4 +- .../Nethermind.Config/INoCategoryConfig.cs | 6 -- .../Config/IMetricsConfig.cs | 6 ++ .../Config/MetricsConfig.cs | 4 +- .../MonitoringService.cs | 61 ++++++------------- 6 files changed, 31 insertions(+), 52 deletions(-) diff --git a/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs b/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs index 63d06109011..bc8a15ebb1f 100644 --- a/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs +++ b/src/Nethermind/Nethermind.Config.Test/ConfigProvider_FindIncorrectSettings_Tests.cs @@ -44,8 +44,6 @@ public void NoCategorySettings() IEnvironment? env = Substitute.For(); env.GetEnvironmentVariables().Returns(new Dictionary() { { "NETHERMIND_CLI_SWITCH_LOCAL", "http://localhost:80" }, - { "NETHERMIND_MONITORING_JOB", "nethermindJob" }, - { "NETHERMIND_MONITORING_GROUP", "nethermindGroup" }, { "NETHERMIND_CONFIG", "test2.json" }, { "NETHERMIND_XYZ", "xyz" }, // not existing, should get error { "QWER", "qwerty" } // not Nethermind setting, no error diff --git a/src/Nethermind/Nethermind.Config/EnvConfigSource.cs b/src/Nethermind/Nethermind.Config/EnvConfigSource.cs index 88443b89f57..f9385bb4bf4 100644 --- a/src/Nethermind/Nethermind.Config/EnvConfigSource.cs +++ b/src/Nethermind/Nethermind.Config/EnvConfigSource.cs @@ -49,13 +49,13 @@ public EnvConfigSource(IEnvironment environmentWrapper) return (null, a[1]); } - // VARIABLES like "NETHERMIND_CLI_SWITCH_LOCAL", "NETHERMIND_MONITORING_JOB" ... + // VARIABLES like "NETHERMIND_CLI_SWITCH_LOCAL" if (a.Length > 2 && !a[1].EndsWith("config", StringComparison.OrdinalIgnoreCase)) { return (null, string.Join(null, a[1..])); } - // Variables like "NETHERMIND_JSONRPCCONFIG_ENABLED" ... + // Variables like "NETHERMIND_JSONRPCCONFIG_ENABLED" return (a[1], a[2]); }); } diff --git a/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs b/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs index e50dcb39cd1..e53ec39fe2d 100644 --- a/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs +++ b/src/Nethermind/Nethermind.Config/INoCategoryConfig.cs @@ -9,12 +9,6 @@ public interface INoCategoryConfig : IConfig [ConfigItem(Description = "Path to the configuration file.")] public string Config { get; set; } - [ConfigItem(Description = "Sets the job name for metrics monitoring.", EnvironmentVariable = "NETHERMIND_MONITORING_JOB")] - public string MonitoringJob { get; set; } - - [ConfigItem(Description = "Sets the default group name for metrics monitoring.", EnvironmentVariable = "NETHERMIND_MONITORING_GROUP")] - public string MonitoringGroup { get; set; } - [ConfigItem(Description = "Defines host value for CLI function \"switchLocal\".", DefaultValue = "http://localhost", EnvironmentVariable = "NETHERMIND_CLI_SWITCH_LOCAL")] public string CliSwitchLocal { get; set; } } diff --git a/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs b/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs index 0186df10e5d..cb7c6b3e98f 100644 --- a/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs +++ b/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs @@ -31,4 +31,10 @@ public interface IMetricsConfig : IConfig [ConfigItem(Description = "Whether to publish database size metrics.", DefaultValue = "true")] bool EnableDbSizeMetrics { get; } + + [ConfigItem(Description = "The Prometheus metrics group name.", DefaultValue = "nethermind")] + string MonitoringGroup { get; } + + [ConfigItem(Description = "The Prometheus metrics job name.", DefaultValue = "nethermind")] + string MonitoringJob { get; } } diff --git a/src/Nethermind/Nethermind.Monitoring/Config/MetricsConfig.cs b/src/Nethermind/Nethermind.Monitoring/Config/MetricsConfig.cs index a6cb0e1534d..24d0db0f793 100644 --- a/src/Nethermind/Nethermind.Monitoring/Config/MetricsConfig.cs +++ b/src/Nethermind/Nethermind.Monitoring/Config/MetricsConfig.cs @@ -9,9 +9,11 @@ public class MetricsConfig : IMetricsConfig public int? ExposePort { get; set; } = null; public bool Enabled { get; set; } = false; public bool CountersEnabled { get; set; } = false; - public string PushGatewayUrl { get; set; } = ""; + public string PushGatewayUrl { get; set; } = null; public int IntervalSeconds { get; set; } = 5; public string NodeName { get; set; } = "Nethermind"; public bool EnableDbSizeMetrics { get; set; } = true; + public string MonitoringGroup { get; set; } = "nethermind"; + public string MonitoringJob { get; set; } = "nethermind"; } } diff --git a/src/Nethermind/Nethermind.Monitoring/MonitoringService.cs b/src/Nethermind/Nethermind.Monitoring/MonitoringService.cs index af60d635e84..f3e01acc89b 100644 --- a/src/Nethermind/Nethermind.Monitoring/MonitoringService.cs +++ b/src/Nethermind/Nethermind.Monitoring/MonitoringService.cs @@ -50,42 +50,42 @@ public MonitoringService(IMetricsController metricsController, IMetricsConfig me _logger = logManager is null ? throw new ArgumentNullException(nameof(logManager)) : logManager.GetClassLogger(); - _options = GetOptions(); + _options = GetOptions(metricsConfig); } public async Task StartAsync() { - if (!string.IsNullOrWhiteSpace(_pushGatewayUrl)) + if (_pushGatewayUrl is not null) { - MetricPusherOptions pusherOptions = new MetricPusherOptions + MetricPusherOptions pusherOptions = new() { Endpoint = _pushGatewayUrl, Job = _options.Job, Instance = _options.Instance, IntervalMilliseconds = _intervalSeconds * 1000, - AdditionalLabels = new[] - { - new Tuple("nethermind_group", _options.Group), - }, + AdditionalLabels = [new Tuple("nethermind_group", _options.Group)], OnError = ex => { if (ex.InnerException is SocketException) { - if (_logger.IsError) _logger.Error("Could not reach PushGatewayUrl, Please make sure you have set the correct endpoint in the configurations.", ex); + if (_logger.IsError) _logger.Error($"Cannot reach Pushgateway at {_pushGatewayUrl}", ex); return; } if (_logger.IsTrace) _logger.Error(ex.Message, ex); // keeping it as Error to log the exception details with it. } }; - MetricPusher metricPusher = new MetricPusher(pusherOptions); + MetricPusher metricPusher = new(pusherOptions); metricPusher.Start(); } + if (_exposePort is not null) { new NethermindKestrelMetricServer(_exposeHost, _exposePort.Value).Start(); } - await Task.Factory.StartNew(() => _metricsController.StartUpdating(), TaskCreationOptions.LongRunning); + + await Task.Factory.StartNew(_metricsController.StartUpdating, TaskCreationOptions.LongRunning); + if (_logger.IsInfo) _logger.Info($"Started monitoring for the group: {_options.Group}, instance: {_options.Instance}"); } @@ -101,42 +101,21 @@ public Task StopAsync() return Task.CompletedTask; } - private Options GetOptions() - => new Options(GetValueFromVariableOrDefault("JOB", "nethermind"), GetGroup(), GetInstance()); - - private string GetInstance() - => _nodeName.Replace("enode://", string.Empty).Split("@").FirstOrDefault(); - - private string GetGroup() - { - string group = GetValueFromVariableOrDefault("GROUP", "nethermind"); - string endpoint = _pushGatewayUrl.Split("/").LastOrDefault(); - if (!string.IsNullOrWhiteSpace(endpoint) && endpoint.Contains('-')) - { - group = endpoint.Split("-")[0] ?? group; - } - - return group; - } - - private static string GetValueFromVariableOrDefault(string variable, string @default) + private Options GetOptions(IMetricsConfig config) { - string value = Environment.GetEnvironmentVariable($"NETHERMIND_MONITORING_{variable}")?.ToLowerInvariant(); + string endpoint = _pushGatewayUrl?.Split("/").Last(); + string group = endpoint?.Contains('-', StringComparison.Ordinal) == true + ? endpoint.Split("-")[0] : config.MonitoringGroup; + string instance = _nodeName.Replace("enode://", string.Empty).Split("@")[0]; - return string.IsNullOrWhiteSpace(value) ? @default : value; + return new(config.MonitoringJob, group, instance); } - private class Options + private class Options(string job, string group, string instance) { - public string Job { get; } - public string Instance { get; } - public string Group { get; } - public Options(string job, string @group, string instance) - { - Job = job; - Group = @group; - Instance = instance; - } + public string Job { get; } = job; + public string Instance { get; } = instance; + public string Group { get; } = group; } } } From ea2d23587d0f7e392f1054cd6a983f931aa77043 Mon Sep 17 00:00:00 2001 From: Ruben Buniatyan Date: Fri, 1 Nov 2024 20:11:13 +0100 Subject: [PATCH 5/7] Remove redundant default values --- src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs b/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs index cb7c6b3e98f..4f7f14ac34b 100644 --- a/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs +++ b/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs @@ -11,7 +11,7 @@ public interface IMetricsConfig : IConfig [ConfigItem(Description = "The IP address to expose Prometheus metrics at. The value of `+` means listening on all available hostnames. Setting this to `localhost` prevents remote access.", DefaultValue = "+")] string ExposeHost { get; } - [ConfigItem(Description = "The port to expose Prometheus metrics at.", DefaultValue = "null")] + [ConfigItem(Description = "The port to expose Prometheus metrics at.")] int? ExposePort { get; } [ConfigItem(Description = "Whether to publish various metrics to Prometheus Pushgateway at a given interval.", DefaultValue = "false")] @@ -20,7 +20,7 @@ public interface IMetricsConfig : IConfig [ConfigItem(Description = "Whether to publish metrics using .NET diagnostics that can be collected with dotnet-counters.", DefaultValue = "false")] bool CountersEnabled { get; } - [ConfigItem(Description = "The Prometheus Pushgateway instance URL.", DefaultValue = "")] + [ConfigItem(Description = "The Prometheus Pushgateway instance URL.")] string PushGatewayUrl { get; } [ConfigItem(DefaultValue = "5", Description = "The frequency of pushing metrics to Prometheus, in seconds.")] From 553c54753347f389a5ccb2e9c9781b5ae72cb99e Mon Sep 17 00:00:00 2001 From: Ruben Buniatyan Date: Fri, 1 Nov 2024 20:12:25 +0100 Subject: [PATCH 6/7] Fix default value --- src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs b/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs index 4f7f14ac34b..b263e55340c 100644 --- a/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs +++ b/src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs @@ -26,7 +26,7 @@ public interface IMetricsConfig : IConfig [ConfigItem(DefaultValue = "5", Description = "The frequency of pushing metrics to Prometheus, in seconds.")] int IntervalSeconds { get; } - [ConfigItem(Description = "The name to display on the Grafana dashboard.", DefaultValue = "\"Nethermind\"")] + [ConfigItem(Description = "The name to display on the Grafana dashboard.", DefaultValue = "Nethermind")] string NodeName { get; } [ConfigItem(Description = "Whether to publish database size metrics.", DefaultValue = "true")] From bb8be784426a2a7ed46ffdc9b4e88b51b64ea750 Mon Sep 17 00:00:00 2001 From: Ruben Buniatyan Date: Fri, 1 Nov 2024 20:15:53 +0100 Subject: [PATCH 7/7] Fix tests --- src/Nethermind/Nethermind.Runner.Test/ConfigFilesTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Runner.Test/ConfigFilesTests.cs b/src/Nethermind/Nethermind.Runner.Test/ConfigFilesTests.cs index e5f1956dde8..8dae53b7b5e 100644 --- a/src/Nethermind/Nethermind.Runner.Test/ConfigFilesTests.cs +++ b/src/Nethermind/Nethermind.Runner.Test/ConfigFilesTests.cs @@ -162,7 +162,7 @@ public void Metrics_disabled_by_default(string configWildcard) Test(configWildcard, c => c.Enabled, false); Test(configWildcard, c => c.NodeName.ToUpperInvariant(), (cf, p) => cf.Replace("_", " ").Replace(".json", "").ToUpperInvariant().Replace("POACORE", "POA CORE")); Test(configWildcard, c => c.IntervalSeconds, 5); - Test(configWildcard, c => c.PushGatewayUrl, ""); + Test(configWildcard, c => c.PushGatewayUrl, (string)null); } [TestCase("^spaceneth ^volta", 50)]