Add captcha solves metric

This commit is contained in:
mrjvs
2023-12-21 20:11:17 +01:00
parent 5ebecd1476
commit 07ecd445f9
2 changed files with 32 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ const log = scopedLogger('metrics');
export type Metrics = {
user: Counter<'namespace'>;
captchaSolves: Counter<'success'>;
providerHostnames: Counter<'hostname'>;
providerStatuses: Counter<'provider_id' | 'status'>;
watchMetrics: Counter<'title' | 'tmdb_full_id' | 'provider_id' | 'success'>;
@@ -38,6 +39,11 @@ export async function setupMetrics(app: FastifyInstance) {
help: 'mw_user_help',
labelNames: ['namespace'],
}),
captchaSolves: new Counter({
name: 'mw_captcha_solves',
help: 'mw_captcha_solves',
labelNames: ['success'],
}),
providerHostnames: new Counter({
name: 'mw_provider_hostname_count',
help: 'mw_provider_hostname_count',

View File

@@ -58,7 +58,7 @@ export const metricsRouter = makeRouter((app) => {
if (lastItem) {
getMetrics().watchMetrics.inc({
tmdb_full_id: lastItem.tmdbId,
tmdb_full_id: lastItem.type + '-' + lastItem.tmdbId,
provider_id: lastSuccessfulItem?.providerId ?? lastItem.providerId,
title: lastItem.title,
success: (!!lastSuccessfulItem).toString(),
@@ -68,4 +68,29 @@ export const metricsRouter = makeRouter((app) => {
return true;
}),
);
app.post(
'/metrics/captcha',
{
schema: {
body: z.object({
success: z.boolean(),
}),
},
},
handle(async ({ body, req, limiter }) => {
await limiter?.assertAndBump(req, {
id: 'captcha_solves',
max: 300,
inc: 1,
window: '30m',
});
getMetrics().captchaSolves.inc({
success: body.success.toString(),
});
return true;
}),
);
});