Skip to content
This repository has been archived by the owner on Dec 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request #15 from osrs-tracker/feature/prometheus-logging
Browse files Browse the repository at this point in the history
Added prometheus metrics and fixed + improved logging format
  • Loading branch information
FreekMencke authored May 27, 2019
2 parents 5881169 + 7fa13ab commit c9c2b9a
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 22 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dist
node_modules
scripts
.dockerignore
.editorconfig
.gitignore
.travis.yml
Expand Down
37 changes: 36 additions & 1 deletion package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "osrs-tracker-api",
"version": "0.2.0",
"version": "0.3.0",
"private": true,
"author": "Freek Mencke",
"homepage": "https://twitter.com/FreekMencke",
Expand All @@ -24,9 +24,11 @@
"compression": "1.7.4",
"cors": "2.8.5",
"express": "4.16.4",
"express-prom-bundle": "^5.1.5",
"helmet": "3.18.0",
"mysql": "2.17.1",
"node-fetch": "2.6.0"
"node-fetch": "2.6.0",
"prom-client": "^11.3.0"
},
"devDependencies": {
"@types/body-parser": "^1.17.0",
Expand Down
21 changes: 18 additions & 3 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Worker } from 'cluster';
import compression from 'compression';
import cors from 'cors';
import express, { Application } from 'express';
import * as prometheusMetricsMiddleware from 'express-prom-bundle';
import helmet from 'helmet';
import { config } from '../config/config';
import { Logger } from './common/logger';
Expand All @@ -18,14 +19,20 @@ import { XpRouter } from './routes/xp.router';
export class App {
private _app: Application;

constructor() {
static run(worker: Worker): App {
const app = new App();
app.start(worker);
return app;
}

private constructor() {
this._app = express();

this.setupMiddleware();
this.setupRouters();
}

start(worker: Worker): void {
private start(worker: Worker): void {
this._app.listen(config.port, () => {
Logger.log(`WORKER ${worker.id} CREATED ON PORT ${config.port}`);
this.setupRouters();
Expand All @@ -38,7 +45,15 @@ export class App {
this._app.use(cors());
this._app.use(bodyParser.urlencoded({ extended: true }));
this._app.use(bodyParser.json());
this._app.use(requestLogger(['/health']));
this._app.use(requestLogger(['/health', '/metrics']));
this._app.use(
prometheusMetricsMiddleware({
autoregister: false,
customLabels: { app: 'osrs-tracker-api' },
includeMethod: true,
includePath: true,
})
);
}

private setupRouters(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { exists, mkdir } from 'fs';
import { join } from 'path';
import { Logger } from './logger';
import { Logger } from '../logger';

export class FileSystemUtils {
static createIconsFolderIfMissing(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createPool, PoolConnection } from 'mysql';
import { config } from '../../config/config';
import { Logger } from './logger';
import { config } from '../../../config/config';
import { Logger } from '../logger';

export class SqlUtils {
private static readonly DB_POOL = createPool({
Expand Down
9 changes: 8 additions & 1 deletion src/app/middleware/logger.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ export const requestLogger = (blacklist: string[] = []): RequestHandler => (
res: Response,
next: NextFunction
) => {
const start = process.hrtime();

next();

if (blacklist.some(url => req.originalUrl.startsWith(url))) return;

Logger.log(res.statusCode, req.method, req.originalUrl);
res.on('finish', () => {
const end = process.hrtime(start);
const elapsedTime = `${Math.floor(end[0] * 1000 + end[1] / 1000000)}ms`;

Logger.log(req.method, req.originalUrl + ';', res.statusCode, res.statusMessage + ';', elapsedTime);
});
};
8 changes: 3 additions & 5 deletions src/app/repositories/item.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { PoolConnection } from 'mysql';
import { Item } from '../data/item';

export class ItemRepository {

static getItem(itemId: number, connection: PoolConnection): Promise<{ statusCode: number, items?: Item[] }> {
static getItem(itemId: number, connection: PoolConnection): Promise<{ statusCode: number; items?: Item[] }> {
return new Promise(resolve => {
connection.query('SELECT * FROM Item WHERE id = ?', itemId, (outerError, results: Item[]) => {
if (outerError) {
resolve({ statusCode: 500 });
} else if (results && results.length > 0) {
resolve({
statusCode: 200,
items: results
items: results,
});
} else {
resolve({ statusCode: 404 });
Expand All @@ -20,7 +19,7 @@ export class ItemRepository {
});
}

static getItems(query: string, connection: PoolConnection): Promise<{ statusCode: number, items?: Item[] }> {
static getItems(query: string, connection: PoolConnection): Promise<{ statusCode: number; items?: Item[] }> {
return new Promise(resolve => {
connection.query('SELECT * FROM Item WHERE name LIKE ?', [`%${query}%`], (outerError, results: Item[]) => {
if (outerError) {
Expand All @@ -33,5 +32,4 @@ export class ItemRepository {
});
});
}

}
2 changes: 1 addition & 1 deletion src/app/routes/health.router-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application, Router } from 'express';
import { SqlUtils } from '../common/sql-utils';
import { SqlUtils } from '../common/utils/sql-utils';
import { HealthRepository } from '../repositories/health.repository';
import { RouterFactory } from './router.interface';

Expand Down
2 changes: 1 addition & 1 deletion src/app/routes/item.router-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application, Router } from 'express';
import { SqlUtils } from '../common/sql-utils';
import { SqlUtils } from '../common/utils/sql-utils';
import { ItemRepository } from '../repositories/item.repository';
import { RouterFactory } from './router.interface';

Expand Down
2 changes: 1 addition & 1 deletion src/app/routes/news.router-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application, Router } from 'express';
import { SqlUtils } from '../common/sql-utils';
import { SqlUtils } from '../common/utils/sql-utils';
import { NewsRepository } from '../repositories/news.repository';
import { RouterFactory } from './router.interface';

Expand Down
2 changes: 1 addition & 1 deletion src/app/routes/player.router-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application, Router } from 'express';
import { SqlUtils } from '../common/sql-utils';
import { SqlUtils } from '../common/utils/sql-utils';
import { PlayerRepository } from '../repositories/player.repository';
import { RouterFactory } from './router.interface';

Expand Down
2 changes: 1 addition & 1 deletion src/app/routes/xp.router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application, Router } from 'express';
import { SqlUtils } from '../common/sql-utils';
import { SqlUtils } from '../common/utils/sql-utils';
import { XpRepository } from '../repositories/xp.repository';
import { RouterFactory } from './router.interface';

Expand Down
1 change: 1 addition & 0 deletions src/config/config.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const dbCredentials: PoolConfig = JSON.parse(readFileSync('src/config/secrets/db

export const config: IConfig = {
port: Number(process.env.PORT) || 8080,
portMetrics: Number(process.env.PORT_METRICS) || 8088,
poolConfig: Object.assign(dbCredentials, {
ssl: {
ca: readFileSync('src/config/secrets/db-ca.pem'),
Expand Down
1 change: 1 addition & 0 deletions src/config/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { PoolConfig } from 'mysql';

export interface IConfig {
port: number;
portMetrics: number;
poolConfig: PoolConfig;
}
1 change: 1 addition & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const dbCredentials: PoolConfig = JSON.parse(readFileSync('/run/secrets/db-osrs-

export const config: IConfig = {
port: Number(process.env.PORT) || 8080,
portMetrics: Number(process.env.PORT_METRICS) || 8088,
poolConfig: Object.assign(dbCredentials, {
ssl: {
ca: readFileSync('/run/secrets/db-ca.pem'),
Expand Down
11 changes: 9 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import cluster from 'cluster';
import * as express from 'express';
import { clusterMetrics } from 'express-prom-bundle';
import os from 'os';
import { App } from './app/app';
import { FileSystemUtils } from './app/common/file-system-utils';
import { Logger } from './app/common/logger';
import { FileSystemUtils } from './app/common/utils/file-system-utils';
import { config } from './config/config';

if (cluster.isMaster) {
FileSystemUtils.createIconsFolderIfMissing();
Logger.log('OSRS TRACKER API ACTIVE - FORKING WORKERS');

os.cpus().forEach(() => cluster.fork());

const metricsApp = express();
metricsApp.use('/metrics', clusterMetrics());
metricsApp.listen(config.portMetrics);

cluster.on('exit', worker => {
Logger.log(`WORKER ${worker.id} DIED - CREATING NEW WORKER`);
cluster.fork();
});
} else {
new App().start(cluster.worker);
App.run(cluster.worker);
}

0 comments on commit c9c2b9a

Please sign in to comment.