core: export metrics with tx2

This commit is contained in:
undefined committed 2021-09-15 18:11:43 +08:00
1 parent 14df7d7a05
commit 7f25778671
5 files changed
+21 -9

No files matched your search

+5 -4
View File
@@ -34,16 +34,17 @@ export function addon(addonPath: string, prepend = false) {
global.publicDirs[prepend ? 'push' : 'unshift'](publicPath);
const targets = fs.readdirSync(publicPath);
for (const target of targets) {
if (global.ui.manifest[target] && !prepend) {
global.ui.manifest[target] = publicPath;
} else if (!global.staticFiles[target]) {
global.ui.manifest[target] = publicPath;
if (global.Hydro.ui.manifest[target] && !prepend) {
global.Hydro.ui.manifest[target] = publicPath;
} else if (!global.Hydro.ui.manifest[target]) {
global.Hydro.ui.manifest[target] = publicPath;
}
}
}
global.addons[prepend ? 'unshift' : 'push'](modulePath);
} catch (e) {
logger.error(`Addon not found: ${addonPath}`);
logger.error(e);
}
} else logger.error(`Addon not found: ${addonPath}`);
}
+10
View File
@@ -16,6 +16,7 @@ import {
import moment from 'moment-timezone';
import { ObjectID } from 'mongodb';
import sockjs from 'sockjs';
import tx2 from 'tx2';
import { parseMemoryMB } from '@hydrooj/utils/lib/utils';
import {
BlacklistedError, CsrfTokenError, HydroError,
@@ -39,6 +40,12 @@ import * as bus from './bus';
const argv = cac().parse();
const logger = new Logger('server');
const reqCount = tx2.meter({
name: 'req/sec',
samples: 1,
timeframe: 60,
});
const connCount = tx2.counter({ name: 'connections' });
export const app = new Koa();
export const server = http.createServer(app.callback());
export const router = new Router();
@@ -664,6 +671,7 @@ async function bail(name: string, ...args: any[]) {
}
async function handle(ctx, HandlerClass, checker) {
reqCount.mark();
const args = {
domainId: 'system', ...ctx.params, ...ctx.query, ...ctx.request.body, __start: Date.now(),
};
@@ -862,9 +870,11 @@ export function Connection(
h.message(JSON.parse(data));
});
}
connCount.inc();
conn.on('close', async () => {
if (h.cleanup) await h.cleanup(args);
if (h.finish) await h.finish(args);
connCount.dec();
});
} catch (e) {
logger.warn('%o', e);