Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Converted metrics to typescript #20

Open
wants to merge 2 commits into
base: objection-typescript
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
server:
build: .
image: aragon/protocol-backend
command: npm run start:server:dev
command: yarn start:server:dev
depends_on:
- postgres
- loki
Expand All @@ -23,6 +23,7 @@ services:
volumes:
- ./packages/server/src:/app/packages/server/src
- ./packages/server/test:/app/packages/server/test
- ./packages/shared/build:/app/packages/shared/build
env_file:
- .env
logging: &loki
Expand All @@ -35,22 +36,22 @@ services:
services:
build: .
image: aragon/protocol-backend
command: npm run start:services:dev
command: yarn start:services:dev
depends_on:
- server
- loki
volumes:
- ./packages/services/bin:/app/packages/services/bin
- ./packages/services/src:/app/packages/services/src
- ./packages/services/test:/app/packages/services/test
- ./packages/shared/build:/app/packages/shared/build
env_file:
- .env
logging: *loki

backoffice:
build: .
image: aragon/protocol-backend
command: npm run start:app
command: yarn start:app
depends_on:
- server
- loki
Expand All @@ -75,7 +76,6 @@ services:
# speed up development by mounting some local directories with hot reload
- ./packages/server/src:/app/packages/server/src
- ./packages/server/test:/app/packages/server/test
- ./packages/services/bin:/app/packages/services/bin
- ./packages/services/src:/app/packages/services/src
- ./packages/services/test:/app/packages/services/test
- ./packages/shared/src:/app/packages/shared/src
Expand Down
4 changes: 0 additions & 4 deletions packages/app/.gitignore

This file was deleted.

14 changes: 0 additions & 14 deletions packages/server/.babelrc

This file was deleted.

15 changes: 15 additions & 0 deletions packages/server/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"root": true,
"plugins": ["@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
// allow using any and non null assertion for Objection models
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off"
}
}
1 change: 0 additions & 1 deletion packages/server/.gitignore

This file was deleted.

18 changes: 18 additions & 0 deletions packages/server/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"presets": [
"@babel/preset-typescript",
[
"@babel/preset-env",
{
"targets": {
"node": "current"
},
"useBuiltIns": "usage",
"corejs": 3
}
]
],
"plugins": [
"@babel/plugin-proposal-export-namespace-from"
]
}
30 changes: 17 additions & 13 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
"private": true,
"author": "Aragon One",
"license": "(GPL-3.0-or-later OR AGPL-3.0-or-later)",
"engines": {
"node": ">=9.0.0"
},
"scripts": {
"build": "babel ./src --out-dir ./build --source-maps --copy-files",
"build:shared": "yarn workspace @aragon/protocol-backend-shared build",
"start": "yarn build && yarn db:setup && node ./build",
"start:dev": "yarn db:setup && nodemon --ignore ./build --exec babel-node ./src/index.js",
"knex": "npx babel-node ./node_modules/.bin/knex",
"test": "npx mocha test --recursive --exit --require @babel/register",
"lint": "eslint src/**/*.ts --ext .ts",
"test": "npx mocha test --recursive --exit --require ./test/helpers/babel-ts",
"build": "yarn lint && yarn check-types && yarn build:js",
"build:js": "babel ./src --out-dir ./build --extensions .ts,.js",
"build:shared": "lerna run build --scope '*/*-shared' --stream",
"build:clean": "yarn clean && yarn build",
"clean": "rm -rf ./build",
"check-types": "tsc",
"start": "yarn db:setup && node ./build/index.js",
"start:dev": "yarn db:setup && nodemon --exec 'babel-node --extensions .ts,.js' ./src/index.js",
"db:setup": "lerna run db:setup --scope '*/*-shared' --stream"
},
"dependencies": {
"@aragon/protocol-backend-shared": "^0.2.21",
"@promster/express": "^4.0.0",
"@promster/server": "^4.0.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
Expand All @@ -33,7 +32,6 @@
"http-status-codes": "^1.4.0",
"morgan": "^1.9.1",
"postmark": "^2.5.3",
"prom-client": "^11.5.3",
"regenerator-runtime": "^0.13.3",
"validator": "^13.0.0",
"web3-utils": "^1.2.4"
Expand All @@ -43,9 +41,15 @@
"@babel/core": "^7.7.7",
"@babel/node": "^7.7.7",
"@babel/preset-env": "^7.7.7",
"@babel/preset-typescript": "^7.12.1",
"@babel/register": "^7.12.1",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"eslint": "^7.13.0",
"mocha": "^7.1.1",
"nodemon": "^2.0.2"
"nodemon": "^2.0.2",
"typescript": "^4.0.5"
}
}
4 changes: 2 additions & 2 deletions packages/server/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import helmet from 'helmet'
import morgan from 'morgan'
import express from 'express'
import bodyParser from 'body-parser'
import { createMiddleware } from '@promster/express'
import metrics from './helpers/metrics-reporter'

import routes from './routes'
import errorHandler from './errors/error-handler'
Expand All @@ -20,7 +20,7 @@ dotenv.config()
// Set up express layers
const app = express()
app.set('trust proxy', 1) // required for secure sessions
app.use(createMiddleware({ app }))
app.use(metrics.createExpressMiddleware())
app.use(helmet())
app.use(morgan('dev'))
app.use(corsMiddleware)
Expand Down
7 changes: 3 additions & 4 deletions packages/server/src/errors/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import HttpStatus from 'http-status-codes'
import { Errors as PostmarkErrors } from 'postmark'

import HttpError from './http-error'
import MetricsReporter from '../helpers/metrics-reporter'
import metrics from '../helpers/metrics-reporter'

export default app => (err, req, res, next) => {
if (res.headersSent) {
return next(err)
}

const reporter = MetricsReporter(app)
let code, body

if (err instanceof HttpError) {
Expand All @@ -25,7 +24,7 @@ export default app => (err, req, res, next) => {
code = HttpStatus.INTERNAL_SERVER_ERROR
body = { errors: [{ email: 'Could not send email.' }] }
console.error(err.stack)
reporter.emailError()
metrics.emailError()
}
else if (err.message.includes('CORS')) {
code = HttpStatus.BAD_REQUEST
Expand All @@ -37,7 +36,7 @@ export default app => (err, req, res, next) => {
body = 'Something went wrong :('

if (err instanceof DBError) {
reporter.dbError()
metrics.dbError()
}
}

Expand Down
42 changes: 0 additions & 42 deletions packages/server/src/helpers/metrics-reporter.js

This file was deleted.

26 changes: 26 additions & 0 deletions packages/server/src/helpers/metrics-reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Reporter, Metrics } from '@aragon/protocol-backend-shared/build/helpers/metrics-reporter'

const COUNTER_METRICS: Metrics = {
db: [
{ name: 'queries', help: 'DB queries per table' },
{ name: 'errors', help: 'DB errors' },
],
email: [
{ name: 'errors', help: 'Total email errors' },
]
}

class MetricsReporter extends Reporter {

dbQuery(): void {
this.counters.db.queries.inc()
}
dbError(): void {
this.counters.db.errors.inc()
}
emailError(): void {
this.counters.email.errors.inc()
}
}

export default new MetricsReporter(COUNTER_METRICS)
11 changes: 4 additions & 7 deletions packages/server/src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import app from './app'
import { createServer } from '@promster/server'
import { signalIsUp } from '@promster/express'
import metrics from './helpers/metrics-reporter'

const serverPort = process.env.SERVER_PORT || 8000
app.listen(serverPort, error => {
if (error) return console.error(error)
signalIsUp()
metrics.setExpressMiddlewareUp()
console.log(`Server listening on port ${serverPort}`)
})

// Start Prometheus metrics
const metricsPort = process.env.SERVER_METRICS_PORT || 9091
createServer({ port: metricsPort }).then(() =>
console.log(`Metrics server started on port ${metricsPort}`)
)
const metricsPort = Number(process.env.SERVER_METRICS_PORT) || 9091
metrics.createServer(metricsPort).then()
4 changes: 4 additions & 0 deletions packages/server/test/helpers/babel-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This is added to mocha --require ./test/helpers/babel-ts to allow importing .ts files in tests
require('@babel/register')({
extensions: ['.js','.ts'],
})
16 changes: 16 additions & 0 deletions packages/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": [
"src/**/*"
],
"compilerOptions": {
"strict": true,
"noEmit": true,
"moduleResolution": "Node",
// allow advanced js features, proper targeting is done using Babel
"target": "ESNext",
// allow non-ES modules with 'export =' syntax as default imports
"esModuleInterop": true,
// allow json import
"resolveJsonModule": true
}
}
14 changes: 0 additions & 14 deletions packages/services/.babelrc

This file was deleted.

15 changes: 15 additions & 0 deletions packages/services/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"root": true,
"plugins": ["@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
// allow using any and non null assertion for Objection models
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off"
}
}
13 changes: 2 additions & 11 deletions packages/services/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

This repo provides a set of background services in charge of those parts of Aragon Protocol that can be done automatically to ensure a good user experience for the users of the protocol.

### Setup

To work locally, simply go to the root directory, and make sure you have set up a propoer `.env` file following the `.env.sample` file.
Once you have done that, spin up a docker container with:
```bash
docker-compose build
docker-compose up -d
```

### Workers

It provides the following list of worker services:
Expand All @@ -22,7 +13,7 @@ It provides the following list of worker services:
- [`Settlements`](./src/workers/settlements.js): It will try to execute and settle penalties, rewards, and appeals for all the rounds of a dispute if possible. Configured initially to run one time per five minutes indefinitely.
- [`Contract Monitor`](./src/workers/contract-monitor.js): It periodically queries protocol address for any failing transactions in the past 24 hours and renders Prometheus gauge metrics that can be used for alerting.

All the background services are configured through the `config.js` file using the following variables:
All the background services are configured through the [`src/bin/config.js`](src/bin/config.js) file using the following variables:
- `name`: Name of the worker used for logging. It will assume `unknown` if undefined.
- `path`: Name of the root file that will be executed every time a new job for that worker is executed. This parameter is mandatory, and it must be a file exporting an async function accepting the following list of parameters: `worker`, `job`, `logger`.
- `processes`: Number of workers run in parallel. It will assume `1` if undefined.
Expand All @@ -33,4 +24,4 @@ All the background services are configured through the `config.js` file using th

### Keys

This repo needs the private key to be defined as a envrionment variable `PRIVATE_KEY`.
This repo needs the private key to be defined as a environment variable `PRIVATE_KEY`.
Loading