Skip to content

Commit

Permalink
Prometheus balance (#232)
Browse files Browse the repository at this point in the history
* add isAccountBalanceBelowMinimum to client

* add 12 hours interval to balance report

* if no threshold file, default to zero

* remove unused import

* group metrics to file

* get balance metric every 30min

* excessive line spacer

* remove typo
  • Loading branch information
ngmachado committed Jul 11, 2023
1 parent d7b09dc commit b7b264c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
24 changes: 24 additions & 0 deletions src/httpserver/metrics/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const promclient = require("prom-client");

// Collect default metrics
const collectDefaultMetrics = promclient.collectDefaultMetrics;
const register = new promclient.Registry();
collectDefaultMetrics({
app: 'sentinel-monitoring-app',
timeout: 10000,
gcDurationBuckets: [0.001, 0.01, 0.1, 1, 2, 5],
register
});

const accountBalanceGauge = new promclient.Gauge({
name: 'sentinel_account_balance',
help: 'Current balance of the Sentinel account'
});

// Register your custom metrics
register.registerMetric(accountBalanceGauge);

module.exports = {
register,
accountBalanceGauge
};
29 changes: 19 additions & 10 deletions src/httpserver/server.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const express = require("express");
const promclient = require('prom-client');

const { register, accountBalanceGauge } = require('./metrics/metrics'); // import the register and custom metrics
class HTTPServer {
constructor (app) {
this.app = app;
this.server = express();
this.port = this.app.config.METRICS_PORT;
const register = new promclient.Registry();
this.register = register;
promclient.collectDefaultMetrics({
app: 'sentinel-monitoring-app',
timeout: 10000,
gcDurationBuckets: [0.001, 0.01, 0.1, 1, 2, 5],
register
});

}

async updateAccountBalance() {
try {
const balance = await this.app.client.getAccountBalance();
accountBalanceGauge.set(balance);
} catch (e) {
console.error('Failed to update account balance:', e);
}
}

start () {
Expand Down Expand Up @@ -80,12 +80,21 @@ class HTTPServer {
res.send(await this.register.metrics());
});

this.balanceInterval = setInterval(() => {
this.updateAccountBalance();
}, 30 * 60 * 1000); // 30 minutes

this.runningInstance = this.server.listen(this.port, () => {
this.app.logger.info(`Metrics: listening via http on port ${this.port}`);
});



}

close () {
// stop the balance interval
clearInterval(this.balanceInterval);
this.runningInstance.close(() => {
console.debug("HTTP server closed");
});
Expand Down
1 change: 0 additions & 1 deletion src/services/notificationJobs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const BN = require("bn.js");
const {wad4human} = require("@decentral.ee/web3-helpers/src/math-utils");
const timeout = ms => new Promise(resolve => setTimeout(resolve, ms));
async function trigger (obj, ms) {
Expand Down

0 comments on commit b7b264c

Please sign in to comment.