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

Commit

Permalink
added metrics cluster support
Browse files Browse the repository at this point in the history
  • Loading branch information
Freek Mencke committed May 27, 2019
1 parent 06196a9 commit 7fa13ab
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ export class App {
this._app.use(bodyParser.urlencoded({ extended: true }));
this._app.use(bodyParser.json());
this._app.use(requestLogger(['/health', '/metrics']));
this._app.use(prometheusMetricsMiddleware({ includeMethod: true, includePath: true }));
this._app.use(
prometheusMetricsMiddleware({
autoregister: false,
customLabels: { app: 'osrs-tracker-api' },
includeMethod: true,
includePath: true,
})
);
}

private setupRouters(): void {
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
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
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 { 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();
Expand Down

0 comments on commit 7fa13ab

Please sign in to comment.