Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit fa518b1

Browse files
committed
feat: metrics (WIP) - feat: Dockerfile - fix/feat: various other things
1 parent 4a1ffcc commit fa518b1

File tree

8 files changed

+56
-18
lines changed

8 files changed

+56
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test/setup/tmp-disposable-nodes-addrs.json
77
dist
88
coverage
99
**/*.swp
10+
**/*.bak
1011
examples/sub-module/**/bundle.js
1112
examples/sub-module/**/*-minified.js
1213
examples/sub-module/*-bundle.js

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM node:8
2+
COPY . /src
3+
WORKDIR /src
4+
RUN npm i --production
5+
CMD ["npm", "start"]
6+
EXPOSE 9090

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"websockets-star": "src/bin.js"
88
},
99
"scripts": {
10-
"rendezvous": "node sig-server/src/bin.js",
10+
"rendezvous": "node src/bin.js",
11+
"start": "node src/bin.js",
1112
"lint": "aegir-lint",
1213
"test": "aegir-test --env node",
1314
"release": "aegir-release --env no-build",
@@ -26,6 +27,7 @@
2627
"async": "^2.5.0",
2728
"data-queue": "0.0.2",
2829
"debug": "^3.0.1",
30+
"epimetheus": "^1.0.54",
2931
"hapi": "^16.6.0",
3032
"inert": "^4.2.1",
3133
"libp2p-crypto": "^0.10.3",
@@ -36,6 +38,7 @@
3638
"once": "^1.4.0",
3739
"peer-id": "^0.10.1",
3840
"peer-info": "^0.11.0",
41+
"prom-client": "^10.1.0",
3942
"socket.io": "^2.0.3",
4043
"socket.io-client": "^2.0.3",
4144
"socket.io-pull-stream": "^0.1.1",

src/bin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ signalling.start({
1111
port: argv.port || argv.p || process.env.PORT || 9090,
1212
host: argv.host || argv.h || process.env.HOST || '0.0.0.0',
1313
cryptoChallenge: !(argv.disableCryptoChallenge || process.env.DISABLE_CRYPTO_CHALLENGE),
14-
strictMultiaddr: !(argv.disableStrictMultiaddr || process.env.DISABLE_STRICT_MULTIADDR)
14+
strictMultiaddr: !(argv.disableStrictMultiaddr || process.env.DISABLE_STRICT_MULTIADDR),
15+
metrics: true
1516
}, (err, _server) => {
1617
if (err) {
1718
throw err

src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ module.exports = {
1919
},
2020
refreshPeerListIntervalMS: 10000,
2121
cryptoChallenge: true,
22-
strictMultiaddr: true
22+
strictMultiaddr: false,
23+
metrics: false
2324
}

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Hapi = require('hapi')
44
const merge = require('merge-recursive').recursive
55
const path = require('path')
6+
const epimetheus = require('epimetheus')
67

78
exports = module.exports
89

@@ -53,5 +54,7 @@ exports.start = (options, callback) => {
5354
})
5455
})
5556

57+
if (config.metrics) epimetheus.instrument(http)
58+
5659
return http
5760
}

src/routes.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ const SocketIO = require('socket.io')
44
const sp = require('socket.io-pull-stream')
55
const util = require('./utils')
66
const uuid = require('uuid')
7+
const client = require("prom-client")
8+
const fake = {
9+
gauge: {
10+
set: () => {}
11+
},
12+
counter: {
13+
inc: () => {}
14+
}
15+
}
716

817
module.exports = (config, http) => {
918
const log = config.log
1019
const io = new SocketIO(http.listener)
11-
const proto = new util.Protocol(config.log)
20+
const proto = new util.Protocol(log)
1221

1322
proto.addRequest('ss-join', ['multiaddr', 'string', 'function'], join)
1423
proto.addRequest('ss-leave', ['multiaddr'], leave)
@@ -18,17 +27,21 @@ module.exports = (config, http) => {
1827

1928
log('create new server', config)
2029

21-
const peers = this._peers = {}
30+
this._peers = {}
2231
const nonces = {}
2332

33+
const peers_metric = config.metrics ? new client.Gauge({ name: 'rendezvous_peers', help: 'peers online now' }) : fake.gauge
34+
const dials_success_total = config.metrics ? new client.Counter({ name: 'rendezvous_dials_total_success', help: 'sucessfully completed dials since server started' }) : fake.counter
35+
const dials_failure_total = config.metrics ? new client.Counter({ name: 'rendezvous_dials_total_failure', help: 'failed dials since server started' }) : fake.counter
36+
const dials_total = config.metrics ? new client.Counter({ name: 'rendezvous_dials_total', help: 'all dials since server started' }) : fake.counter
37+
2438
const getPeers = () => this._peers
39+
const refreshMetrics = () => peers_metric.set(Object.keys(getPeers()).length)
2540

26-
this.peers = () => {
27-
return peers
28-
}
41+
this.peers = () => getPeers()
2942

3043
function safeEmit (addr, event, arg) {
31-
const peer = peers[addr]
44+
const peer = getPeers()[addr]
3245
if (!peer) {
3346
log('trying to emit %s but peer is gone', event)
3447
return
@@ -95,6 +108,7 @@ module.exports = (config, http) => {
95108
function joinFinalize (socket, multiaddr, cb) {
96109
const log = config.log.bind(config.log, '[' + socket.id + ']')
97110
getPeers()[multiaddr] = socket
111+
refreshMetrics()
98112
socket.addrs.push(multiaddr)
99113
log('registered as', multiaddr)
100114

@@ -104,6 +118,7 @@ module.exports = (config, http) => {
104118

105119
socket.once('ss-leave', function handleLeave (ma) {
106120
if (ma === multiaddr) {
121+
refreshMetrics()
107122
socket.addrs = socket.addrs.filter(m => m !== ma)
108123
stopSendingPeers()
109124
} else {
@@ -136,41 +151,48 @@ module.exports = (config, http) => {
136151
}
137152

138153
function leave (socket, multiaddr) {
139-
if (peers[multiaddr]) {
140-
delete peers[multiaddr]
154+
if (getPeers()[multiaddr]) {
155+
delete getPeers()[multiaddr]
141156
}
142157
}
143158

144159
function disconnect (socket) {
145-
Object.keys(peers).forEach((mh) => {
146-
if (peers[mh].id === socket.id) {
147-
delete peers[mh]
160+
Object.keys(getPeers()).forEach((mh) => {
161+
if (getPeers()[mh].id === socket.id) {
162+
delete getPeers()[mh]
148163
}
149164
})
165+
refreshMetrics()
150166
}
151167

152168
function dial (socket, from, to, dialId, cb) {
153169
const log = config.log.bind(config.log, '[' + dialId + ']')
154170
const s = socket.addrs.filter((a) => a === from)[0]
155171

172+
dials_total.inc()
173+
156174
if (!s) {
157-
return cb(new Error('Not authorized for this address'))
175+
dials_failure_total.inc()
176+
return cb('Not authorized for this address')
158177
}
159178

160179
log(from, 'is dialing', to)
161-
const peer = peers[to]
180+
const peer = getPeers()[to]
162181

163182
if (!peer) {
164-
return cb(new Error('Peer not found'))
183+
dials_failure_total.inc()
184+
return cb('Peer not found')
165185
}
166186

167187
socket.createProxy(dialId + '.dialer', peer)
168188

169189
peer.emit('ss-incomming', dialId, from, err => {
170190
if (err) {
191+
dials_failure_total.inc()
171192
return cb(err)
172193
}
173194

195+
dials_success_total.inc()
174196
peer.createProxy(dialId + '.listener', socket)
175197
cb()
176198
})

test/rendezvous.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ describe('rendezvous', () => {
5858
port: 12345,
5959
refreshPeerListIntervalMS: 1000,
6060
cryptoChallenge: false,
61-
strictMultiaddr: false
61+
strictMultiaddr: false,
62+
metrics: true
6263
}
6364

6465
rendezvous.start(options, (err, server) => {

0 commit comments

Comments
 (0)