1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-11 05:40:11 +02:00
copilot/spikes/leak/scripts/prometheus.js

160 lines
3.3 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 = '1h ago',
2017-03-06 19:15:16 +02:00
step = '15s',
hostname = 'localhost'
}) => {
const end = timestamp(new Date());
const start = timestamp(date(ago));
2017-03-06 19:15:16 +02:00
const ranges = await map(query, async (query) => {
return await got(url.format({
protocol: 'http:',
slashes: true,
port: '9090',
hostname: hostname,
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 ({
hostname = 'localhost',
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,
port: '9090',
hostname: hostname,
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 ({
hostname = 'localhost',
query = []
}) => {
2017-03-06 19:15:16 +02:00
const res = await got(url.format({
protocol: 'http:',
slashes: true,
port: '9090',
hostname: hostname,
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,
hostname: argv.hostname
};
handlers[argv.type](conf).then((res) => {
console.log(JSON.stringify(res, null, 2));
});
}