Skip to content

Commit 590a0f8

Browse files
committed
feature: improve network configuration
1 parent 5e9d79b commit 590a0f8

File tree

4 files changed

+88
-30
lines changed

4 files changed

+88
-30
lines changed

src/metrics/network.ts

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class NetworkMetric implements MetricsInterface {
1010
private metricFeature: MetricsFeature
1111

1212
private defaultConf = {
13-
ports: true,
13+
ports: false,
1414
traffic: true
1515
}
1616

@@ -22,7 +22,7 @@ export default class NetworkMetric implements MetricsInterface {
2222
config = MetricConfig.getConfig(config, this.defaultConf)
2323

2424
if (config.traffic) {
25-
this.catchTraffic()
25+
this.catchTraffic(config.traffic)
2626
}
2727

2828
if (config.ports) {
@@ -64,7 +64,7 @@ export default class NetworkMetric implements MetricsInterface {
6464
}
6565
}
6666

67-
catchTraffic () {
67+
catchTraffic (config) {
6868
let download = 0
6969
let upload = 0
7070
let up = '0 B/sec'
@@ -110,42 +110,54 @@ export default class NetworkMetric implements MetricsInterface {
110110

111111
interval.unref()
112112

113-
this.metricFeature.metric({
114-
name : 'Network Download',
115-
agg_type : 'sum',
116-
value : function () { return down }
117-
})
113+
if (config === true || config.download === true) {
114+
this.metricFeature.metric({
115+
name: 'Network Download',
116+
agg_type: 'sum',
117+
value: function () {
118+
return down
119+
}
120+
})
121+
}
118122

119-
this.metricFeature.metric({
120-
name : 'Network Upload',
121-
agg_type : 'sum',
122-
value : function () { return up }
123-
})
123+
if (config === true || config.upload === true) {
124+
this.metricFeature.metric({
125+
name: 'Network Upload',
126+
agg_type: 'sum',
127+
value: function () {
128+
return up
129+
}
130+
})
131+
}
124132

125-
const originalWrite = netModule.Socket.prototype.write
133+
if (config === true || config.upload === true) {
134+
const originalWrite = netModule.Socket.prototype.write
126135

127-
netModule.Socket.prototype.write = function (data) {
128-
if (data.length) {
129-
upload += data.length
136+
netModule.Socket.prototype.write = function (data) {
137+
if (data.length) {
138+
upload += data.length
139+
}
140+
return originalWrite.apply(this, arguments)
130141
}
131-
return originalWrite.apply(this, arguments)
132142
}
133143

134-
const originalRead = netModule.Socket.prototype.read
144+
if (config === true || config.download === true) {
145+
const originalRead = netModule.Socket.prototype.read
135146

136-
netModule.Socket.prototype.read = function () {
147+
netModule.Socket.prototype.read = function () {
137148

138-
if (!this.monitored) {
139-
this.monitored = true
149+
if (!this.monitored) {
150+
this.monitored = true
140151

141-
this.on('data', function (data) {
142-
if (data.length) {
143-
download += data.length
144-
}
145-
})
146-
}
152+
this.on('data', function (data) {
153+
if (data.length) {
154+
download += data.length
155+
}
156+
})
157+
}
147158

148-
return originalRead.apply(this, arguments)
159+
return originalRead.apply(this, arguments)
160+
}
149161
}
150162
}
151163
}

test/fixtures/features/networkChild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Metric from '../../../src/features/metrics'
22

33
const metric = new Metric()
4-
metric.init({network: true}, true)
4+
metric.init({network: {ports: true}}, true)
55

66
const httpModule = require('http')
77

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Metric from '../../../src/features/metrics'
2+
3+
const metric = new Metric()
4+
metric.init({network: {traffic: {upload: true}}}, true)
5+
6+
const httpModule = require('http')
7+
8+
let timer
9+
10+
const server = httpModule.createServer((req, res) => {
11+
res.writeHead(200)
12+
res.end('hey')
13+
}).listen(3002, () => {
14+
timer = setInterval(function () {
15+
httpModule.get('http://localhost:' + server.address().port)
16+
httpModule.get('http://localhost:' + server.address().port + '/toto')
17+
}, 100)
18+
})
19+
20+
process.on('SIGINT', function () {
21+
clearInterval(timer)
22+
server.close()
23+
metric.destroy()
24+
})

test/metrics/network.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,26 @@ describe('Network', function () {
3535
}
3636
})
3737
})
38+
39+
it('should only send upload data', (done) => {
40+
const child = fork(SpecUtils.buildTestPath('fixtures/features/networkWithoutDownloadChild.js'))
41+
42+
child.on('message', pck => {
43+
44+
if (pck.type === 'axm:monitor' && pck.data['Network Upload'].value !== '0 B/sec') {
45+
46+
expect(pck.data.hasOwnProperty('Network Download')).to.equal(false)
47+
48+
expect(pck.data.hasOwnProperty('Network Upload')).to.equal(true)
49+
expect(pck.data['Network Upload'].agg_type).to.equal('sum')
50+
expect(pck.data['Network Upload'].historic).to.equal(true)
51+
expect(pck.data['Network Upload'].type).to.equal('Network Upload')
52+
53+
expect(pck.data.hasOwnProperty('Open ports')).to.equal(false)
54+
55+
child.kill('SIGINT')
56+
done()
57+
}
58+
})
59+
})
3860
})

0 commit comments

Comments
 (0)