Skip to content

Commit

Permalink
Move NETHERMIND_MONITORING_GROUP and NETHERMIND_MONITORING_JOB to…
Browse files Browse the repository at this point in the history
… `IMetricsConfig`
  • Loading branch information
rubo committed Nov 1, 2024
1 parent 22d36b7 commit cdc6a24
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public void NoCategorySettings()
IEnvironment? env = Substitute.For<IEnvironment>();
env.GetEnvironmentVariables().Returns(new Dictionary<string, string>() {
{ "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
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Config/EnvConfigSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
}
Expand Down
6 changes: 0 additions & 6 deletions src/Nethermind/Nethermind.Config/INoCategoryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.Monitoring/Config/IMetricsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
4 changes: 3 additions & 1 deletion src/Nethermind/Nethermind.Monitoring/Config/MetricsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
61 changes: 20 additions & 41 deletions src/Nethermind/Nethermind.Monitoring/MonitoringService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>("nethermind_group", _options.Group),
},
AdditionalLabels = [new Tuple<string, string>("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}");
}

Expand All @@ -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;
}
}
}

0 comments on commit cdc6a24

Please sign in to comment.