Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* Measure things that increment or decrement
*/
const counter = io.counter({
name: 'Downloads'
})
/**
* Now let's create some remote action
* And act on the Counter probe we just created
*/
io.action('decrement', { comment: 'Increment downloads' }, (cb) => {
counter.dec()
cb({ success: true })
})
io.action('increment', { comment : 'Decrement downloads' }, (cb) => {
// Increment the previous counter
counter.inc()
cb({ success: true })
})
io.action('throw error', { comment: 'Throw a random error ' }, (cb) => {
// Increment the previous counter
throw new Error('This error will be caught!')
})
io.action('send event', { comment: 'Sends an event' }, (cb) => {
io.emit('event:sent', {
msg: 'You sent a custom event!'
})
cb('Sent event!')
})
if (err) {
console.error('failed to remove from cache');
return rej(err);
}
res(quantity);
});
});
}
function clearCache(key) {
return removeFromCache('*');
}
let cacheSize = 0;
// action to manually clear the ad cache
if (process.env.NODE_ENV !== 'development') {
io.action('cache:clear', async cb => {
console.log('clearing cache');
try {
await clearCache();
cb({ success: true });
} catch (err) {
console.error(err);
cb({ success: false, err: err.message });
}
});
// setInterval(() => {
// cache.size((err, size) => {
// if (err) {
// console.error(err);
// return
// }
// Increment the previous counter
throw new Error('This error will be caught!')
})
io.action('send event', { comment: 'Sends an event' }, (cb) => {
io.emit('event:sent', {
msg: 'You sent a custom event!'
})
cb('Sent event!')
})
io.action('get env', (cb) => {
cb(process.env)
})
io.action('modules version', { comment: 'Get modules version' }, (cb) => {
cb(process.versions)
})
io.action('Action with params', { comment: 'Returns sent params' }, (data, cb) => {
// Replies the received data
cb(`Data received: ${JSON.stringify(data)}`)
})
/**
* Create an action that hit the HTTP server we just created
* So we can see how the meter probe behaves
*/
io.action('do:http:query', (cb) => {
const options = {
hostname: '127.0.0.1',
port: 5005,
})
io.action('modules version', { comment: 'Get modules version' }, (cb) => {
cb(process.versions)
})
io.action('Action with params', { comment: 'Returns sent params' }, (data, cb) => {
// Replies the received data
cb(`Data received: ${JSON.stringify(data)}`)
})
/**
* Create an action that hit the HTTP server we just created
* So we can see how the meter probe behaves
*/
io.action('do:http:query', (cb) => {
const options = {
hostname: '127.0.0.1',
port: 5005,
path: '/users',
method: 'GET',
headers: { 'Content-Type': 'application/json' }
}
const req = http.request(options, (res) => {
res.setEncoding('utf8')
res.on('data', (data) => {
console.log(data)
})
})
req.on('error', (e) => {
cb({ success: false, err: err.message });
}
});
// setInterval(() => {
// cache.size((err, size) => {
// if (err) {
// console.error(err);
// return
// }
// cacheSize = size;
// });
// }, 1000 * 10);
}
io.metric({
type: 'metric',
name: 'Cache size (bytes)',
value: () => {
return cacheSize;
}
});
}, function (err, conf) {
if (err) {
io.notifyError(err)
return process.exit(1)
}
const cpu = new CPUMetrics(io, conf) // eslint-disable-line
const network = new NetworkMetrics(io, conf) // eslint-disable-line
const disk = new DiskMetrics(io, conf) // eslint-disable-line
const memory = new MemoryMetrics(io, conf) // eslint-disable-line
const fd = new FSMetrics(io, conf) // eslint-disable-line
const tty = new TTYMetrics(io, conf) // eslint-disable-line
const processes = new ProcessesMetrics(io, conf) // eslint-disable-line
const actions = new MonitoringActions()
actions.expose(io)
})
], (err, data) => {
if (err) {
console.error(err)
return io.notifyError(err)
}
// lets update metrics
for (let i = 0, max = metrics.length; i < max; i++) {
var metric = metrics[i]
let stats = {}
// depending on the stats source
switch (metric.from) {
case 'cluster': {
stats = data[0][0]
break
}
case 'cluster_health': {
stats = data[2][0]
break
}
randomVariable++
}, 400)
io.metric({
name: 'Var count',
value: () => {
return randomVariable
}
})
/**
* Probe system #3 - Meter
*
* Probe things that are measured as events / interval.
*/
const meter = io.meter({
name: 'req/min',
timeframe: 60
})
/**
* Use case for Meter Probe
*
* Create a mock http server
*/
http.createServer((req, res) => {
// Then mark it at every connections
meter.mark()
res.end('Thanks')
}).listen(5005)
metrics.forEach(function (metric) {
// instanciate each probe
metric.probe = io.metric({
name: metric.name,
unit: metric.unit || ''
})
})
}
* Create a mock http server
*/
http.createServer((req, res) => {
// Then mark it at every connections
meter.mark()
res.end('Thanks')
}).listen(5005)
/**
* Probe system #4 - Counter
*
* Measure things that increment or decrement
*/
const counter = io.counter({
name: 'Downloads'
})
/**
* Now let's create some remote action
* And act on the Counter probe we just created
*/
io.action('decrement', { comment: 'Increment downloads' }, (cb) => {
counter.dec()
cb({ success: true })
})
io.action('increment', { comment : 'Decrement downloads' }, (cb) => {
// Increment the previous counter
counter.inc()
cb({ success: true })