Skip to content
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
39 changes: 39 additions & 0 deletions lib/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as client from 'prom-client';
import os from 'os';
import { nanoid } from 'nanoid';

const register = new client.Registry();

client.collectDefaultMetrics({ register });

export { register, client };

/**
* Start periodic push to pushgateway
*
* @param workerName - name of the worker for grouping
*/
export function startMetricsPushing(workerName: string): void {
const url = process.env.PROMETHEUS_PUSHGATEWAY_URL;
const interval = parseInt(process.env.PROMETHEUS_PUSHGATEWAY_INTERVAL || '10000');

if (!url) {
return;
}

const hostname = os.hostname();
const ID_SIZE = 5;
const id = nanoid(ID_SIZE);

const gateway = new client.Pushgateway(url, [], register);

console.log(`Start pushing metrics to ${url} every ${interval}ms (host: ${hostname}, id: ${id})`);

setInterval(() => {
gateway.pushAdd({ jobName: 'workers', groupings: { worker: workerName, host: hostname, id } }, (err) => {
if (err) {
console.error('Metrics push error:', err);
}
});
}, interval);
}
89 changes: 18 additions & 71 deletions runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as utils from './lib/utils';
import { Worker } from './lib/worker';
import HawkCatcher from '@hawk.so/nodejs';
import * as dotenv from 'dotenv';
import { startMetricsPushing } from './lib/metrics';

dotenv.config();

Expand Down Expand Up @@ -57,19 +58,17 @@ class WorkerRunner {
.then((workerConstructors) => {
this.constructWorkers(workerConstructors);
})
// .then(() => {
// try {
// this.startMetrics();
// } catch (e) {
// HawkCatcher.send(e);
// console.error(`Metrics not started: ${e}`);
// }
//
// return Promise.resolve();
// })
.then(() => {
return this.startWorkers();
})
.then(() => {
try {
this.startMetrics();
} catch (e) {
HawkCatcher.send(e);
console.error(`Metrics not started: ${e}`);
}
})
.then(() => {
this.observeProcess();
})
Expand All @@ -82,67 +81,15 @@ class WorkerRunner {
/**
* Run metrics exporter
*/
// private startMetrics(): void {
// if (!process.env.PROMETHEUS_PUSHGATEWAY_URL) {
// return;
// }
//
// const PUSH_INTERVAL = parseInt(process.env.PROMETHEUS_PUSHGATEWAY_INTERVAL);
//
// if (isNaN(PUSH_INTERVAL)) {
// throw new Error('PROMETHEUS_PUSHGATEWAY_INTERVAL is invalid or not set');
// }
//
// const collectDefaultMetrics = promClient.collectDefaultMetrics;
// const Registry = promClient.Registry;
//
// const register = new Registry();
// const startGcStats = gcStats(register);
//
// const hostname = os.hostname();
//
// const ID_SIZE = 5;
// const id = nanoid(ID_SIZE);
//
// // eslint-disable-next-line node/no-deprecated-api
// const instance = url.parse(process.env.PROMETHEUS_PUSHGATEWAY_URL).host;
//
// // Initialize metrics for workers
// this.workers.forEach((worker) => {
// // worker.initMetrics();
// worker.getMetrics().forEach((metric: promClient.Counter<string>) => register.registerMetric(metric));
// });
//
// collectDefaultMetrics({ register });
// startGcStats();
//
// this.gateway = new promClient.Pushgateway(process.env.PROMETHEUS_PUSHGATEWAY_URL, null, register);
//
// console.log(`Start pushing metrics to ${process.env.PROMETHEUS_PUSHGATEWAY_URL}`);
//
// // Pushing metrics to the pushgateway every PUSH_INTERVAL
// this.pushIntervalNumber = setInterval(() => {
// this.workers.forEach((worker) => {
// if (!this.gateway || !instance) {
// return;
// }
// // Use pushAdd not to overwrite previous metrics
// this.gateway.pushAdd({
// jobName: 'workers',
// groupings: {
// worker: worker.type.replace('/', '_'),
// host: hostname,
// id,
// },
// }, (err?: Error) => {
// if (err) {
// HawkCatcher.send(err);
// console.log(`Error of pushing metrics to gateway: ${err}`);
// }
// });
// });
// }, PUSH_INTERVAL);
// }
private startMetrics(): void {
if (!process.env.PROMETHEUS_PUSHGATEWAY_URL) {
return;
}

this.workers.forEach((worker) => {
startMetricsPushing(worker.type.replace('/', '_'));
});
}

/**
* Dynamically loads workers through the yarn workspaces
Expand Down
Loading