From f49aa8e391b4f3d0b1d09ebc6fa53e5bbd4a1379 Mon Sep 17 00:00:00 2001 From: tuyennhv Date: Fri, 31 Dec 2021 16:00:11 +0700 Subject: [PATCH] Add peer gossip score metrics (#3556) --- .../lodestar/src/metrics/metrics/lodestar.ts | 11 ++++++++ .../lodestar/src/network/gossip/gossipsub.ts | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/lodestar/src/metrics/metrics/lodestar.ts b/packages/lodestar/src/metrics/metrics/lodestar.ts index f0b5ecdbf57c..22f787ebfd7f 100644 --- a/packages/lodestar/src/metrics/metrics/lodestar.ts +++ b/packages/lodestar/src/metrics/metrics/lodestar.ts @@ -160,6 +160,17 @@ export function createLodestarMetrics( }), }, + gossipPeer: { + scoreByThreshold: register.gauge<"threshold">({ + name: "lodestar_gossip_peer_score_by_threshold_count", + help: "Gossip peer score by threashold", + labelNames: ["threshold"], + }), + score: register.avgMinMax({ + name: "lodestar_gossip_score_avg_min_max", + help: "Avg min max of all gossip peer scores", + }), + }, gossipMesh: { peersByType: register.gauge<"type" | "fork">({ name: "lodestar_gossip_mesh_peers_by_type_count", diff --git a/packages/lodestar/src/network/gossip/gossipsub.ts b/packages/lodestar/src/network/gossip/gossipsub.ts index 5193bcdca094..91b6409d878b 100644 --- a/packages/lodestar/src/network/gossip/gossipsub.ts +++ b/packages/lodestar/src/network/gossip/gossipsub.ts @@ -456,6 +456,31 @@ export class Eth2Gossipsub extends Gossipsub { } } } + + // track gossip peer score + let peerCountScoreGraylist = 0; + let peerCountScorePublish = 0; + let peerCountScoreGossip = 0; + let peerCountScoreMesh = 0; + const {graylistThreshold, publishThreshold, gossipThreshold} = gossipScoreThresholds; + const {scoreByThreshold, score: scoreMetric} = metrics.gossipPeer; + const gossipScores = []; + + for (const peerIdStr of this.peers.keys()) { + const score = this.score.score(peerIdStr); + if (score >= graylistThreshold) peerCountScoreGraylist++; + if (score >= publishThreshold) peerCountScorePublish++; + if (score >= gossipThreshold) peerCountScoreGossip++; + if (score >= 0) peerCountScoreMesh++; + gossipScores.push(score); + } + + scoreByThreshold.set({threshold: "graylist"}, peerCountScoreGraylist); + scoreByThreshold.set({threshold: "publish"}, peerCountScorePublish); + scoreByThreshold.set({threshold: "gossip"}, peerCountScoreGossip); + scoreByThreshold.set({threshold: "mesh"}, peerCountScoreMesh); + + scoreMetric.set(gossipScores); } }