Skip to content
This repository has been archived by the owner on Sep 22, 2021. It is now read-only.

Added health monitor #697

Merged
merged 31 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7c8fce0
Added health monitor
niklabh Apr 22, 2020
77acee6
Added health check for auth server
niklabh Apr 26, 2020
b8ff379
Added other statuses
niklabh Apr 26, 2020
10ace48
Merge branch 'master' into niklabh-feature-health-monitor
niklabh Apr 27, 2020
e5729a3
Added prisma version and rpc call
niklabh Apr 27, 2020
a52a2c2
Merge branch 'master' into niklabh-feature-health-monitor
niklabh Apr 30, 2020
caf9832
Added post with author query
niklabh Apr 30, 2020
1e066f9
Added HEALTH_PORT
niklabh Apr 30, 2020
4067f37
Update health-monitor/README.md
niklabh Apr 30, 2020
4836acc
license Apache-2.0
niklabh Apr 30, 2020
1dd18e4
lint
niklabh Apr 30, 2020
82539f3
accumulator
niklabh Apr 30, 2020
d543664
Getting latest block rather than finalised
niklabh Apr 30, 2020
fc2dfb9
added referendum delays
niklabh Apr 30, 2020
d490178
Merge branch 'master' into niklabh-feature-health-monitor
Tbaut May 4, 2020
d080fe9
Update chain-db-watcher/src/index.ts
niklabh May 4, 2020
3872df3
Update health-monitor/src/index.ts
niklabh May 4, 2020
097d26f
Update health-monitor/src/index.ts
niklabh May 4, 2020
eb0040c
Merge branch 'master' into niklabh-feature-health-monitor
Tbaut May 6, 2020
815ba22
Merge branch 'master' into niklabh-feature-health-monitor
niklabh May 11, 2020
94bd9db
using tsc
niklabh May 11, 2020
5ac72f3
fix
niklabh May 11, 2020
2b98e8c
moved to dev depen
niklabh May 11, 2020
13861a2
change as per review
niklabh May 11, 2020
5d3ce52
Merge branch 'master' into niklabh-feature-health-monitor
Tbaut May 11, 2020
bb2cc09
Fix health monitor
niklabh May 11, 2020
3f9ae78
Merge branch 'master' into niklabh-feature-health-monitor
Tbaut May 12, 2020
a43fba6
update @polkadt/api
Tbaut May 12, 2020
7d37ef7
post id can be null and onchain_referendum is an array
Tbaut May 12, 2020
9322c7b
fix threshold add logs and info
Tbaut May 12, 2020
306ab7c
Merge branch 'master' into niklabh-feature-health-monitor
niklabh May 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chain-db-watcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ COUNCIL_TOPIC_ID=2
TREASURY_TOPIC_ID=4
HASURA_GRAPHQL_ADMIN_SECRET=<your admin secret>
START_FROM=0
PORT=8019
```
## Run
- `yarn`
Expand Down
14 changes: 14 additions & 0 deletions chain-db-watcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { execute } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import chalk from 'chalk';
import dotenv from 'dotenv';
import http from 'http';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import ws from 'ws';

Expand Down Expand Up @@ -208,6 +209,19 @@ async function main (): Promise<void> {
process.exit(1);
}
});

const hostname = '127.0.0.1';
const port = Number(process.env.PORT) || 8019;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('ok');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
}

main().catch(error => console.error(chalk.red(error)));
3 changes: 3 additions & 0 deletions health-monitor/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
.env
11 changes: 11 additions & 0 deletions health-monitor/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# http://editorconfig.org/

root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = tab
indent_size = 4
1 change: 1 addition & 0 deletions health-monitor/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
49 changes: 49 additions & 0 deletions health-monitor/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"env": {
"es6": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:import/typescript",
"semistandard"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["./tsconfig.json"]
},
"plugins": [
"@typescript-eslint",
"header",
"security",
"promise",
"simple-import-sort"
],
"rules": {
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/no-unused-vars": "error",
"arrow-spacing":["warn",{ "before": true, "after": true }],
"camelcase": "off",
"comma-dangle": ["error", "never"],
"header/header": [2, "../header.js"],
"indent": ["error", "tab"],
"no-extra-parens": ["error", "all", { "returnAssign": false }],
"no-multiple-empty-lines": ["error", {"max": 1}],
"no-tabs": ["error", { "allowIndentationTabs": true }],
"no-trailing-spaces": ["warn"],
"no-unused-vars": "off",
"object-curly-spacing": ["error", "always"],
"quotes": ["error", "single", { "avoidEscape": true }],
"semi": [2, "always"],
"simple-import-sort/sort": "error",
"sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}],
"switch-colon-spacing": ["error", {"after": true, "before": false}]
}
}
7 changes: 7 additions & 0 deletions health-monitor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.env
data
build
*.sh
*.log
docker-compose.yaml
18 changes: 18 additions & 0 deletions health-monitor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM node:12

RUN mkdir -p /opt/app

RUN chown node:node /opt/app

WORKDIR /opt/app

COPY --chown=node:node package.json .
COPY --chown=node:node yarn.lock .

USER node

RUN yarn

COPY --chown=node:node . .

CMD yarn start
19 changes: 19 additions & 0 deletions health-monitor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## health-monitor

Health monitor service for polkassembly. provides single API which check health of all polkassembly components.

## Installation

Configure environment variables with a .env file with following values.

```bash
PORT=8034
AUTH_SERVER="http://localhost:8010"
HASURA_SERVER="http://localhost:8080"
CHAIN_DB_WATCHER_SERVER="http://localhost:8019"
CHAIN_DB_SERVER="http://0.0.0.0:4466"
REACT_SERVER="http://localhost:3000"
ARCHIVE_NODE_ENDPOINT="wss://kusama-rpc.polkadot.io"
```


38 changes: 38 additions & 0 deletions health-monitor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "health-monitor",
"version": "0.0.1",
"description": "Healthcheck Monitoring for polkassembly",
"main": "index.js",
"scripts": {
"start": "ts-node src/index.ts",
"lint": "npx eslint ./src/ --ext .js,.ts",
"lint:fix": "npx eslint ./src/ --ext .js,.ts --fix"
},
"dependencies": {
"@polkadot/api": "^1.11.2",
"@typescript-eslint/parser": "^2.29.0",
"dotenv": "^8.2.0",
"express": "^4.16.4",
"node-fetch": "^2.6.0",
"ts-node": "^8.5.4",
"typescript": "^3.8.3"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.2",
"@types/node": "^12.12.11",
"@types/node-fetch": "^2.5.5",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"eslint": "^6.8.0",
"eslint-config-semistandard": "^15.0.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-header": "^3.0.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-simple-import-sort": "^5.0.2",
"eslint-plugin-standard": "^4.0.1"
},
"license": "MIT"
}
96 changes: 96 additions & 0 deletions health-monitor/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2019-2020 @paritytech/polkassembly authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import dotenv from 'dotenv';
import express from 'express';
import fetch from 'node-fetch';
import { ApiPromise, WsProvider } from '@polkadot/api';

dotenv.config();

const app = express();

app.set('host', '0.0.0.0');
app.set('port', process.env.PORT || 8034);

export interface HealthCheckResult {
authServerStatus: number;
hasuraServerStatus: number;
chainDbWatcherServerStatus: number;
reactServerStatus: number;
latestBlockNumber: number;
prismaVersion: string;
}

async function getLatestBlockNumber (): Promise<number> {
const provider = new WsProvider(process.env.ARCHIVE_NODE_ENDPOINT);
const api = await ApiPromise.create({ provider });

const latestBlockNumber = (
await api.derive.chain.bestNumberFinalized()
).toNumber();

return latestBlockNumber;
}

async function getPrismaVersion () {
return fetch(`${process.env.CHAIN_DB_SERVER}/management`, {
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
operationName: null,
variables: {},
query:`{
serverInfo {
version
}
}`
}),
method: 'POST'
}).then(res => res.json());
}

async function healthcheck (): Promise<HealthCheckResult> {
const [
authServer,
hasuraServer,
chainDbWatcherServer,
reactServer,
latestBlockNumber,
prismaVersion
] = await Promise.all([
fetch(`${process.env.AUTH_SERVER}/healthcheck`),
fetch(`${process.env.HASURA_SERVER}/healthz`),
fetch(`${process.env.HASURA_SERVER}/healthz`),
fetch(`${process.env.REACT_SERVER}/healthcheck`),
getLatestBlockNumber(),
getPrismaVersion()
]);

return {
authServerStatus: authServer.status,
chainDbWatcherServerStatus: chainDbWatcherServer.status,
hasuraServerStatus: hasuraServer.status,
reactServerStatus: reactServer.status,
latestBlockNumber,
prismaVersion: prismaVersion.data.serverInfo.version
};
}

app.use('/healthcheck', (req, res, next) => {
healthcheck()
.then((result) => res.json(result))
.catch(next);
});

/**
* Start Express server.
*/
app.listen(app.get('port'), () => {
console.log(`App is running at http://localhost:${app.get('port')}`);
console.log(' Press CTRL-C to stop\n');
});

module.exports = app;
83 changes: 83 additions & 0 deletions health-monitor/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"es2017",
], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./build", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": [
"./node_modules/@types",
"./src/types"
], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
// "skipLibCheck": true, /* Skip checking .d.ts files for errors */
"pretty": true
},
"include": [
"./src/**/*", "knexfile.ts", "./test/**/*"
],
"exclude": [
"node_modules"
]
}




Loading