mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
plugins: mark entrypoint as index
This commit is contained in:
1 parent
1210957a2a
commit
833b960a48
14 files changed
+81
-81
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { AggregatorRegistry, metric } from 'prom-client';
|
||||
import { Context, Handler, SystemModel } from 'hydrooj';
|
||||
import { createRegistry } from './metrics';
|
||||
|
||||
declare module 'hydrooj' {
|
||||
interface EventMap {
|
||||
metrics: (id: string, metrics: any) => void;
|
||||
}
|
||||
interface SystemKeys {
|
||||
'prom-client.name': string;
|
||||
'prom-client.password': string;
|
||||
'prom-client.collect_rate': number;
|
||||
}
|
||||
}
|
||||
|
||||
const instances: Record<string, metric[]> = {};
|
||||
|
||||
class MetricsHandler extends Handler {
|
||||
noCheckPermView = true;
|
||||
notUsage = true;
|
||||
|
||||
async get() {
|
||||
if (!this.request.headers.authorization) {
|
||||
this.response.status = 401;
|
||||
this.response.body = {};
|
||||
this.response.addHeader('WWW-Authenticate', 'Basic');
|
||||
return;
|
||||
}
|
||||
const [name, password] = SystemModel.getMany(['prom-client.name', 'prom-client.password']);
|
||||
const key = this.request.headers.authorization?.split('Basic ')?.[1];
|
||||
if (!key || key !== Buffer.from(`${name}:${password}`).toString('base64')) {
|
||||
this.response.status = 403;
|
||||
this.response.body = {};
|
||||
return;
|
||||
}
|
||||
this.response.body = await AggregatorRegistry.aggregate(Object.values(instances)).metrics();
|
||||
this.response.type = 'text/plain';
|
||||
}
|
||||
}
|
||||
|
||||
export function apply(ctx: Context) {
|
||||
const registry = createRegistry(ctx);
|
||||
ctx.on('metrics', (id, metrics) => { instances[id] = metrics; });
|
||||
ctx.setInterval(async () => {
|
||||
ctx.broadcast('metrics', process.env.NODE_APP_INSTANCE!, await registry.getMetricsAsJSON());
|
||||
}, 5000 * (+SystemModel.get('prom-client.collect_rate') || 1));
|
||||
ctx.Route('metrics', '/metrics', MetricsHandler);
|
||||
}
|
||||
Reference in new issue
Block a user