Skip to content

Commit

Permalink
added prometheus for tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
tbssajal committed Dec 28, 2022
1 parent 99f2ecc commit 3dbc08a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Lachain.Core/Network/BannedPeerTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Lachain.Storage.Repositories;
using Lachain.Utility;
using Lachain.Utility.Utils;
using Prometheus;

namespace Lachain.Core.Network
{
Expand All @@ -31,6 +32,19 @@ public class BannedPeerTracker : IBannedPeerTracker
// Scans for banned peer in system contract tx
// If a peer is banned locally, then sends a system contract tx to broadcast
private static readonly ILogger<BannedPeerTracker> Logger = LoggerFactory.GetLoggerForClass<BannedPeerTracker>();
private static readonly Counter BanVotesCounter = Metrics.CreateCounter(
"lachain_peer_ban_vote_count",
"Number of times a peer was voted by validators to be banned",
"peer"
);
private static readonly Gauge BannedByVotesCycle = Metrics.CreateGauge(
"lachain_peer_banned_by_vote_cycle",
"Latest cycle where a peer was banned for having enough votes from validators",
new GaugeConfiguration
{
LabelNames = new[] {"peer", "votes"}
}
);
private readonly IPeerBanManager _banManager;
private readonly ITransactionBuilder _transactionBuilder;
private readonly ITransactionSigner _transactionSigner;
Expand Down Expand Up @@ -201,9 +215,14 @@ private void ProcessBanRequest(InvocationContext context)
}
var votes = _repostiroy.AddVoteForBannedPeer(cycle, peerToBan, sender);
_banPeerVotes[peerToBan.ToPublicKey()] = votes;
BanVotesCounter.WithLabels(peerToBan.ToHex()).Inc();
if (votes > ThresholdForBan)
{
// ban peer as more ThresholdForBan validators voted for this peer
Logger.LogTrace(
$"Peer {peerToBan.ToHex()} is banned on request of {votes} validators (Threshold {ThresholdForBan}) in cycle {cycle}"
);
BannedByVotesCycle.WithLabels(peerToBan.ToHex(), votes.ToString()).Set(cycle);
_banManager.BanPeer(peerToBan);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Lachain.Networking/Hub/ClientWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public class ClientWorker : IDisposable
"peer", "message_type"
);

private static readonly Counter PenaltyCounter = Metrics.CreateCounter(
"lachain_peer_penalty_count",
"Number of penalties done by peer",
"peer"
);

public byte[] PeerPublicKey { get; }
private readonly IMessageFactory _messageFactory;
private readonly HubConnector _hubConnector;
Expand Down Expand Up @@ -195,6 +201,7 @@ public int AdvanceEra(ulong era)

public void IncPenalty()
{
PenaltyCounter.WithLabels(PeerPublicKey.ToHex()).Inc();
_penaltyHandler.IncPenalty();
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/Lachain.Networking/PeerFault/PeerBanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@
using Lachain.Proto;
using Lachain.Storage.Repositories;
using Lachain.Utility.Utils;
using Prometheus;

namespace Lachain.Networking.PeerFault
{
public class PeerBanManager : IPeerBanManager
{
private static readonly ILogger<PeerBanManager> Logger = LoggerFactory.GetLoggerForClass<PeerBanManager>();
private static readonly Counter BanForPenaltyCounter = Metrics.CreateCounter(
"lachain_peer_banned_for_penalty_count",
"Number of times a peer was banned for penalty",
"peer"
);
private static readonly Gauge BanByPenaltyCycle = Metrics.CreateGauge(
"lachain_peer_banned_cycle",
"Latest cycle where a peer was banned for penalty",
new GaugeConfiguration
{
LabelNames = new[] {"peer", "penalties"}
}
);
private ulong _cycle = 0;
// ban this peer for 3 cycles: this _cycle, _cycle + 1 and _cycle + 2
// this way even if he can become validator, will not get any attendance
Expand Down Expand Up @@ -120,6 +134,8 @@ private void BanPeerForPenalty(object? sender, (byte[] publicKey, ulong penaltie
Task.Factory.StartNew(() =>
{
var (publicKey, penalties) = @event;
BanForPenaltyCounter.WithLabels(publicKey.ToHex()).Inc();
BanByPenaltyCycle.WithLabels(publicKey.ToHex(), penalties.ToString()).Set(_cycle);
BanPeer(publicKey);
Logger.LogTrace($"Banned peer {publicKey.ToHex()} for {penalties} penalties during cycle {_cycle}");
OnPeerBanned?.Invoke(this, (publicKey, penalties));
Expand Down

0 comments on commit 3dbc08a

Please sign in to comment.