Skip to content

Commit

Permalink
Merge branch 'main' into chore/Veritable-naming
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-marky authored Nov 30, 2022
2 parents a708216 + 10f3cbb commit 4308257
Show file tree
Hide file tree
Showing 9 changed files with 614 additions and 476 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
14 changes: 5 additions & 9 deletions app/keyWatcher/api.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { buildApi } from '@digicatapult/dscp-node'
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api'

import env from '../env.js'

export const createNodeApi = async () => {
const { api, keyring } = buildApi({
options: {
apiHost: env.NODE_HOST,
apiPort: env.NODE_PORT,
},
})
const provider = new WsProvider(`ws://${env.NODE_HOST}:${env.NODE_PORT}`)
const api = new ApiPromise({ provider })

await api.isReady
await api.isReadyOrError.catch(() => {}) // prevent unhandled promise rejection errors

return {
_api: api,
_keyring: keyring,
_keyring: new Keyring({ type: 'sr25519' }),
isEventKeyUpdate: (event) => api.events.ipfsKey.UpdateKey.is(event),
getCurrentKey: async () => await api.query.ipfsKey.key(),
setupEventProcessor: (eventProcessor) => api.query.system.events(eventProcessor),
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
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
Loading

0 comments on commit 4308257

Please sign in to comment.