Skip to content

Commit

Permalink
Fixed BuildGaugeName (#4932)
Browse files Browse the repository at this point in the history
Added unit tests
  • Loading branch information
OlegJakushkin authored Nov 23, 2022
1 parent 5c165df commit e76a320
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
32 changes: 31 additions & 1 deletion src/Nethermind/Nethermind.Monitoring.Test/MetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,49 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using FluentAssertions;
using Nethermind.Core;
using Nethermind.Logging;
using Nethermind.Monitoring.Config;
using Nethermind.Monitoring.Metrics;
using Nethermind.Runner;
using NUnit.Framework;

namespace Nethermind.Monitoring.Test
{
[TestFixture]
public class MetricsTests
{
public static class TestMetrics
{
[System.ComponentModel.Description("A test description")]
public static long OneTwoThree { get; set; }

[System.ComponentModel.Description("Another test description.")]
[DataMember(Name = "one_two_three")]
public static long OneTwoThreeSpecial { get; set; }
}

[Test]
public void Test_gauge_names()
{
MetricsConfig metricsConfig = new()
{
Enabled = true
};
MetricsController metricsController = new(metricsConfig);
metricsController.RegisterMetrics(typeof(TestMetrics));
var gauges = metricsController._gauges;
var keyDefault = $"{nameof(TestMetrics)}.{nameof(TestMetrics.OneTwoThree)}";
var keySpecial = $"{nameof(TestMetrics)}.{nameof(TestMetrics.OneTwoThreeSpecial)}";

Assert.Contains(keyDefault, gauges.Keys);
Assert.Contains(keySpecial, gauges.Keys);

Assert.AreEqual(gauges[keyDefault].Name, "nethermind_one_two_three");
Assert.AreEqual(gauges[keySpecial].Name, "one_two_three");
}

[Test]
public void Register_and_update_metrics_should_not_throw_exception()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ public class MetricsController : IMetricsController
{
private readonly int _intervalSeconds;
private Timer _timer;
private Dictionary<string, Gauge> _gauges = new();
private Dictionary<Type, (PropertyInfo, string)[]> _propertiesCache = new();
private Dictionary<Type, (FieldInfo, string)[]> _fieldsCache = new();
private Dictionary<Type, (string DictName, IDictionary<string, long> Dict)> _dynamicPropCache = new();
private HashSet<Type> _metricTypes = new();
private readonly Dictionary<Type, (PropertyInfo, string)[]> _propertiesCache = new();
private readonly Dictionary<Type, (FieldInfo, string)[]> _fieldsCache = new();
private readonly Dictionary<Type, (string DictName, IDictionary<string, long> Dict)> _dynamicPropCache = new();
private readonly HashSet<Type> _metricTypes = new();

public readonly Dictionary<string, Gauge> _gauges = new();

public void RegisterMetrics(Type type)
{
Expand Down Expand Up @@ -102,7 +103,7 @@ private static string BuildGaugeName(MemberInfo propertyInfo) =>
propertyInfo.GetCustomAttribute<DataMemberAttribute>()?.Name ?? BuildGaugeName(propertyInfo.Name);

private static string BuildGaugeName(string propertyName) =>
Regex.Replace(propertyName, @"(\p{Ll})(\p{Lu})", "nethermind_$1_$2").ToLowerInvariant();
$"nethermind_{Regex.Replace(propertyName, @"(\p{Ll})(\p{Lu})", "$1_$2").ToLowerInvariant()}";

private static Gauge CreateGauge(string name, string help = "", GaugeConfiguration configuration = null)
=> Prometheus.Metrics.CreateGauge(name, help, configuration);
Expand Down

0 comments on commit e76a320

Please sign in to comment.