Cleaner response

This commit is contained in:
Nemo 2018-06-04 11:46:11 +05:30
parent e828ffa4ac
commit a0bf669132
3 changed files with 152 additions and 32 deletions

View File

@ -11,3 +11,95 @@ act_fup_usage_gigabytes_total 41.42
# TYPE act_fup_max_gigabytes_total gauge
act_fup_max_gigabytes_total 500
```
It also exposes some nodeJS metrics:
```
# HELP process_cpu_user_seconds_total Total user CPU time spent in seconds.
# TYPE process_cpu_user_seconds_total counter
process_cpu_user_seconds_total 0.001233 1528092827015
# HELP process_cpu_system_seconds_total Total system CPU time spent in seconds.
# TYPE process_cpu_system_seconds_total counter
process_cpu_system_seconds_total 0.000114 1528092827015
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.001347 1528092827015
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1528092827
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 39550976 1528092827028
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 1074991104 1528092827028
# HELP process_heap_bytes Process heap size in bytes.
# TYPE process_heap_bytes gauge
process_heap_bytes 88317952 1528092827028
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 13 1528092827026
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 358146
# HELP nodejs_eventloop_lag_seconds Lag of event loop in seconds.
# TYPE nodejs_eventloop_lag_seconds gauge
nodejs_eventloop_lag_seconds 0.011319479 1528092827027
# HELP nodejs_active_handles_total Number of active handles.
# TYPE nodejs_active_handles_total gauge
nodejs_active_handles_total 2 1528092827016
# HELP nodejs_active_requests_total Number of active requests.
# TYPE nodejs_active_requests_total gauge
nodejs_active_requests_total 4 1528092827016
# HELP nodejs_heap_size_total_bytes Process heap size from node.js in bytes.
# TYPE nodejs_heap_size_total_bytes gauge
nodejs_heap_size_total_bytes 19193856 1528092827016
# HELP nodejs_heap_size_used_bytes Process heap size used from node.js in bytes.
# TYPE nodejs_heap_size_used_bytes gauge
nodejs_heap_size_used_bytes 8858072 1528092827016
# HELP nodejs_external_memory_bytes Nodejs external memory size in bytes.
# TYPE nodejs_external_memory_bytes gauge
nodejs_external_memory_bytes 42604 1528092827016
# HELP nodejs_heap_space_size_total_bytes Process heap space size total from node.js in bytes.
# TYPE nodejs_heap_space_size_total_bytes gauge
nodejs_heap_space_size_total_bytes{space="new"} 8388608 1528092827016
nodejs_heap_space_size_total_bytes{space="old"} 7639040 1528092827016
nodejs_heap_space_size_total_bytes{space="code"} 1048576 1528092827016
nodejs_heap_space_size_total_bytes{space="map"} 544768 1528092827016
nodejs_heap_space_size_total_bytes{space="large_object"} 1572864 1528092827016
# HELP nodejs_heap_space_size_used_bytes Process heap space size used from node.js in bytes.
# TYPE nodejs_heap_space_size_used_bytes gauge
nodejs_heap_space_size_used_bytes{space="new"} 1038624 1528092827016
nodejs_heap_space_size_used_bytes{space="old"} 6219304 1528092827016
nodejs_heap_space_size_used_bytes{space="code"} 883776 1528092827016
nodejs_heap_space_size_used_bytes{space="map"} 474400 1528092827016
nodejs_heap_space_size_used_bytes{space="large_object"} 249120 1528092827016
# HELP nodejs_heap_space_size_available_bytes Process heap space size available from node.js in bytes.
# TYPE nodejs_heap_space_size_available_bytes gauge
nodejs_heap_space_size_available_bytes{space="new"} 3086048 1528092827016
nodejs_heap_space_size_available_bytes{space="old"} 845168 1528092827016
nodejs_heap_space_size_available_bytes{space="code"} 0 1528092827016
nodejs_heap_space_size_available_bytes{space="map"} 0 1528092827016
nodejs_heap_space_size_available_bytes{space="large_object"} 1481833984 1528092827016
# HELP nodejs_version_info Node.js version info.
# TYPE nodejs_version_info gauge
nodejs_version_info{version="v10.3.0",major="10",minor="3",patch="0"} 1
```

View File

@ -4,44 +4,66 @@ const MY_PACKAGE_SELECTOR_ID =
'table[style="margin-top:-10px;"] tr:first-child+tr';
const DATA_SELECTOR = 'packagecol3';
const REGEX = /\d+\.\d{0,2}/g;
const DATA_USAGE_REGEX = /\d+\.\d{0,2}/g;
var browser;
async function getUsage() {
var options = {
executablePath:
process.env['CHROME_BIN'] || '/usr/bin/chromium-snapshot-bin',
args: ['--no-sandbox', '--disable-setuid-sandbox'].concat(
process.env.hasOwnProperty['PROXY_SERVER']
? [`--proxy-server=${process.env['PROXY_SERVER']}`]
: []
),
};
let metrics = {
used: null,
total: null,
};
const page = await browser.newPage();
try {
const page = await browser.newPage();
await page.goto('http://portal.actcorp.in/group/blr/myaccount');
await page.click(MY_PACKAGE_SELECTOR_ID);
await page.waitFor(3000),
(text = await page.evaluate(sel => {
return document.getElementsByClassName(sel)[3].innerText;
}, DATA_SELECTOR));
[metrics.used, metrics.total] = text.match(REGEX).map(x => parseFloat(x));
[metrics.used, metrics.total] = text
.match(DATA_USAGE_REGEX)
.map(x => parseFloat(x));
} catch (e) {
console.log("Could'nt scrape ACT page, faced an error");
console.log("Couldn't scrape ACT page, faced an error");
console.log(e);
return null;
} finally {
page.close();
return metrics;
}
}
(async () => {
const browser = await puppeteer.launch(args);
module.exports = {
getUsage: getUsage,
function chromeLaunchConfig() {
var options = {
args: ['--no-sandbox', '--disable-setuid-sandbox'].concat(
process.env.hasOwnProperty('PROXY_SERVER')
? [`--proxy-server=${process.env['PROXY_SERVER']}`]
: []
),
};
if (process.env.hasOwnProperty('DISABLE_HEADLESS')) {
options.headless = false;
}
if (process.env.hasOwnProperty('CHROME_BIN')) {
options.executablePath = process.env['CHROME_BIN'];
}
console.log('Launching Chrome with args:');
console.log(options);
return options;
}
// Async IIFE FTW
(async () => {
browser = await puppeteer.launch(chromeLaunchConfig());
console.log('Browser Initialized');
})();
module.exports = {
getUsage: getUsage,
};

View File

@ -3,7 +3,7 @@ const port = 3000;
const pClient = require('prom-client');
const metrics = require('./index');
const collectDefaultMetrics = pClient.collectDefaultMetrics;
pClient.collectDefaultMetrics({ timeout: 60000 });
const requestHandler = async (req, res) => {
let date = new Date(Date.now()).toLocaleString();
@ -11,20 +11,26 @@ const requestHandler = async (req, res) => {
switch (req.url) {
case '/metrics':
let m = await metrics.getUsage();
let usedGauge = new pClient.Gauge({
name: 'act_fup_usage_gigabytes_total',
help: 'ACT current usage in GB',
});
usedGauge.set(m.used);
let totalGauge = new pClient.Gauge({
name: 'act_fup_max_gigabytes_total',
help: 'ACT FUP limit in GB',
});
totalGauge.set(m.total);
let register = pClient.register;
// TODO: Switch to the correct err, res pattern with promise
if (m !== null) {
let usedGauge = new pClient.Gauge({
name: 'act_fup_usage_gigabytes_total',
help: 'ACT current usage in GB',
});
usedGauge.set(m.used);
let totalGauge = new pClient.Gauge({
name: 'act_fup_max_gigabytes_total',
help: 'ACT FUP limit in GB',
});
totalGauge.set(m.total);
let register = pClient.register;
res.setHeader('Content-Type', register.contentType);
res.end(register.metrics());
} else {
res.sendStatus(500);
res.end('Scrape failed');
}
res.setHeader('Content-Type', register.contentType);
res.end(register.metrics());
break;
default:
break;