Skip to content

Commit

Permalink
changes for metrics tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benzekrimaha committed Apr 18, 2024
1 parent 9ac860c commit 2c333f0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CountItems/CountMaster.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const async = require('async');
const { reshapeExceptionError } = require('arsenal').errorUtils;
const monitoring = require('../utils/monitoring');

class CountMaster {
constructor(params) {
this.log = params.log;
this.manager = params.manager;
this.client = params.client;
this.metrics = params.metrics;
}

stop(signal, callback) {
Expand Down Expand Up @@ -34,6 +36,7 @@ class CountMaster {
}
return next();
}),
next => this.metrics.start(next),
next => this.manager.setup(next),
next => this.client.getBucketInfos(this.log, (err, bucketList) => {
if (err) {
Expand All @@ -45,6 +48,7 @@ class CountMaster {
this.log.info('got buckets infos', {
bucketCount: bucketList.bucketCount,
});
monitoring.bucketCount.set(bucketList.bucketCount);
this.manager.addWork(bucketList);
return next();
}),
Expand Down
5 changes: 4 additions & 1 deletion CountItems/masterProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const CountMaster = require('./CountMaster');
const CountManager = require('./CountManager');
const createMongoParams = require('../utils/createMongoParams');
const createWorkers = require('./utils/createWorkers');

const { network } = require('arsenal');

Check failure on line 9 in CountItems/masterProcess.js

View workflow job for this annotation

GitHub Actions / tests

Expected 1 empty line after require statement not followed by another require

Check failure on line 9 in CountItems/masterProcess.js

View workflow job for this annotation

GitHub Actions / tests

`arsenal` import should occur before import of `../utils/S3UtilsMongoClient`
const WebServer = network.http.server;
const monitoring = require('../utils/monitoring');

Check failure on line 11 in CountItems/masterProcess.js

View workflow job for this annotation

GitHub Actions / tests

Expected 1 empty line after require statement not followed by another require
const logLevel = Number.parseInt(process.env.DEBUG, 10) === 1
? 'debug' : 'info';

Expand Down Expand Up @@ -35,6 +37,7 @@ const countMaster = new CountMaster({
maxConcurrent: concurrentCursors,
}),
client: new S3UtilsMongoClient(createMongoParams(log)),
metrics: new WebServer(8003, log).onRequest(monitoring.metricsHandler)

Check failure on line 40 in CountItems/masterProcess.js

View workflow job for this annotation

GitHub Actions / tests

Missing trailing comma
});

const handleSignal = sig => countMaster.stop(sig, () => process.exit(0));
Expand Down
57 changes: 57 additions & 0 deletions utils/monitoring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { errors } = require('arsenal');
const promClient = require('prom-client');

const collectDefaultMetrics = promClient.collectDefaultMetrics;

Check failure on line 4 in utils/monitoring.js

View workflow job for this annotation

GitHub Actions / tests

Use object destructuring

const bucketCount = new promClient.Gauge({
name: 'bucketCount_metrics',
help: 'Total number of buckets',
labelNames: ['bucketName'],
});

/**
* @param {http.ServerResponse} res - http response object
* @param {errors.ArsenalError} error - Error code
* @return {void}
*/
function _writeResponse(res, error) {
let statusCode = 200;
if (error) {
if (Number.isInteger(error.code)) {
statusCode = error.code;
} else {
statusCode = 500;
}
}
res.writeHead(statusCode, { 'Content-Type': 'application/json' });
res.end();
}

/**
* @param {http.IncomingMessage} req - http request object
* @param {http.ServerResponse} res - http response object
* @return {void}
*/
async function metricsHandler(req, res) {
if (req.method !== 'GET' || req.url !== '/metrics') {
return _writeResponse(res, errors.MethodNotAllowed);
}

try {
const promMetrics = await promClient.register.metrics();
const contentLen = Buffer.byteLength(promMetrics, 'utf8');
return res.writeHead(200, {
'content-length': contentLen,
'content-type': promClient.register.contentType,
}).end(promMetrics);
} catch (ex) {
return _writeResponse(res, errors.MethodNotAllowed);
}
}

module.exports = {
client: promClient,
collectDefaultMetrics,
metricsHandler,
bucketCount,
};

0 comments on commit 2c333f0

Please sign in to comment.