1
0
mirror of https://github.com/yldio/copilot.git synced 2024-12-05 01:20:08 +02:00
copilot/legacy/spikes/no-leak/scripts/prometheus.js

157 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-03-06 19:15:16 +02:00
const map = require('apr-map');
const forceArray = require('force-array');
const get = require('lodash.get');
const date = require('date.js');
const timestamp = require('internet-timestamp');
const got = require('got');
const url = require('url');
const qs = require('qs');
const transform = (res) => {
2017-03-06 19:15:16 +02:00
return forceArray(res).reduce((sum, { data }) => {
const result = !Array.isArray(data)
? data.result
: data;
return result.reduce((sum, inst) => {
const metric = !inst.job
? inst.metric
: inst;
const {
values = [],
value = []
} = inst;
const {
instance,
job,
__name__
} = metric;
const oldJob = get(sum, job, {});
2017-03-06 19:15:16 +02:00
const oldInstance = get(sum, `${job}.${instance}`, {});
const _value = values.length ? values : value
return Object.assign(sum, {
[job]: Object.assign(oldJob, {
2017-03-06 19:15:16 +02:00
[instance]: Object.assign(oldInstance, {
[__name__]: _value
})
})
})
}, sum);
}, {});
};
2017-03-06 19:15:16 +02:00
const range = module.exports.range = async ({
query = [],
ago = '24h ago',
2017-03-06 19:15:16 +02:00
step = '15s',
2017-03-20 10:38:10 +02:00
host = 'localhost:9090'
}) => {
const end = timestamp(new Date());
const start = timestamp(date(ago));
const ranges = await map(forceArray(query), async (query) => {
2017-03-06 19:15:16 +02:00
return await got(url.format({
protocol: 'http:',
slashes: true,
2017-03-20 10:38:10 +02:00
host: host,
pathname: '/api/v1/query_range',
query: {
query,
end,
step,
start
}
2017-03-06 19:15:16 +02:00
}))
});
return transform(
ranges.map((range) => JSON.parse(range.body))
);
};
2017-03-06 19:15:16 +02:00
const query = module.exports.query = async ({
2017-03-20 10:38:10 +02:00
host = 'localhost:9090',
query = []
}) => {
2017-03-06 19:15:16 +02:00
const res = await map(query, async (query) => {
return await got(url.format({
protocol: 'http:',
slashes: true,
2017-03-20 10:38:10 +02:00
host: host,
pathname: '/api/v1/query',
query: {
query: query
}
2017-03-06 19:15:16 +02:00
}))
});
return transform(
res.map((res) => JSON.parse(res.body))
);
};
2017-03-06 19:15:16 +02:00
const tree = module.exports.tree = async ({
2017-03-20 10:38:10 +02:00
host = 'localhost:9090',
query = []
}) => {
2017-03-06 19:15:16 +02:00
const res = await got(url.format({
protocol: 'http:',
slashes: true,
2017-03-20 10:38:10 +02:00
host: host,
pathname: '/api/v1/series',
search: qs.stringify({
match: query
}, {
arrayFormat: 'brackets'
})
2017-03-06 19:15:16 +02:00
}));
2017-03-07 19:04:56 +02:00
return transform(JSON.parse(res.body));
};
if (!module.parent) {
process.on('unhandledRejection', (reason) => {
throw reason
});
const usage = () => {
console.error(`
Usage: node metrics.js --type={type} --query={metric} --step={step} --ago={ago}
2017-03-06 19:15:16 +02:00
node scripts/prometheus.js --type=range --query=node_memory_heap_used_bytes --query=node_memory_heap_total_bytes
`.trim());
return process.exit(1);
}
const argv = require('minimist')(process.argv.slice(2));
if (!argv.query || !argv.type) {
return usage();
}
const handlers = {
tree,
range,
query
};
if (!handlers[argv.type]) {
return usage();
}
const conf = {
query: argv.query,
ago: argv.ago,
step: argv.step,
2017-03-20 10:38:10 +02:00
host: argv.host
};
handlers[argv.type](conf).then((res) => {
console.log(JSON.stringify(res, null, 2));
});
}