From 07ecd445f919e021a5c893af0cb270cd287c35a5 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Thu, 21 Dec 2023 20:11:17 +0100 Subject: [PATCH] Add captcha solves metric --- src/modules/metrics/index.ts | 6 ++++++ src/routes/metrics.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/modules/metrics/index.ts b/src/modules/metrics/index.ts index adb621c..e6f798f 100644 --- a/src/modules/metrics/index.ts +++ b/src/modules/metrics/index.ts @@ -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', diff --git a/src/routes/metrics.ts b/src/routes/metrics.ts index 9b54625..e560505 100644 --- a/src/routes/metrics.ts +++ b/src/routes/metrics.ts @@ -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; + }), + ); });