Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 8f0254e

Browse files
victorbdaviddias
authored andcommitted
feat: add instrumentation
1 parent 54be08b commit 8f0254e

File tree

8 files changed

+94
-7
lines changed

8 files changed

+94
-7
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM node:6
2+
3+
RUN apt-get update
4+
RUN apt-get install --yes python2.7 git-all pkg-config libncurses5-dev libssl-dev libnss3-dev libexpat-dev libc6
5+
6+
WORKDIR /usr/src/app
7+
8+
COPY package.json /usr/src/app/package.json
9+
10+
RUN npm install
11+
RUN npm install wrtc
12+
13+
COPY . /usr/src/app
14+
15+
ENV IPFS_WRTC_LINUX_WINDOWS=1
16+
ENV IPFS_BOOTSTRAP=1
17+
ENV IPFS_MONITORING=1
18+
ENV IPFS_PATH=/root/.jsipfs
19+
20+
EXPOSE 4002
21+
EXPOSE 4003
22+
EXPOSE 5002
23+
EXPOSE 9090
24+
25+
CMD ./init-and-daemon.sh

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,20 @@ src # Main source code folder
520520
└── ...
521521
```
522522

523+
### Monitoring
524+
525+
The HTTP API exposed with js-ipfs can also be used for exposing metrics about
526+
the running js-ipfs node and other nodejs metrics.
527+
528+
To enable it, you need to set the environment variable `IPFS_MONITORING` (any value)
529+
530+
Once environment variable is set and the js-ipfs daemon is running, you can get
531+
the metrics (in prometheus format) by making a GET request to the following endpoint:
532+
533+
```
534+
http://localhost:5002/debug/metrics/prometheus
535+
```
536+
523537
### IPFS Core Architecture
524538

525539
![](/img/core.png)

init-and-daemon.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /bin/sh -e
2+
node src/cli/bin.js init
3+
4+
sed -i.bak 's/127.0.0.1/0.0.0.0/g' $IPFS_PATH/config
5+
6+
node src/cli/bin.js daemon

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@
153153
"update-notifier": "^2.2.0",
154154
"yargs": "8.0.2"
155155
},
156+
"optionalDependencies": {
157+
"prom-client": "^10.0.2",
158+
"prometheus-gc-stats": "^0.5.0"
159+
},
156160
"contributors": [
157161
"Andrew de Andrade <[email protected]>",
158162
"CHEVALAY JOSSELIN <[email protected]>",
@@ -161,7 +165,6 @@
161165
"Daniel J. O'Quinn <[email protected]>",
162166
"Daniela Borges Matos de Carvalho <[email protected]>",
163167
"David Dias <[email protected]>",
164-
"Dzmitry Das <[email protected]>",
165168
"Enrico Marino <[email protected]>",
166169
"Felix Yan <[email protected]>",
167170
"Francisco Baio Dias <[email protected]>",
@@ -171,12 +174,10 @@
171174
"Greenkeeper <[email protected]>",
172175
173176
"Harsh Vakharia <[email protected]>",
174-
"Johannes Wikner <[email protected]>",
175177
"Jon Schlinkert <[email protected]>",
176178
"João Antunes <[email protected]>",
177179
"Kevin Wang <[email protected]>",
178180
"Lars Gierth <[email protected]>",
179-
"Maciej Krüger <[email protected]>",
180181
"Marius Darila <[email protected]>",
181182
"Michelle Lee <[email protected]>",
182183
"Mikeal Rogers <[email protected]>",
@@ -185,17 +186,14 @@
185186
"Oskar Nyberg <[email protected]>",
186187
"Pau Ramon Revilla <[email protected]>",
187188
"Pedro Teixeira <[email protected]>",
188-
"RasmusErik Voel Jensen <[email protected]>",
189189
"Richard Littauer <[email protected]>",
190190
"Rod Keys <[email protected]>",
191191
"Sid Harder <[email protected]>",
192192
"SidHarder <[email protected]>",
193193
"Stephen Whitmore <[email protected]>",
194194
"Stephen Whitmore <[email protected]>",
195195
"Terence Pae <[email protected]>",
196-
"Uroš Jurglič <[email protected]>",
197196
"Xiao Liang <[email protected]>",
198-
"bitspill <[email protected]>",
199197
200198
"jbenet <[email protected]>",
201199
"kumavis <[email protected]>",
@@ -204,4 +202,4 @@
204202
205203
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>"
206204
]
207-
}
205+
}

src/http-api/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ function HttpApi (repo, config, cliArgs) {
2323
this.log = debug('jsipfs:http-api')
2424
this.log.error = debug('jsipfs:http-api:error')
2525

26+
if (process.env.IPFS_MONITORING) {
27+
// Setup debug metrics collection
28+
const prometheusClient = require('prom-client')
29+
const prometheusGcStats = require('prometheus-gc-stats')
30+
const collectDefaultMetrics = prometheusClient.collectDefaultMetrics
31+
collectDefaultMetrics({ timeout: 5000 })
32+
prometheusGcStats(prometheusClient.register)()
33+
}
34+
2635
this.start = (init, callback) => {
2736
if (typeof init === 'function') {
2837
callback = init
@@ -41,6 +50,8 @@ function HttpApi (repo, config, cliArgs) {
4150
try { wrtc = require('wrtc') } catch (err) {}
4251

4352
if (wrtc || electronWebRTC) {
53+
const using = wrtc ? 'wrtc' : 'electron-webrtc'
54+
console.log(`Using ${using} for webrtc support`)
4455
const wstar = new WStar({ wrtc: (wrtc || electronWebRTC) })
4556
libp2p.modules.transport = [wstar]
4657
libp2p.modules.discovery = [wstar.discovery]

src/http-api/routes/debug.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const register = require('prom-client').register
4+
const client = require('prom-client')
5+
6+
// Endpoint for handling debug metrics
7+
module.exports = (server) => {
8+
const api = server.select('API')
9+
// Clear the register to make sure we're not registering multiple ones
10+
register.clear()
11+
const gauge = new client.Gauge({ name: 'number_of_peers', help: 'the_number_of_currently_connected_peers' })
12+
13+
api.route({
14+
method: 'GET',
15+
path: '/debug/metrics/prometheus',
16+
handler: (request, reply) => {
17+
if (!process.env.IPFS_MONITORING) {
18+
return reply('Monitoring is disabled. Enable it by setting environment variable IPFS_MONITORING')
19+
.code(501) // 501 = Not Implemented
20+
}
21+
server.app.ipfs.swarm.peers((err, res) => {
22+
if (err) {
23+
return reply(err).code(500)
24+
}
25+
const count = res.length
26+
gauge.set(count)
27+
reply(register.metrics()).header('Content-Type', register.contentType)
28+
})
29+
}
30+
})
31+
}

src/http-api/routes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ module.exports = (server) => {
1212
require('./bitswap')(server)
1313
require('./files')(server)
1414
require('./pubsub')(server)
15+
require('./debug')(server)
1516
}

0 commit comments

Comments
 (0)