Skip to content

Commit

Permalink
Features/unified p2p message metrics (#6835)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukasz Rozmej <lukasz.rozmej@gmail.com>
  • Loading branch information
asdacap and LukaszRozmej authored Mar 21, 2024
1 parent 200ea2b commit dc4c50a
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 81 deletions.
13 changes: 13 additions & 0 deletions src/Nethermind/Nethermind.Core/Metric/IMetricLabels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

namespace Nethermind.Core.Metric;

/// <summary>
/// Used by MetricController to provide labels. Useful in high performance scenario where you don't want to set a string
/// on the metric dictionary as key and/or you don't want to use tuple.
/// </summary>
public interface IMetricLabels
{
string[] Labels { get; }
}
12 changes: 12 additions & 0 deletions src/Nethermind/Nethermind.Monitoring.Test/MetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using FluentAssertions;
using Nethermind.Core;
using Nethermind.Core.Attributes;
using Nethermind.Core.Metric;
using Nethermind.Logging;
using Nethermind.Monitoring.Config;
using Nethermind.Monitoring.Metrics;
Expand All @@ -34,6 +35,9 @@ public static class TestMetrics
[KeyIsLabel("somelabel")]
public static ConcurrentDictionary<SomeEnum, long> WithLabelledDictionary { get; set; } = new();

[KeyIsLabel("label1", "label2", "label3")]
public static ConcurrentDictionary<CustomLabelType, long> WithCustomLabelType { get; set; } = new();

public static IDictionary<string, long> OldDictionaryMetrics { get; set; } = new ConcurrentDictionary<string, long>();
}

Expand All @@ -43,6 +47,11 @@ public enum SomeEnum
Option2,
}

public struct CustomLabelType(int num1, int num2, int num3) : IMetricLabels
{
public string[] Labels => [num1.ToString(), num2.ToString(), num3.ToString()];
}

[Test]
public void Test_update_correct_gauge()
{
Expand All @@ -57,6 +66,7 @@ public void Test_update_correct_gauge()
TestMetrics.OneTwoThreeSpecial = 1234;
TestMetrics.WithLabelledDictionary[SomeEnum.Option1] = 2;
TestMetrics.WithLabelledDictionary[SomeEnum.Option2] = 3;
TestMetrics.WithCustomLabelType[new CustomLabelType(1, 11, 111)] = 1111;
TestMetrics.OldDictionaryMetrics["metrics0"] = 4;
TestMetrics.OldDictionaryMetrics["metrics1"] = 5;
metricsController.UpdateMetrics(null);
Expand All @@ -65,6 +75,7 @@ public void Test_update_correct_gauge()
var keyDefault = $"{nameof(TestMetrics)}.{nameof(TestMetrics.OneTwoThree)}";
var keySpecial = $"{nameof(TestMetrics)}.{nameof(TestMetrics.OneTwoThreeSpecial)}";
var keyDictionary = $"{nameof(TestMetrics)}.{nameof(TestMetrics.WithLabelledDictionary)}";
var keyDictionary2 = $"{nameof(TestMetrics)}.{nameof(TestMetrics.WithCustomLabelType)}";
var keyOldDictionary0 = $"{nameof(TestMetrics.OldDictionaryMetrics)}.metrics0";
var keyOldDictionary1 = $"{nameof(TestMetrics.OldDictionaryMetrics)}.metrics1";

Expand All @@ -81,6 +92,7 @@ public void Test_update_correct_gauge()
Assert.That(gauges[keySpecial].Value, Is.EqualTo(1234));
Assert.That(gauges[keyDictionary].WithLabels(SomeEnum.Option1.ToString()).Value, Is.EqualTo(2));
Assert.That(gauges[keyDictionary].WithLabels(SomeEnum.Option2.ToString()).Value, Is.EqualTo(3));
Assert.That(gauges[keyDictionary2].WithLabels("1", "11", "111").Value, Is.EqualTo(1111));
Assert.That(gauges[keyOldDictionary0].Value, Is.EqualTo(4));
Assert.That(gauges[keyOldDictionary1].Value, Is.EqualTo(5));
}
Expand Down
31 changes: 19 additions & 12 deletions src/Nethermind/Nethermind.Monitoring/Metrics/MetricsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Nethermind.Core;
using Nethermind.Core.Attributes;
using Nethermind.Core.Collections;
using Nethermind.Core.Metric;
using Nethermind.Monitoring.Config;
using Prometheus;

Expand Down Expand Up @@ -252,19 +253,25 @@ private void UpdateMetrics(Type type)
foreach (object key in dict.Keys)
{
double value = Convert.ToDouble(dict[key]);
if (key is ITuple keyAsTuple)
switch (key)
{
string[] labels = new string[keyAsTuple.Length];
for (int i = 0; i < keyAsTuple.Length; i++)
{
labels[i] = keyAsTuple[i].ToString();
}

ReplaceValueIfChanged(value, gaugeName, labels);
}
else
{
ReplaceValueIfChanged(value, gaugeName, key.ToString());
case IMetricLabels label:
ReplaceValueIfChanged(value, gaugeName, label.Labels);
break;
case ITuple keyAsTuple:
{
string[] labels = new string[keyAsTuple.Length];
for (int i = 0; i < keyAsTuple.Length; i++)
{
labels[i] = keyAsTuple[i].ToString();
}

ReplaceValueIfChanged(value, gaugeName, labels);
break;
}
default:
ReplaceValueIfChanged(value, gaugeName, key.ToString());
break;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Network.Test/P2P/SessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Nethermind.Network.P2P.ProtocolHandlers;
using Nethermind.Network.Rlpx;
using Nethermind.Stats.Model;
using NonBlocking;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
using NUnit.Framework;
Expand Down
Loading

0 comments on commit dc4c50a

Please sign in to comment.