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

Commit

Permalink
feat: add instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
victorb authored and daviddias committed Aug 31, 2017
1 parent 54be08b commit 8f0254e
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 7 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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
RUN npm install wrtc

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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions init-and-daemon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +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
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <andrew@deandrade.com.br>",
"CHEVALAY JOSSELIN <josselin54.chevalay@gmail.com>",
Expand All @@ -161,7 +165,6 @@
"Daniel J. O'Quinn <danieljoquinn@gmail.com>",
"Daniela Borges Matos de Carvalho <alunassertiva@gmail.com>",
"David Dias <daviddias.p@gmail.com>",
"Dzmitry Das <dbachko@gmail.com>",
"Enrico Marino <enrico.marino@email.com>",
"Felix Yan <felixonmars@archlinux.org>",
"Francisco Baio Dias <xicombd@gmail.com>",
Expand All @@ -171,12 +174,10 @@
"Greenkeeper <support@greenkeeper.io>",
"Haad <haadcode@users.noreply.github.com>",
"Harsh Vakharia <harshjv@users.noreply.github.com>",
"Johannes Wikner <johannes.wikner@gmail.com>",
"Jon Schlinkert <dev@sellside.com>",
"João Antunes <j.goncalo.antunes@gmail.com>",
"Kevin Wang <kevin@fossa.io>",
"Lars Gierth <larsg@systemli.org>",
"Maciej Krüger <mkg20001@gmail.com>",
"Marius Darila <marius.darila@gmail.com>",
"Michelle Lee <michelle@protocol.ai>",
"Mikeal Rogers <mikeal.rogers@gmail.com>",
Expand All @@ -185,17 +186,14 @@
"Oskar Nyberg <oskar@oskarnyberg.com>",
"Pau Ramon Revilla <masylum@gmail.com>",
"Pedro Teixeira <i@pgte.me>",
"RasmusErik Voel Jensen <github@solsort.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"Rod Keys <rod@zokos.com>",
"Sid Harder <sideharder@gmail.com>",
"SidHarder <softwarenavigator@gmail.com>",
"Stephen Whitmore <stephen.whitmore@gmail.com>",
"Stephen Whitmore <noffle@users.noreply.github.com>",
"Terence Pae <terencepae@gmail.com>",
"Uroš Jurglič <jurglic@gmail.com>",
"Xiao Liang <yxliang01@users.noreply.github.com>",
"bitspill <bitspill+github@bitspill.net>",
"haad <haad@headbanggames.com>",
"jbenet <juan@benet.ai>",
"kumavis <kumavis@users.noreply.github.com>",
Expand All @@ -204,4 +202,4 @@
"tcme <hi@this-connect.me>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
]
}
}
11 changes: 11 additions & 0 deletions src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ function HttpApi (repo, config, cliArgs) {
this.log = debug('jsipfs:http-api')
this.log.error = debug('jsipfs:http-api:error')

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') {
callback = init
Expand All @@ -41,6 +50,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]
Expand Down
31 changes: 31 additions & 0 deletions src/http-api/routes/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const register = require('prom-client').register
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({
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
}
server.app.ipfs.swarm.peers((err, res) => {
if (err) {
return reply(err).code(500)
}
const count = res.length
gauge.set(count)
reply(register.metrics()).header('Content-Type', register.contentType)
})
}
})
}
1 change: 1 addition & 0 deletions src/http-api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ module.exports = (server) => {
require('./bitswap')(server)
require('./files')(server)
require('./pubsub')(server)
require('./debug')(server)
}

0 comments on commit 8f0254e

Please sign in to comment.