Skip to content

Commit

Permalink
Merge pull request #44 from digicatapult/feature/in-421
Browse files Browse the repository at this point in the history
IN-421: implementing metrics in servicewatcher.
  • Loading branch information
n3op2 authored Nov 29, 2022
2 parents cbdf8d6 + 28602d3 commit 10f3cbb
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const vars = envalid.cleanEnv(
LOG_LEVEL: envalid.str({ default: 'info', devDefault: 'debug' }),
PORT: envalid.port({ default: 80, devDefault: 3000 }),
NODE_HOST: envalid.host({ devDefault: 'localhost' }),
IPFS_API_HOST: envalid.host({ devDefault: 'localhost' }),
IPFS_API_PORT: envalid.port({ default: 5001 }),
NODE_PORT: envalid.port({ default: 9944 }),
IPFS_PATH: envalid.str({ default: '/ipfs', devDefault: path.resolve(__dirname, '..', `data`) }),
IPFS_EXECUTABLE: envalid.str({
Expand Down
31 changes: 31 additions & 0 deletions app/utils/ServiceWatcher.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as client from 'prom-client'
import axios from 'axios'

import { TimeoutError } from './Errors.js'
import env from '../env.js'

Expand All @@ -9,8 +12,20 @@ class ServiceWatcher {
constructor(apis) {
this.report = {}
this.#pollPeriod = env.HEALTHCHECK_POLL_PERIOD_MS
this.ipfsApiUrl = `http://${env.IPFS_API_HOST}:${env.IPFS_API_PORT}/api/v0/`
this.#timeout = env.HEALTHCHECK_TIMEOUT_MS
this.services = this.#init(apis)
this.metrics = {
peerCount: () => {
if (!this.metrics.peerCount) {
return new client.Gauge({
name: 'dscp_ipfs_swarm_peer_count',
help: 'a number of discovered and connected peers',
labelNames: ['type'],
})
}
},
}
}

delay(ms, service = false) {
Expand Down Expand Up @@ -44,6 +59,21 @@ class ServiceWatcher {
.filter(Boolean)
}

async #updateMetrics() {
const { data: connectedPeers } = await axios({
url: `${this.ipfsApiUrl}swarm/peers`,
method: 'POST',
})
const { data: discoveredPeers } = await axios({
url: `${this.ipfsApiUrl}swarm/addrs`,
method: 'POST',
})

// update instance's metrics object
this.metrics.peerCount.set({ type: 'discovered' }, Object.keys(discoveredPeers.Addrs).length)
this.metrics.peerCount.set({ type: 'connected' }, connectedPeers.Peers?.length || 0)
}

// starts the generator resolving after the first update
// use ServiceWatcher.gen.return() to stop
async start() {
Expand All @@ -54,6 +84,7 @@ class ServiceWatcher {
try {
const services = await getAll
services.forEach(({ name, ...rest }) => this.update(name, rest))
await this.#updateMetrics().catch((err) => (this.metrics.error = err))
} catch (error) {
// if no service assume that this is server error e.g. TypeError, Parse...
const name = error.service || 'server'
Expand Down
4 changes: 2 additions & 2 deletions helm/dscp-ipfs/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: v2
name: dscp-ipfs
appVersion: '2.8.2'
appVersion: '2.9.0'
description: A Helm chart for dscp-ipfs
version: '2.8.2'
version: '2.9.0'
type: application
dependencies:
- name: dscp-node
Expand Down
5 changes: 5 additions & 0 deletions helm/dscp-ipfs/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ spec:
configMapKeyRef:
name: {{ include "dscp-ipfs.fullname" . }}-config
key: healthCheckPort
- name: IPFS_API_PORT
valueFrom:
configMapKeyRef:
name: {{ include "dscp-ipfs.fullname" . }}-config
key: ipfsApiPort
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
Expand Down
2 changes: 1 addition & 1 deletion helm/dscp-ipfs/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ statefulSet:
image:
repository: digicatapult/dscp-ipfs
pullPolicy: IfNotPresent
tag: 'v2.8.2'
tag: 'v2.9.0'

storage:
storageClass: ""
Expand Down
86 changes: 83 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@digicatapult/dscp-ipfs",
"version": "2.8.2",
"version": "2.9.0",
"description": "Service for DSCP",
"main": "app/index.js",
"type": "module",
"scripts": {
"test": "NODE_ENV=test mocha --config ./test/mocharc.cjs ./test",
"test:integration": "NODE_ENV=test mocha --config ./test/mocharc.cjs ./test/integration",
"test:unit": "NODE_ENV=test mocha --config ./test/mocharc-unit.cjs ./test/unit",
"lint": "eslint .",
"lint": "eslint . --fix",
"depcheck": "depcheck",
"start": "node app/index.js",
"dev": "NODE_ENV=dev nodemon app/index.js | pino-colada",
Expand All @@ -33,6 +33,7 @@
"homepage": "https://github.com/digicatapult/dscp-ipfs#readme",
"dependencies": {
"@polkadot/api": "^9.9.4",
"axios": "^1.2.0",
"dotenv": "^16.0.3",
"envalid": "^7.3.1",
"express": "^4.18.2",
Expand All @@ -52,7 +53,7 @@
"formdata-node": "^5.0.0",
"go-ipfs": "^0.16.0",
"mocha": "^10.1.0",
"node-fetch": "^3.2.10",
"node-fetch": "^3.3.0",
"nodemon": "^2.0.20",
"nyc": "^15.1.0",
"pino-colada": "^2.2.2",
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const mochaGlobalSetup = async function () {
this.stopServer = stopServer

await startServer(this)
await waitForIpfsApi(`5001`)
await waitForIpfsApi(env.IPFS_API_PORT)
}

export const mochaGlobalTeardown = async function () {
Expand Down
5 changes: 3 additions & 2 deletions test/integration/ipfs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { FormData, Blob } from 'formdata-node'
import { expect } from 'chai'
import delay from 'delay'

import env from '../../app/env.js'
import { getSwarmKey, setSwarmKey } from './helper/api.js'
import { setupIPFS, waitForIpfsApi } from './helper/ipfs.js'

const uploadA = async (fileName, contents) => {
const form = new FormData()
form.append('file', new Blob([contents]), fileName)
const body = await fetch(`http://localhost:5001/api/v0/add?cid-version=0`, {
const body = await fetch(`http://localhost:${env.IPFS_API_PORT}/api/v0/add?cid-version=0`, {
method: 'POST',
body: form,
})
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('ipfs', function () {
context.hash = await uploadA('test-file-4.txt', 'Test 4')
await setSwarmKey(context.swarmKey)
await delay(500)
await waitForIpfsApi(`5001`)
await waitForIpfsApi(env.IPFS_API_PORT)
})

it('should be retrievable', async function () {
Expand Down
2 changes: 2 additions & 0 deletions test/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ IPFS_LOG_LEVEL=fatal
NODE_HOST=localhost
HEALTHCHECK_POLL_PERIOD_MS=1000
HEALTHCHECK_TIMEOUT_MS=1000
IPFS_API_HOST=localhost
IPFS_API_PORT=5001

0 comments on commit 10f3cbb

Please sign in to comment.