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
@@ -1,62 +1,65 @@
|
||||
import {
|
||||
collectDefaultMetrics, Counter, Gauge, Metric, Registry,
|
||||
} from 'prom-client';
|
||||
import { db } from 'hydrooj';
|
||||
import * as bus from 'hydrooj/src/service/bus';
|
||||
import { Context, db } from 'hydrooj';
|
||||
|
||||
export const registry = new Registry();
|
||||
registry.setDefaultLabels({ instanceid: process.env.NODE_APP_INSTANCE });
|
||||
function createMetric<Q extends string, T extends (new (a: any) => Metric<Q>)>(
|
||||
C: T, name: string, help: string, extra?: T extends new (a: infer R) => any ? Partial<R> : never,
|
||||
): T extends (new (a) => Gauge<Q>) ? Gauge<Q> : T extends (new (a) => Counter<Q>) ? Counter<Q> : Metric<Q> {
|
||||
const metric = new C({ name, help, ...(extra || {}) });
|
||||
registry.registerMetric(metric);
|
||||
return metric as any;
|
||||
export function createRegistry(ctx: Context) {
|
||||
const registry = new Registry();
|
||||
|
||||
registry.setDefaultLabels({ instanceid: process.env.NODE_APP_INSTANCE });
|
||||
function createMetric<Q extends string, T extends (new (a: any) => Metric<Q>)>(
|
||||
C: T, name: string, help: string, extra?: T extends new (a: infer R) => any ? Partial<R> : never,
|
||||
): T extends (new (a) => Gauge<Q>) ? Gauge<Q> : T extends (new (a) => Counter<Q>) ? Counter<Q> : Metric<Q> {
|
||||
const metric = new C({ name, help, ...(extra || {}) });
|
||||
registry.registerMetric(metric);
|
||||
return metric as any;
|
||||
}
|
||||
|
||||
const reqCounter = createMetric(Counter, 'hydro_reqcount', 'reqcount', {
|
||||
labelNames: ['domainId'],
|
||||
});
|
||||
ctx.on('handler/create', (h) => reqCounter.inc({ domainId: h.args.domainId }));
|
||||
|
||||
const judgeCounter = createMetric(Counter, 'hydro_judgecount', 'judgecount');
|
||||
ctx.on('record/judge', () => judgeCounter.inc());
|
||||
|
||||
createMetric(Gauge, 'hydro_regcount', 'regcount', {
|
||||
async collect() {
|
||||
this.set({}, await db.collection('user').countDocuments());
|
||||
},
|
||||
});
|
||||
|
||||
const submissionCounter = createMetric(Counter, 'hydro_submission', 'submissioncount', {
|
||||
labelNames: ['lang', 'domainId'],
|
||||
});
|
||||
ctx.on('handler/after/ProblemSubmit', (that) => {
|
||||
submissionCounter.inc({ lang: that.args.lang, domainId: that.args.domainId });
|
||||
});
|
||||
|
||||
const connectionGauge = createMetric(Gauge, 'hydro_connection', 'connectioncount', {
|
||||
labelNames: ['domainId'],
|
||||
});
|
||||
ctx.on('connection/active', (h) => {
|
||||
connectionGauge.inc({ domainId: h.args.domainId });
|
||||
});
|
||||
ctx.on('connection/close', (h) => {
|
||||
connectionGauge.dec({ domainId: h.args.domainId });
|
||||
});
|
||||
|
||||
const taskColl = db.collection('task');
|
||||
createMetric(Gauge, 'hydro_task', 'taskcount', {
|
||||
async collect() {
|
||||
const data = await taskColl.aggregate([
|
||||
{ $group: { _id: '$type', count: { $sum: 1 } } },
|
||||
]).toArray();
|
||||
this.reset();
|
||||
for (const line of data) {
|
||||
this.set({ type: line._id as unknown as string }, line.count);
|
||||
}
|
||||
},
|
||||
labelNames: ['type'],
|
||||
});
|
||||
|
||||
collectDefaultMetrics({ register: registry });
|
||||
return registry;
|
||||
}
|
||||
|
||||
const reqCounter = createMetric(Counter, 'hydro_reqcount', 'reqcount', {
|
||||
labelNames: ['domainId'],
|
||||
});
|
||||
bus.on('handler/create', (h) => reqCounter.inc({ domainId: h.args.domainId }));
|
||||
|
||||
const judgeCounter = createMetric(Counter, 'hydro_judgecount', 'judgecount');
|
||||
bus.on('record/judge', () => judgeCounter.inc());
|
||||
|
||||
createMetric(Gauge, 'hydro_regcount', 'regcount', {
|
||||
async collect() {
|
||||
this.set({}, await db.collection('user').countDocuments());
|
||||
},
|
||||
});
|
||||
|
||||
const submissionCounter = createMetric(Counter, 'hydro_submission', 'submissioncount', {
|
||||
labelNames: ['lang', 'domainId'],
|
||||
});
|
||||
bus.on('handler/after/ProblemSubmit', (that) => {
|
||||
submissionCounter.inc({ lang: that.args.lang, domainId: that.args.domainId });
|
||||
});
|
||||
|
||||
const connectionGauge = createMetric(Gauge, 'hydro_connection', 'connectioncount', {
|
||||
labelNames: ['domainId'],
|
||||
});
|
||||
bus.on('connection/active', (h) => {
|
||||
connectionGauge.inc({ domainId: h.args.domainId });
|
||||
});
|
||||
bus.on('connection/close', (h) => {
|
||||
connectionGauge.dec({ domainId: h.args.domainId });
|
||||
});
|
||||
|
||||
const taskColl = db.collection('task');
|
||||
createMetric(Gauge, 'hydro_task', 'taskcount', {
|
||||
async collect() {
|
||||
const data = await taskColl.aggregate([
|
||||
{ $group: { _id: '$type', count: { $sum: 1 } } },
|
||||
]).toArray();
|
||||
this.reset();
|
||||
for (const line of data) {
|
||||
this.set({ type: line._id as unknown as string }, line.count);
|
||||
}
|
||||
},
|
||||
labelNames: ['type'],
|
||||
});
|
||||
|
||||
collectDefaultMetrics({ register: registry });
|
||||
Reference in new issue
Block a user