Skip to content

Commit

Permalink
Prometheus metrics improvements (#5570)
Browse files Browse the repository at this point in the history
* db size metrics

* Refactor & Syncing

* Fix issues

* Fix build

* Network metric

* Revert "Network metric"

This reverts commit 1711149.

* Instance & Network

* SyncType & PruningMode

* Fix sync time

* Refactor

* Refactor

* Fix test

* Add test

* Add all dbs

* Typo

* Exception & property

* Fix build
  • Loading branch information
deffrian authored Apr 26, 2023
1 parent 5d2f835 commit 24e6f06
Show file tree
Hide file tree
Showing 28 changed files with 252 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ public BlockTree(
$"lowest inserted header {LowestInsertedHeader?.Number}, " +
$"body {LowestInsertedBodyNumber}, " +
$"lowest sync inserted block number {LowestInsertedBeaconHeader?.Number}");
ThisNodeInfo.AddInfo("Chain ID :", $"{(ChainId == NetworkId ? Core.BlockchainIds.GetBlockchainName(NetworkId) : ChainId)}");
ProductInfo.Network = $"{(ChainId == NetworkId ? BlockchainIds.GetBlockchainName(NetworkId) : ChainId)}";
ThisNodeInfo.AddInfo("Chain ID :", ProductInfo.Network);
ThisNodeInfo.AddInfo("Chain head :", $"{Head?.Header.ToString(BlockHeader.Format.Short) ?? "0"}");
if (ChainId != NetworkId)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Nethermind/Nethermind.Core/ProductInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ static ProductInfo()
public static string Runtime { get; }

public static string Version { get; }

public static string Network { get; set; } = "";

public static string Instance { get; set; } = "";

public static string SyncType { get; set; } = "";

public static string PruningMode { get; set; } = "";
}
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Db.Rocks/ColumnDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public void Flush()

private void UpdateReadMetrics() => _mainDb.UpdateReadMetrics();

public long GetSize() => _mainDb.GetSize();

public Span<byte> GetSpan(ReadOnlySpan<byte> key) => _rocksDb.GetSpan(key, _columnFamily);

public void PutSpan(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value)
Expand Down
15 changes: 15 additions & 0 deletions src/Nethermind/Nethermind.Db.Rocks/DbOnTheRocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ protected internal void UpdateWriteMetrics()
Metrics.OtherDbWrites++;
}

public long GetSize()
{
try
{
return long.TryParse(_db.GetProperty("rocksdb.total-sst-files-size"), out long size) ? size : 0;
}
catch (RocksDbSharpException e)
{
if (_logger.IsWarn)
_logger.Warn($"Failed to update DB size metrics {e.Message}");
}

return 0;
}

protected virtual void BuildOptions<T>(PerTableDbConfig dbConfig, Options<T> options) where T : Options<T>
{
_maxThisDbSize = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Db.Rpc/RpcDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void Dispose()
_recordDb.Dispose();
}

public long GetSize() => 0;

public string Name { get; } = "RpcDb";

public byte[] this[ReadOnlySpan<byte> key]
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Db/FullPruning/FullPruningDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Logging;

Expand Down Expand Up @@ -89,6 +87,8 @@ public void Dispose()
_pruningContext?.CloningDb.Dispose();
}

public long GetSize() => _currentDb.GetSize() + (_pruningContext?.CloningDb.GetSize() ?? 0);

public string Name => _settings.DbName;

public KeyValuePair<byte[], byte[]?>[] this[byte[][] keys] => _currentDb[keys];
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Db/IDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IDb : IKeyValueStoreWithBatching, IDisposable
IEnumerable<byte[]> GetAllValues(bool ordered = false);
void Remove(ReadOnlySpan<byte> key);
bool KeyExists(ReadOnlySpan<byte> key);
long GetSize();

void Flush();

Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Db/MemDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public virtual IBatch StartBatch()

public int Count => _db.Count;

public long GetSize() => 0;

public void Dispose()
{
}
Expand Down
40 changes: 40 additions & 0 deletions src/Nethermind/Nethermind.Db/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,46 @@ public static class Metrics
[Description("Indicator if StadeDb is being pruned.")]
public static int StateDbPruning { get; set; }

[GaugeMetric]
[Description("Size of state DB in bytes")]
public static long StateDbSize { get; set; }

[GaugeMetric]
[Description("Size of receipts DB in bytes")]
public static long ReceiptsDbSize { get; set; }

[GaugeMetric]
[Description("Size of headers DB in bytes")]
public static long HeadersDbSize { get; set; }

[GaugeMetric]
[Description("Size of blocks DB in bytes")]
public static long BlocksDbSize { get; set; }

[GaugeMetric]
[Description("Size of bloom DB in bytes")]
public static long BloomDbSize { get; set; }

[GaugeMetric]
[Description("Size of code DB in bytes")]
public static long CodeDbSize { get; set; }

[GaugeMetric]
[Description("Size of blockInfos DB in bytes")]
public static long BlockInfosDbSize { get; set; }

[GaugeMetric]
[Description("Size of cht DB in bytes")]
public static long ChtDbSize { get; set; }

[GaugeMetric]
[Description("Size of metadata DB in bytes")]
public static long MetadataDbSize { get; set; }

[GaugeMetric]
[Description("Size of witness DB in bytes")]
public static long WitnessDbSize { get; set; }

[Description("Metrics extracted from RocksDB Compacion Stats and DB Statistics")]
public static IDictionary<string, long> DbStats { get; set; } = new ConcurrentDictionary<string, long>();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Db/NullDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public bool KeyExists(ReadOnlySpan<byte> key)
return false;
}

public long GetSize() => 0;

public IDb Innermost => this;
public void Flush() { }
public void Clear() { }
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Db/ReadOnlyDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public IBatch StartBatch()
return this.LikeABatch();
}

public long GetSize() => _wrappedDb.GetSize();

public void Remove(ReadOnlySpan<byte> key) { }

public bool KeyExists(ReadOnlySpan<byte> key)
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Db/SimpleFilePublicKeyDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public bool KeyExists(ReadOnlySpan<byte> key)
return _cache.ContainsKey(key);
}

public long GetSize() => 0;

public IDb Innermost => this;
public void Flush() { }
public void Clear()
Expand Down
41 changes: 41 additions & 0 deletions src/Nethermind/Nethermind.Facade.Test/Eth/EthSyncingInfoTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading;
using FluentAssertions;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
using Nethermind.Blockchain.Synchronization;
Expand Down Expand Up @@ -118,6 +120,45 @@ public void IsSyncing_AncientBarriers(long? bodiesTail, long? receiptsTail, bool
Assert.AreEqual(CreateSyncingResult(expectedResult, currentHead, highestBlock), syncingResult);
}

[Test]
public void Should_calculate_sync_time()
{
SyncConfig syncConfig = new();
IBlockTree blockTree = Substitute.For<IBlockTree>();
IReceiptStorage receiptStorage = Substitute.For<IReceiptStorage>();

blockTree.FindBestSuggestedHeader().Returns(Build.A.BlockHeader.WithNumber(100).TestObject);
blockTree.Head.Returns(Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(100).TestObject)
.TestObject);

EthSyncingInfo ethSyncingInfo = new(blockTree, receiptStorage, syncConfig, LimboLogs.Instance);

ethSyncingInfo.IsSyncing().Should().Be(false);
ethSyncingInfo.UpdateAndGetSyncTime().TotalMicroseconds.Should().Be(0);

blockTree.FindBestSuggestedHeader().Returns(Build.A.BlockHeader.WithNumber(100).TestObject);
blockTree.Head.Returns(Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(80).TestObject)
.TestObject);

// First call starting timer
ethSyncingInfo.IsSyncing().Should().Be(true);
ethSyncingInfo.UpdateAndGetSyncTime().TotalMicroseconds.Should().Be(0);

Thread.Sleep(100);

// Second call timer should count some time
ethSyncingInfo.IsSyncing().Should().Be(true);
ethSyncingInfo.UpdateAndGetSyncTime().TotalMicroseconds.Should().NotBe(0);

// Sync ended time should be zero
blockTree.FindBestSuggestedHeader().Returns(Build.A.BlockHeader.WithNumber(100).TestObject);
blockTree.Head.Returns(Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(100).TestObject)
.TestObject);

ethSyncingInfo.IsSyncing().Should().Be(false);
ethSyncingInfo.UpdateAndGetSyncTime().TotalMicroseconds.Should().Be(0);
}

private SyncingResult CreateSyncingResult(bool isSyncing, long currentBlock, long highestBlock)
{
if (!isSyncing) return SyncingResult.NotSyncing;
Expand Down
22 changes: 22 additions & 0 deletions src/Nethermind/Nethermind.Facade/Eth/EthSyncingInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Diagnostics;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
using Nethermind.Blockchain.Synchronization;
Expand Down Expand Up @@ -72,6 +74,26 @@ private SyncingResult ReturnSyncing(long headNumberOrZero, long bestSuggestedNum
};
}

private readonly Stopwatch _syncStopwatch = new();
public TimeSpan UpdateAndGetSyncTime()
{
if (!_syncStopwatch.IsRunning)
{
if (IsSyncing())
{
_syncStopwatch.Start();
}
return TimeSpan.Zero;
}

if (!IsSyncing())
{
_syncStopwatch.Stop();
return TimeSpan.Zero;
}

return _syncStopwatch.Elapsed;
}

public bool IsSyncing()
{
Expand Down
4 changes: 4 additions & 0 deletions src/Nethermind/Nethermind.Facade/Eth/IEthSyncingInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;

namespace Nethermind.Facade.Eth
{
public interface IEthSyncingInfo
{
SyncingResult GetFullInfo();

bool IsSyncing();

TimeSpan UpdateAndGetSyncTime();
}
}
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Facade/Nethermind.Facade.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<ProjectReference Include="..\Nethermind.Core\Nethermind.Core.csproj" />
<ProjectReference Include="..\Nethermind.Crypto\Nethermind.Crypto.csproj" />
<ProjectReference Include="..\Nethermind.Wallet\Nethermind.Wallet.csproj" />
<ProjectReference Include="..\Nethermind.Synchronization\Nethermind.Synchronization.csproj" />
<ProjectReference Include="..\Nethermind.Monitoring\Nethermind.Monitoring.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.Init/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using Nethermind.Core;
using Nethermind.Db;
using Nethermind.Monitoring.Metrics;

namespace Nethermind.Init
Expand All @@ -14,6 +15,10 @@ public static class Metrics
[MetricsStaticDescriptionTag(nameof(ProductInfo.Commit), typeof(ProductInfo))]
[MetricsStaticDescriptionTag(nameof(ProductInfo.Runtime), typeof(ProductInfo))]
[MetricsStaticDescriptionTag(nameof(ProductInfo.BuildTimestamp), typeof(ProductInfo))]
[MetricsStaticDescriptionTag(nameof(ProductInfo.Instance), typeof(ProductInfo))]
[MetricsStaticDescriptionTag(nameof(ProductInfo.Network), typeof(ProductInfo))]
[MetricsStaticDescriptionTag(nameof(ProductInfo.SyncType), typeof(ProductInfo))]
[MetricsStaticDescriptionTag(nameof(ProductInfo.PruningMode), typeof(ProductInfo))]
public static long Version { get; set; }
}
}
3 changes: 1 addition & 2 deletions src/Nethermind/Nethermind.Init/Steps/InitDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
using Nethermind.Db.Rpc;
using Nethermind.JsonRpc.Client;
using Nethermind.Logging;
using Nethermind.Synchronization.ParallelSync;

namespace Nethermind.Init.Steps
{
[RunnerStepDependencies(typeof(ApplyMemoryHint))]
public class InitDatabase : IStep
{
private readonly IBasicApi _api;
private readonly INethermindApi _api;

public InitDatabase(INethermindApi api)
{
Expand Down
Loading

0 comments on commit 24e6f06

Please sign in to comment.