Skip to content
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

Merged
merged 2 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions src/metrics/network.ts
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 () {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine then

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Would be great if we can fix it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem seems to be from amqp lib.
If I remove the line
this.on('data')
then the amqp server is responding, else we got a ECONNRESET error.

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)
}
}
}
2 changes: 2 additions & 0 deletions src/services/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import EventLoopDelayMetric from '../metrics/eventLoopDelay'
import MetricConfig from '../utils/metricConfig'
import EventLoopHandlesRequestsMetric from '../metrics/eventLoopHandlesRequests'
import Transaction from '../metrics/transaction'
import NetworkMetric from '../metrics/network'

debug('axm:metricService')

Expand All @@ -26,6 +27,7 @@ export default class MetricsService {
this.services.set('eventLoopDelay', new EventLoopDelayMetric(metricsFeature))
this.services.set('eventLoopActive', new EventLoopHandlesRequestsMetric(metricsFeature))
this.services.set('transaction', new Transaction(metricsFeature))
this.services.set('network', new NetworkMetric(metricsFeature))
}

init (config?, force?) {
Expand Down
24 changes: 24 additions & 0 deletions test/fixtures/features/networkChild.ts
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()
})
6 changes: 2 additions & 4 deletions test/fixtures/features/tracingChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ const httpModule = require('http')
// test http outbound
let timer

app.get('/', function (req, res){
console.log('home')
app.get('/', function (req, res) {
res.send('home')
})

app.get('/toto', function (req, res){
console.log('toto')
app.get('/toto', function (req, res) {
res.send('toto')
})

Expand Down
38 changes: 38 additions & 0 deletions test/metrics/network.spec.ts
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()
}
})
})
})