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

Prometheus balance #232

Merged
merged 9 commits into from
Jul 11, 2023
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
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