From 8beadcf7a8e5d507cb53b7dfdba0527131bcc209 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Tue, 11 Jul 2017 22:58:08 +0200 Subject: [PATCH 01/11] Add /debug/metrics/prometheus as a endpoint For basic metrics about running process --- package.json | 10 +--------- src/http-api/index.js | 8 ++++++++ src/http-api/routes/debug.js | 24 ++++++++++++++++++++++++ src/http-api/routes/index.js | 1 + 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 src/http-api/routes/debug.js diff --git a/package.json b/package.json index be5c0cfef3..d21c32ada6 100644 --- a/package.json +++ b/package.json @@ -161,7 +161,6 @@ "Daniel J. O'Quinn ", "Daniela Borges Matos de Carvalho ", "David Dias ", - "Dzmitry Das ", "Enrico Marino ", "Felix Yan ", "Francisco Baio Dias ", @@ -171,12 +170,8 @@ "Greenkeeper ", "Haad ", "Harsh Vakharia ", - "Johannes Wikner ", - "Jon Schlinkert ", "João Antunes ", - "Kevin Wang ", "Lars Gierth ", - "Maciej Krüger ", "Marius Darila ", "Michelle Lee ", "Mikeal Rogers ", @@ -185,7 +180,6 @@ "Oskar Nyberg ", "Pau Ramon Revilla ", "Pedro Teixeira ", - "RasmusErik Voel Jensen ", "Richard Littauer ", "Rod Keys ", "Sid Harder ", @@ -193,9 +187,7 @@ "Stephen Whitmore ", "Stephen Whitmore ", "Terence Pae ", - "Uroš Jurglič ", "Xiao Liang ", - "bitspill ", "haad ", "jbenet ", "kumavis ", @@ -204,4 +196,4 @@ "tcme ", "ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ " ] -} \ No newline at end of file +} diff --git a/src/http-api/index.js b/src/http-api/index.js index a0d849e9d2..08281b4b6b 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -7,6 +7,9 @@ const multiaddr = require('multiaddr') const setHeader = require('hapi-set-header') const once = require('once') +const prometheusClient = require('prom-client') +const prometheusGcStats = require('prometheus-gc-stats') + const IPFS = require('../core') const WStar = require('libp2p-webrtc-star') const errorHandler = require('./error-handler') @@ -23,6 +26,11 @@ function HttpApi (repo, config, cliArgs) { this.log = debug('jsipfs:http-api') this.log.error = debug('jsipfs:http-api:error') + // Setup debug metrics collection + const collectDefaultMetrics = prometheusClient.collectDefaultMetrics + collectDefaultMetrics({ timeout: 5000 }) + prometheusGcStats(prometheusClient.register)() + this.start = (init, callback) => { if (typeof init === 'function') { callback = init diff --git a/src/http-api/routes/debug.js b/src/http-api/routes/debug.js new file mode 100644 index 0000000000..1a1cd61819 --- /dev/null +++ b/src/http-api/routes/debug.js @@ -0,0 +1,24 @@ +const register = require('prom-client').register +const client = require('prom-client') + +// Endpoint for handling debug metrics +module.exports = (server) => { + const api = server.select('API') + const gauge = new client.Gauge({ name: 'number_of_peers', help: 'the_number_of_currently_connected_peers' }) + + setInterval(() => { + server.app.ipfs.swarm.peers((err, res) => { + if (err) throw err + const count = res.length + gauge.set(count) + }) + }, 5000) + + api.route({ + method: 'GET', + path: '/debug/metrics/prometheus', + handler: (request, reply) => { + reply(register.metrics()).header('Content-Type', register.contentType) + } + }) +} diff --git a/src/http-api/routes/index.js b/src/http-api/routes/index.js index 7b0afa885d..c317db6de7 100644 --- a/src/http-api/routes/index.js +++ b/src/http-api/routes/index.js @@ -12,4 +12,5 @@ module.exports = (server) => { require('./bitswap')(server) require('./files')(server) require('./pubsub')(server) + require('./debug')(server) } From 9277052b06e931f746e991940061bab24542ab3f Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Tue, 11 Jul 2017 23:03:44 +0200 Subject: [PATCH 02/11] Add Dockerfile --- Dockerfile | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..7b9ae7e731 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM node:6 + +RUN apt-get update +RUN apt-get install --yes python2.7 git-all pkg-config libncurses5-dev libssl-dev libnss3-dev libexpat-dev libc6 + +WORKDIR /usr/src/app + +COPY package.json /usr/src/app/package.json + +RUN npm install + +COPY . /usr/src/app + +ENTRYPOINT ["node", "src/cli/bin.js"] From 42c6e7e11f3252da9f96d9043dae45a6ccba043e Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Wed, 26 Jul 2017 16:14:06 +0200 Subject: [PATCH 03/11] Fix Docker setup --- .dockerignore | 1 + Dockerfile | 6 +++++- init-and-daemon.sh | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100755 init-and-daemon.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..c2658d7d1b --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules/ diff --git a/Dockerfile b/Dockerfile index 7b9ae7e731..ca3bafb946 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,11 @@ WORKDIR /usr/src/app COPY package.json /usr/src/app/package.json RUN npm install +RUN npm install wrtc COPY . /usr/src/app -ENTRYPOINT ["node", "src/cli/bin.js"] +ENV IPFS_WRTC_LINUX_WINDOWS=1 +ENV IPFS_BOOTSTRAP=1 + +CMD ./init-and-daemon.sh diff --git a/init-and-daemon.sh b/init-and-daemon.sh new file mode 100755 index 0000000000..559c3869fd --- /dev/null +++ b/init-and-daemon.sh @@ -0,0 +1,4 @@ +#! /bin/sh -e + +node src/cli/bin.js init +node src/cli/bin.js daemon From d77d9643bc09c25a91de7a2ede136e592dcbf132 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Wed, 26 Jul 2017 16:16:48 +0200 Subject: [PATCH 04/11] Only enable monitoring if IPFS_MONITORING set --- package.json | 6 ++++++ src/http-api/index.js | 14 ++++++++------ src/http-api/routes/debug.js | 4 ++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index d21c32ada6..63e0dc705b 100644 --- a/package.json +++ b/package.json @@ -153,6 +153,10 @@ "update-notifier": "^2.2.0", "yargs": "8.0.2" }, + "optionalDependencies": { + "prom-client": "^10.0.2", + "prometheus-gc-stats": "^0.5.0" + }, "contributors": [ "Andrew de Andrade ", "CHEVALAY JOSSELIN ", @@ -170,7 +174,9 @@ "Greenkeeper ", "Haad ", "Harsh Vakharia ", + "Jon Schlinkert ", "João Antunes ", + "Kevin Wang ", "Lars Gierth ", "Marius Darila ", "Michelle Lee ", diff --git a/src/http-api/index.js b/src/http-api/index.js index 08281b4b6b..920e53669f 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -7,8 +7,6 @@ const multiaddr = require('multiaddr') const setHeader = require('hapi-set-header') const once = require('once') -const prometheusClient = require('prom-client') -const prometheusGcStats = require('prometheus-gc-stats') const IPFS = require('../core') const WStar = require('libp2p-webrtc-star') @@ -26,10 +24,14 @@ function HttpApi (repo, config, cliArgs) { this.log = debug('jsipfs:http-api') this.log.error = debug('jsipfs:http-api:error') - // Setup debug metrics collection - const collectDefaultMetrics = prometheusClient.collectDefaultMetrics - collectDefaultMetrics({ timeout: 5000 }) - prometheusGcStats(prometheusClient.register)() + if (process.env.IPFS_MONITORING) { + // Setup debug metrics collection + const prometheusClient = require('prom-client') + const prometheusGcStats = require('prometheus-gc-stats') + const collectDefaultMetrics = prometheusClient.collectDefaultMetrics + collectDefaultMetrics({ timeout: 5000 }) + prometheusGcStats(prometheusClient.register)() + } this.start = (init, callback) => { if (typeof init === 'function') { diff --git a/src/http-api/routes/debug.js b/src/http-api/routes/debug.js index 1a1cd61819..eb99fd8901 100644 --- a/src/http-api/routes/debug.js +++ b/src/http-api/routes/debug.js @@ -18,6 +18,10 @@ module.exports = (server) => { method: 'GET', path: '/debug/metrics/prometheus', handler: (request, reply) => { + if (!process.env.IPFS_MONITORING) { + return reply('Monitoring is disabled. Enable it by setting environment variable IPFS_MONITORING') + .code(501) // 501 = Not Implemented + } reply(register.metrics()).header('Content-Type', register.contentType) } }) From af95dc0b0a4972e3d7976bd10f6a08a0c1afa32a Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Wed, 26 Jul 2017 16:17:22 +0200 Subject: [PATCH 05/11] Make webrtc usage explicit --- src/http-api/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/http-api/index.js b/src/http-api/index.js index 920e53669f..1f3ca1bb07 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -51,6 +51,8 @@ function HttpApi (repo, config, cliArgs) { try { wrtc = require('wrtc') } catch (err) {} if (wrtc || electronWebRTC) { + const using = wrtc ? 'wrtc' : 'electron-webrtc' + console.log(`Using ${using} for webrtc support`) const wstar = new WStar({ wrtc: (wrtc || electronWebRTC) }) libp2p.modules.transport = [wstar] libp2p.modules.discovery = [wstar.discovery] From 2e556dbb9f57e749d6b72739fbd724be255b07d5 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Wed, 26 Jul 2017 16:30:52 +0200 Subject: [PATCH 06/11] Enable monitoring when run via docker --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index ca3bafb946..5f08b2c40a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,5 +14,6 @@ COPY . /usr/src/app ENV IPFS_WRTC_LINUX_WINDOWS=1 ENV IPFS_BOOTSTRAP=1 +ENV IPFS_MONITORING=1 CMD ./init-and-daemon.sh From 10c1e1b79afb3a6159ef2fa5ebd87ad1ad2a7107 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Wed, 26 Jul 2017 16:31:09 +0200 Subject: [PATCH 07/11] Get peers when endpoint hit --- src/http-api/routes/debug.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/http-api/routes/debug.js b/src/http-api/routes/debug.js index eb99fd8901..5f468c8cbe 100644 --- a/src/http-api/routes/debug.js +++ b/src/http-api/routes/debug.js @@ -6,14 +6,6 @@ module.exports = (server) => { const api = server.select('API') const gauge = new client.Gauge({ name: 'number_of_peers', help: 'the_number_of_currently_connected_peers' }) - setInterval(() => { - server.app.ipfs.swarm.peers((err, res) => { - if (err) throw err - const count = res.length - gauge.set(count) - }) - }, 5000) - api.route({ method: 'GET', path: '/debug/metrics/prometheus', @@ -22,7 +14,12 @@ module.exports = (server) => { return reply('Monitoring is disabled. Enable it by setting environment variable IPFS_MONITORING') .code(501) // 501 = Not Implemented } - reply(register.metrics()).header('Content-Type', register.contentType) + server.app.ipfs.swarm.peers((err, res) => { + if (err) throw err + const count = res.length + gauge.set(count) + reply(register.metrics()).header('Content-Type', register.contentType) + }) } }) } From ec643b3a9e15fb31c3ce3cf7a3ced7533db885db Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Wed, 26 Jul 2017 16:35:50 +0200 Subject: [PATCH 08/11] Add docs for enabling monitoring --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 4246f24749..843d14916c 100644 --- a/README.md +++ b/README.md @@ -520,6 +520,20 @@ src # Main source code folder └── ... ``` +### Monitoring + +The HTTP API exposed with js-ipfs can also be used for exposing metrics about +the running js-ipfs node and other nodejs metrics. + +To enable it, you need to set the environment variable `IPFS_MONITORING` (any value) + +Once environment variable is set and the js-ipfs daemon is running, you can get +the metrics (in prometheus format) by making a GET request to the following endpoint: + +``` +http://localhost:5002/debug/metrics/prometheus +``` + ### IPFS Core Architecture ![](/img/core.png) From e6302eb61b62f6b0c8dd49d827b6a3a065908b0f Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Thu, 24 Aug 2017 16:53:19 +0200 Subject: [PATCH 09/11] Expose API and better handling of errors --- Dockerfile | 6 ++++++ init-and-daemon.sh | 4 +++- src/http-api/routes/debug.js | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5f08b2c40a..603a6182f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,5 +15,11 @@ COPY . /usr/src/app ENV IPFS_WRTC_LINUX_WINDOWS=1 ENV IPFS_BOOTSTRAP=1 ENV IPFS_MONITORING=1 +ENV IPFS_PATH=/root/.jsipfs + +EXPOSE 4002 +EXPOSE 4003 +EXPOSE 5002 +EXPOSE 9090 CMD ./init-and-daemon.sh diff --git a/init-and-daemon.sh b/init-and-daemon.sh index 559c3869fd..a01721e436 100755 --- a/init-and-daemon.sh +++ b/init-and-daemon.sh @@ -1,4 +1,6 @@ #! /bin/sh -e - node src/cli/bin.js init + +sed -i.bak 's/127.0.0.1/0.0.0.0/g' $IPFS_PATH/config + node src/cli/bin.js daemon diff --git a/src/http-api/routes/debug.js b/src/http-api/routes/debug.js index 5f468c8cbe..186999f094 100644 --- a/src/http-api/routes/debug.js +++ b/src/http-api/routes/debug.js @@ -1,5 +1,6 @@ const register = require('prom-client').register const client = require('prom-client') +const boom = require('boom') // Endpoint for handling debug metrics module.exports = (server) => { @@ -15,7 +16,9 @@ module.exports = (server) => { .code(501) // 501 = Not Implemented } server.app.ipfs.swarm.peers((err, res) => { - if (err) throw err + if (err) { + return reply(err).code(500) + } const count = res.length gauge.set(count) reply(register.metrics()).header('Content-Type', register.contentType) From 5f3e8aec3ae4c68048d3d8e52c8a78bc51240002 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Wed, 30 Aug 2017 20:07:24 +0200 Subject: [PATCH 10/11] Fix linting --- src/http-api/index.js | 1 - src/http-api/routes/debug.js | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/http-api/index.js b/src/http-api/index.js index 1f3ca1bb07..834ee49844 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -7,7 +7,6 @@ const multiaddr = require('multiaddr') const setHeader = require('hapi-set-header') const once = require('once') - const IPFS = require('../core') const WStar = require('libp2p-webrtc-star') const errorHandler = require('./error-handler') diff --git a/src/http-api/routes/debug.js b/src/http-api/routes/debug.js index 186999f094..b7e560af1f 100644 --- a/src/http-api/routes/debug.js +++ b/src/http-api/routes/debug.js @@ -1,6 +1,7 @@ +'use strict' + const register = require('prom-client').register const client = require('prom-client') -const boom = require('boom') // Endpoint for handling debug metrics module.exports = (server) => { From 5abb83d18325e805e055ef725d8bdba191749619 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Wed, 30 Aug 2017 20:46:39 +0200 Subject: [PATCH 11/11] Clear registers before registering number_of_peers --- src/http-api/routes/debug.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/http-api/routes/debug.js b/src/http-api/routes/debug.js index b7e560af1f..d5ae1e434d 100644 --- a/src/http-api/routes/debug.js +++ b/src/http-api/routes/debug.js @@ -6,6 +6,8 @@ const client = require('prom-client') // Endpoint for handling debug metrics module.exports = (server) => { const api = server.select('API') + // Clear the register to make sure we're not registering multiple ones + register.clear() const gauge = new client.Gauge({ name: 'number_of_peers', help: 'the_number_of_currently_connected_peers' }) api.route({