From b38f2dd61ae13b273a744a348df3953072e06791 Mon Sep 17 00:00:00 2001 From: scooletz Date: Tue, 3 Sep 2024 14:04:12 +0200 Subject: [PATCH] pushing scheduled properly --- .../Nethermind.Monitoring/MetricPusher.cs | 11 +++-------- .../Metrics/IMetricsController.cs | 2 +- .../Metrics/MetricsController.cs | 15 ++++----------- .../Nethermind.Monitoring/MonitoringService.cs | 2 +- 4 files changed, 9 insertions(+), 21 deletions(-) diff --git a/src/Nethermind/Nethermind.Monitoring/MetricPusher.cs b/src/Nethermind/Nethermind.Monitoring/MetricPusher.cs index 15217778a82..360c927866c 100644 --- a/src/Nethermind/Nethermind.Monitoring/MetricPusher.cs +++ b/src/Nethermind/Nethermind.Monitoring/MetricPusher.cs @@ -21,7 +21,6 @@ namespace Nethermind.Monitoring; /// public class MetricPusher : MetricHandler { - private readonly TimeSpan _pushInterval; private readonly HttpMethod _method; private readonly Uri _targetUrl; private readonly Func _httpClientProvider; @@ -64,8 +63,6 @@ public MetricPusher(MetricPusherOptions options) } _targetUrl = targetUrl; - - _pushInterval = TimeSpan.FromMilliseconds(options.IntervalMilliseconds); _onError = options.OnError; _method = options.ReplaceOnPush ? HttpMethod.Put : HttpMethod.Post; @@ -84,22 +81,20 @@ protected override Task StartServer(CancellationToken cancel) { try { - await _wait.WaitAsync(_pushInterval, cancel); + await _wait.WaitAsync(cancel); } catch (OperationCanceledException) { break; } + // As the wait above is cancellable, this will send at the end await PushOnce(); } - - // Push the final state - await PushOnce(); }); } - public void ForceUpdate() => _wait.Release(); + public void ScheduleUpdatePush() => _wait.Release(); private async Task PushOnce() { diff --git a/src/Nethermind/Nethermind.Monitoring/Metrics/IMetricsController.cs b/src/Nethermind/Nethermind.Monitoring/Metrics/IMetricsController.cs index 7f33924ab4c..afab446cf1a 100644 --- a/src/Nethermind/Nethermind.Monitoring/Metrics/IMetricsController.cs +++ b/src/Nethermind/Nethermind.Monitoring/Metrics/IMetricsController.cs @@ -8,7 +8,7 @@ namespace Nethermind.Monitoring.Metrics public interface IMetricsController { void RegisterMetrics(Type type); - void StartUpdating(Action onForced); + void StartUpdating(Action metricsUpdated); void StopUpdating(); void AddMetricsUpdateAction(Action callback); void ForceUpdate(); diff --git a/src/Nethermind/Nethermind.Monitoring/Metrics/MetricsController.cs b/src/Nethermind/Nethermind.Monitoring/Metrics/MetricsController.cs index fef44a8c0b4..b5ef88ca0b6 100644 --- a/src/Nethermind/Nethermind.Monitoring/Metrics/MetricsController.cs +++ b/src/Nethermind/Nethermind.Monitoring/Metrics/MetricsController.cs @@ -203,7 +203,7 @@ public MetricsController(IMetricsConfig metricsConfig) _useCounters = metricsConfig.CountersEnabled; } - public void StartUpdating(Action onForced) + public void StartUpdating(Action metricsUpdated) { _cts = new CancellationTokenSource(); Task.Run(() => RunLoop(_cts.Token)); @@ -216,15 +216,11 @@ async Task RunLoop(CancellationToken ct) while (ct.IsCancellationRequested == false) { - bool forced = false; - Task wait = GetForceUpdateWait(); try { - - Task finished = await Task.WhenAny(wait, Task.Delay(waitTime, ct)); - forced = finished == wait; + await Task.WhenAny(wait, Task.Delay(waitTime, ct)); } catch (OperationCanceledException) { @@ -234,11 +230,8 @@ async Task RunLoop(CancellationToken ct) UpdateMetrics(); - if (forced && onForced != null) - { - // The update was forced and there's a onForced delegate. Execute it. - onForced(); - } + // Inform about metrics updated + metricsUpdated?.Invoke(); if (ct.IsCancellationRequested == false) { diff --git a/src/Nethermind/Nethermind.Monitoring/MonitoringService.cs b/src/Nethermind/Nethermind.Monitoring/MonitoringService.cs index 6dff1c91a22..b0d3fa2afe6 100644 --- a/src/Nethermind/Nethermind.Monitoring/MonitoringService.cs +++ b/src/Nethermind/Nethermind.Monitoring/MonitoringService.cs @@ -85,7 +85,7 @@ public Task StartAsync() new NethermindKestrelMetricServer(_exposeHost, _exposePort.Value).Start(); } - _metricsController.StartUpdating(pusher != null ? pusher.ForceUpdate : null); + _metricsController.StartUpdating(pusher != null ? pusher.ScheduleUpdatePush : null); if (_logger.IsInfo) _logger.Info($"Started monitoring for the group: {_options.Group}, instance: {_options.Instance}");