Skip to content

Commit

Permalink
BE-449 | metric changes
Browse files Browse the repository at this point in the history
Signed-off-by: manishdas12 <manishdas12@yahoo.in>
  • Loading branch information
manishdas12 committed Nov 7, 2023
1 parent 476b8f3 commit 9624de9
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/Explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { authCheckMiddleware } from './middleware/auth-check';
import swaggerDocument from './swagger.json';
import { ExplorerError } from './common/ExplorerError';
import { localLoginStrategy } from './passport/local-login';
// eslint-disable-next-line spellcheck/spell-checker
import { metricsRoutes } from './rest/metricsroutes';

/**
*
Expand Down Expand Up @@ -132,6 +134,10 @@ export class Explorer {
platform.initializeListener(explorerconfig.sync);

this.platforms.push(platform);
const metricsRouter = Express.Router();
// Initializing metrics services
await metricsRoutes(metricsRouter, platform);
this.app.use('/metrics', metricsRouter);
}
}

Expand Down
63 changes: 63 additions & 0 deletions app/metrics/collect-metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/

import { Proxy } from '../platform/fabric/Proxy';
import { Platform } from '../platform/fabric/Platform';
import {
metric_ledger_height,
ledger_blockchain_height,
ledger_transaction_count,
metric_node_up
} from './metrics';

export async function collectMetrics(platform: Platform) {
const proxy: Proxy = platform.getProxy();
const networks = await proxy.networkList();
for (const network of networks) {
const network_id = network.id;
// get all the channels info
const channelList = await proxy.getChannelsInfo(network_id);
for (const channelInfo of channelList) {
const channel_genesis = channelInfo.channel_genesis_hash;
ledger_blockchain_height
.labels({
channel: channelInfo.channelname,
channel_genesis_hash: channelInfo.channel_genesis_hash
})
.set(channelInfo.blocks);
ledger_transaction_count
.labels({
channel: channelInfo.channelname,
channel_genesis_hash: channelInfo.channel_genesis_hash
})
.inc();

// get the peer status and the ledger height
const peerStatus = await proxy.getPeersStatus(network_id, channel_genesis);
setLedgerHeight(peerStatus, channelInfo.channelname);
}
}
}

async function setLedgerHeight(peerStatus: any[], channel: string) {
for (const peer of peerStatus) {
if (peer.peer_type === 'PEER' && typeof peer.ledger_height_low === 'number') {
metric_ledger_height
.labels({
mspid: peer.mspid,
requests: peer.requests,
server_hostname: peer.server_hostname,
channel: channel
})
.set(peer.ledger_height_low);
}
let status = 0;
if (peer.status === 'UP') {
status = 1;
}
metric_node_up
.labels({ node: peer.server_hostname, mspid: peer.mspid })
.set(status);
}
}
42 changes: 42 additions & 0 deletions app/metrics/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/

import client from 'prom-client';

export const metric_ledger_height = new client.Gauge({
name: 'metric_ledger_height',
help: 'metric_ledger_height_low: returns ledger height of the peer nodes',
labelNames: ['channel', 'mspid', 'requests', 'server_hostname']
});

export const ledger_blockchain_height = new client.Gauge({
name: 'ledger_blockchain_height',
help: 'ledger_blockchain_height: returns block height of channel',
labelNames: ['channel', 'channel_genesis_hash']
});

export const ledger_transaction_count = new client.Counter({
name: 'ledger_transaction_count',
help: 'metric_channel_txns: returns transaction count on a channel',
labelNames: ['channel', 'channel_genesis_hash']
});

export const metric_node_up = new client.Gauge({
name: 'metric_node_up',
help: 'metric_node_up: returns status of peer and orderer node',
labelNames: ['node', 'mspid']
});

export const ledger_blockstorage_commit_time = new client.Histogram({
name: 'ledger_blockstorage_commit_time',
help:
'ledger_blockstorage_commit_time: Time taken in sec for committing block changes to state db.',
labelNames: ['channel', 'channel_genesis_hash']
});

export const fabric_version = new client.Gauge({
name: 'fabric_version',
help: 'fabric_version: show fabric versions',
labelNames: ['channel', 'channel_genesis_hash']
});
21 changes: 21 additions & 0 deletions app/rest/metricsroutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/

import { Router } from 'express';
import promClient from 'prom-client';
import { helper } from '../common/helper';
import { collectMetrics } from '../metrics/collect-metrics';

const logger = helper.getLogger('metricRoutes');
export async function metricsRoutes(router: Router, platform: any) {
// scrap metrics for every 5 seconds
setInterval(() => {
collectMetrics(platform);
}, 5 * 1000);

router.get('/', async (_, res) => {
logger.info('available metrics....');
res.send(await promClient.register.metrics());
});
}

0 comments on commit 9624de9

Please sign in to comment.