Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Prometheus metrics for indexer #531

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/graphql/src/application/uc/GetMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { client } from '~/graphql/GraphQLSDK';
import BlockDAO from '~/infra/dao/BlockDAO';

export default class GetMetrics {
async getFuelCoreLastBlockHeight() {
const { data } = await client.sdk.blocks({ last: 1 });
const blocks = data.blocks;
const [lastBlock] = blocks.nodes;
const height = Number(lastBlock?.header.height ?? '0');
return height;
}

async getIndexerLastBlockHeight() {
const blockDAO = new BlockDAO();
const latestBlock = await blockDAO.findLatestBlockAdded();
const blockId = latestBlock ? latestBlock.id : 0;
return blockId;
}

async execute(): Promise<any> {
const fuelCoreLastBlockHeight = await this.getFuelCoreLastBlockHeight();
const indexerLastBlockHeight = await this.getIndexerLastBlockHeight();
const indexerBlockHeightDelay =
fuelCoreLastBlockHeight - indexerLastBlockHeight;
const indexerHealth = indexerBlockHeightDelay < 100 ? 1 : 0;
return {
explorer_indexer_fuel_core_last_block_height: fuelCoreLastBlockHeight,
explorer_indexer_last_block_height_synced: indexerLastBlockHeight,
explorer_indexer_block_height_sync_delay: indexerBlockHeightDelay,
explorer_indexer_health: indexerHealth,
};
}
}
14 changes: 13 additions & 1 deletion packages/graphql/src/infra/server/App.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import cors from 'cors';
import express from 'express';
import express, { Request, Response } from 'express';
import GetMetrics from '~/application/uc/GetMetrics';

export class Server {
setup() {
const app = express();
app.use(cors<cors.CorsRequest>());
app.use(express.json());

app.get('/metrics', async (_req: Request, res: Response) => {
const getMetrics = new GetMetrics();
const output = await getMetrics.execute();
const lines: any = [];
for (const element in output) {
lines.push(`${element} ${output[element]}`);
}
res.setHeader('content-type', 'text/plain');
res.send(lines.join('\n'));
});
return app;
}

Expand Down
Loading