-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: add network metrics + test #26
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import * as netModule from 'net' | ||
import MetricsFeature from '../features/metrics' | ||
import MetricsInterface from './metricsInterface' | ||
import MetricConfig from '../utils/metricConfig' | ||
|
||
import debug from 'debug' | ||
debug('axm:network') | ||
|
||
export default class NetworkMetric implements MetricsInterface { | ||
private metricFeature: MetricsFeature | ||
|
||
private defaultConf = { | ||
ports: true, | ||
traffic: true | ||
} | ||
|
||
constructor (metricFeature: MetricsFeature) { | ||
this.metricFeature = metricFeature | ||
} | ||
|
||
init (config?) { | ||
config = MetricConfig.getConfig(config, this.defaultConf) | ||
|
||
if (config.traffic) { | ||
this.catchTraffic() | ||
} | ||
|
||
if (config.ports) { | ||
this.catchPorts() | ||
} | ||
} | ||
|
||
destroy () { | ||
debug('NetworkMetric destroyed !') | ||
} | ||
|
||
catchPorts () { | ||
const portsList: Array<any> = [] | ||
let openedPorts = 'N/A' | ||
|
||
this.metricFeature.metric({ | ||
name : 'Open ports', | ||
value : function () { return openedPorts } | ||
}) | ||
|
||
const originalListen = netModule.Server.prototype.listen | ||
|
||
netModule.Server.prototype.listen = function () { | ||
const port = parseInt(arguments[0], 10) | ||
|
||
if (!isNaN(port) && portsList.indexOf(port) === -1) { | ||
portsList.push(port) | ||
openedPorts = portsList.sort().join() | ||
} | ||
|
||
this.once('close', function () { | ||
if (portsList.indexOf(port) > -1) { | ||
portsList.splice(portsList.indexOf(port), 1) | ||
openedPorts = portsList.sort().join() | ||
} | ||
}) | ||
|
||
return originalListen.apply(this, arguments) | ||
} | ||
} | ||
|
||
catchTraffic () { | ||
let download = 0 | ||
let upload = 0 | ||
let up = '0 B/sec' | ||
let down = '0 B/sec' | ||
|
||
const filter = function (bytes) { | ||
let toFixed = 0 | ||
|
||
if (bytes < 1024) { | ||
toFixed = 6 | ||
} else if (bytes < (1024 * 1024)) { | ||
toFixed = 3 | ||
} else if (bytes !== 0) { | ||
toFixed = 2 | ||
} | ||
|
||
bytes = (bytes / (1024 * 1024)).toFixed(toFixed) | ||
|
||
let cutZeros = 0 | ||
|
||
for (let i = (bytes.length - 1); i > 0; --i) { | ||
if (bytes[i] === '.') { | ||
++cutZeros | ||
break | ||
} | ||
if (bytes[i] !== '0') break | ||
++cutZeros | ||
} | ||
|
||
if (cutZeros > 0) { | ||
bytes = bytes.slice(0, -(cutZeros)) | ||
} | ||
|
||
return (bytes + ' MB/s') | ||
} | ||
|
||
const interval = setInterval(function () { | ||
up = filter(upload) | ||
down = filter(download) | ||
upload = 0 | ||
download = 0 | ||
}, 999) | ||
|
||
interval.unref() | ||
|
||
this.metricFeature.metric({ | ||
name : 'Network Download', | ||
agg_type : 'sum', | ||
value : function () { return down } | ||
}) | ||
|
||
this.metricFeature.metric({ | ||
name : 'Network Upload', | ||
agg_type : 'sum', | ||
value : function () { return up } | ||
}) | ||
|
||
const originalWrite = netModule.Socket.prototype.write | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to see why this patching break the AMQP clients ? keymetrics/pmx#52 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem seems to be from amqp lib. I add a configuration system to disable only the upload feature if necessary. |
||
|
||
netModule.Socket.prototype.write = function (data) { | ||
if (data.length) { | ||
upload += data.length | ||
} | ||
return originalWrite.apply(this, arguments) | ||
} | ||
|
||
const originalRead = netModule.Socket.prototype.read | ||
|
||
netModule.Socket.prototype.read = function () { | ||
|
||
if (!this.monitored) { | ||
this.monitored = true | ||
|
||
this.on('data', function (data) { | ||
if (data.length) { | ||
download += data.length | ||
} | ||
}) | ||
} | ||
|
||
return originalRead.apply(this, arguments) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Metric from '../../../src/features/metrics' | ||
|
||
const metric = new Metric() | ||
metric.init({network: true}, true) | ||
|
||
const httpModule = require('http') | ||
|
||
let timer | ||
|
||
const server = httpModule.createServer((req, res) => { | ||
res.writeHead(200) | ||
res.end('hey') | ||
}).listen(3002, () => { | ||
timer = setInterval(function () { | ||
httpModule.get('http://localhost:' + server.address().port) | ||
httpModule.get('http://localhost:' + server.address().port + '/toto') | ||
}, 100) | ||
}) | ||
|
||
process.on('SIGINT', function () { | ||
clearInterval(timer) | ||
server.close() | ||
metric.destroy() | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect, assert } from 'chai' | ||
import 'mocha' | ||
|
||
import SpecUtils from '../fixtures/utils' | ||
import { fork, exec } from 'child_process' | ||
|
||
describe('Network', function () { | ||
this.timeout(5000) | ||
|
||
it('should send network data', (done) => { | ||
const child = fork(SpecUtils.buildTestPath('fixtures/features/networkChild.js')) | ||
|
||
child.on('message', pck => { | ||
|
||
if (pck.type === 'axm:monitor' && pck.data['Network Download'].value !== '0 B/sec') { | ||
|
||
expect(pck.data.hasOwnProperty('Network Download')).to.equal(true) | ||
expect(pck.data['Network Download'].agg_type).to.equal('sum') | ||
expect(pck.data['Network Download'].historic).to.equal(true) | ||
expect(pck.data['Network Download'].type).to.equal('Network Download') | ||
|
||
expect(pck.data.hasOwnProperty('Network Upload')).to.equal(true) | ||
expect(pck.data['Network Upload'].agg_type).to.equal('sum') | ||
expect(pck.data['Network Upload'].historic).to.equal(true) | ||
expect(pck.data['Network Upload'].type).to.equal('Network Upload') | ||
|
||
expect(pck.data.hasOwnProperty('Open ports')).to.equal(true) | ||
expect(pck.data['Open ports'].value).to.equal('3002') | ||
expect(pck.data['Open ports'].agg_type).to.equal('avg') | ||
expect(pck.data['Open ports'].historic).to.equal(true) | ||
expect(pck.data['Open ports'].type).to.equal('Open ports') | ||
|
||
child.kill('SIGINT') | ||
done() | ||
} | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If i remember we decided to drop this metrics since not really useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add some configuration, so it's disable by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fine then