Skip to content

Commit

Permalink
Add metrics to show number of peers by client (#3571)
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths authored Jan 5, 2022
1 parent c384b36 commit 659e3e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/lodestar/src/metrics/metrics/lodestar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export function createLodestarMetrics(
help: "number of peers, labeled by direction",
labelNames: ["direction"],
}),
peersByClient: register.gauge<"client">({
name: "lodestar_peers_by_client_count",
help: "number of peers, labeled by client",
labelNames: ["client"],
}),
peersSync: register.gauge({
name: "lodestar_peers_sync_count",
help: "Current count of peers useful for sync",
Expand Down
9 changes: 8 additions & 1 deletion packages/lodestar/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {GoodByeReasonCode, GOODBYE_KNOWN_CODES, Libp2pEvent} from "../../constan
import {IMetrics} from "../../metrics";
import {NetworkEvent, INetworkEventBus} from "../events";
import {IReqResp, ReqRespMethod, RequestTypedContainer} from "../reqresp";
import {prettyPrintPeerId} from "../util";
import {prettyPrintPeerId, getClientFromPeerStore} from "../util";
import {ISubnetsService} from "../subnets";
import {Libp2pPeerMetadataStore} from "./metastore";
import {PeerDiscovery, SubnetDiscvQueryMs} from "./discover";
Expand Down Expand Up @@ -519,11 +519,14 @@ export class PeerManager {
private runPeerCountMetrics(metrics: IMetrics): void {
let total = 0;
const peersByDirection = new Map<string, number>();
const peersByClient = new Map<string, number>();
for (const connections of this.libp2p.connectionManager.connections.values()) {
const openCnx = connections.find((cnx) => cnx.stat.status === "open");
if (openCnx) {
const direction = openCnx.stat.direction;
peersByDirection.set(direction, 1 + (peersByDirection.get(direction) ?? 0));
const client = getClientFromPeerStore(openCnx.remotePeer, this.libp2p.peerStore.metadataBook);
peersByClient.set(client, 1 + (peersByClient.get(client) ?? 0));
total++;
}
}
Expand All @@ -532,6 +535,10 @@ export class PeerManager {
metrics.peersByDirection.set({direction}, peers);
}

for (const [client, peers] of peersByClient.entries()) {
metrics.peersByClient.set({client}, peers);
}

let syncPeers = 0;
for (const peer of this.connectedPeers.values()) {
if (peer.relevantStatus === RelevantPeerStatus.relevant) {
Expand Down

0 comments on commit 659e3e1

Please sign in to comment.