Skip to content

Commit

Permalink
fixup! feat: metric for sync status
Browse files Browse the repository at this point in the history
  • Loading branch information
iadmytro committed Mar 21, 2023
1 parent ab8e163 commit 11d8696
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
26 changes: 23 additions & 3 deletions packages/cardano-services/src/Http/HttpServer.ts
@@ -1,11 +1,11 @@
import * as OpenApiValidator from 'express-openapi-validator';
import { DB_BLOCKS_BEHIND_TOLERANCE, listenPromise, serverClosePromise } from '../util';
import { Gauge, Registry } from 'prom-client';
import { HttpServerConfig, ServiceHealth, ServicesHealthCheckResponse } from './types';
import { HttpService } from './HttpService';
import { Logger } from 'ts-log';
import { ProviderError, ProviderFailure } from '@cardano-sdk/core';
import { RunnableModule, contextLogger, fromSerializableObject, toSerializableObject } from '@cardano-sdk/util';
import { listenPromise, serverClosePromise } from '../util';
import bodyParser from 'body-parser';
import express from 'express';
import expressPromBundle from 'express-prom-bundle';
Expand Down Expand Up @@ -157,19 +157,39 @@ export class HttpServer extends RunnableModule {
return serverClosePromise(this.server);
}

// eslint-disable-next-line sonarjs/cognitive-complexity
private async initMetrics() {
const promRegistry = new Registry();
for (const service of this.#dependencies.services) {
// '-' replaced with '_' to satisfy name validation rules
const name = `node_sync_percentage_${service.slug.replace('-', '_')}`;
const serviceName = service.slug.replace('-', '_');
// eslint-disable-next-line no-new
new Gauge<string>({
async collect() {
const currentValue = (await service.healthCheck()).localNode?.networkSync;
if (currentValue) this.set(currentValue);
},
help: `Node synchronization status - ${service.name}`,
name,
name: `node_sync_percentage_${serviceName}`,
registers: [promRegistry]
});

// eslint-disable-next-line no-new
new Gauge<string>({
async collect() {
const data = await service.healthCheck();
let syncStatus;
if (data.projectedTip && data.localNode?.ledgerTip) {
syncStatus =
(data.projectedTip.blockNo * 100) / (data.localNode?.ledgerTip?.blockNo - DB_BLOCKS_BEHIND_TOLERANCE);
}
if (syncStatus) {
if (syncStatus > 100) this.set(100);
else this.set(syncStatus);
}
},
help: `Projection synchronization status - ${service.name}`,
name: `projection_sync_percentage_${serviceName}`,
registers: [promRegistry]
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/cardano-services/test/Http/HttpServer.test.ts
Expand Up @@ -351,6 +351,7 @@ describe('HttpServer', () => {
expect(response.status).toBe(200);
expect(response.data.includes('healthcheck 1')).toEqual(true);
expect(response.data.includes('node_sync_percentage')).toEqual(true);
expect(response.data.includes('projection_sync_percentage')).toEqual(true);
});
it('metrics endpoint with unhealthy service', async () => {
const service = new SomeHttpService(ServiceNames.StakePool, provider, logger);
Expand Down
2 changes: 1 addition & 1 deletion packages/ogmios/src/CardanoNode/OgmiosCardanoNode.ts
Expand Up @@ -97,7 +97,7 @@ export class OgmiosCardanoNode extends RunnableModule implements CardanoNode {
return {
localNode: {
ledgerTip: lastKnownTip,
networkSync: Cardano.Percent(networkSynchronization)
networkSync: Cardano.Percent(networkSynchronization * 100)
},
ok: networkSynchronization > 0.99
};
Expand Down

0 comments on commit 11d8696

Please sign in to comment.