From 407ff70ae01d50ae78d90b2a295f4da11ec0318a Mon Sep 17 00:00:00 2001 From: steunix Date: Tue, 5 Aug 2025 22:33:56 +0200 Subject: [PATCH 1/2] Add metrics for onetime tokens Fixes #326 --- api/v1/controllers/onetimetokens.mjs | 7 +++++++ docs/index.md | 2 ++ lib/const.mjs | 2 ++ passweaver-api.mjs | 2 ++ test/metrics.spec.cjs | 13 ++++++++++++- 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/api/v1/controllers/onetimetokens.mjs b/api/v1/controllers/onetimetokens.mjs index b299778..b7ff9be 100644 --- a/api/v1/controllers/onetimetokens.mjs +++ b/api/v1/controllers/onetimetokens.mjs @@ -15,6 +15,7 @@ import * as Const from '../../../lib/const.mjs' import * as JV from '../../../lib/jsonvalidator.mjs' import * as Items from '../../../model/item.mjs' import * as KMS from '../../../lib/kms/kms.mjs' +import * as Metrics from '../../../lib/metrics.mjs' import jsonwebtoken from 'jsonwebtoken' @@ -111,6 +112,9 @@ export async function get (req, res, next) { Events.add(req.user, Const.EV_ACTION_READ, Const.EV_ENTITY_ONETIMESHARE, ottoken.id) Events.add(req.user, Const.EV_ACTION_READVIATOKEN, Const.EV_ENTITY_ITEM, ottoken.itemid) } + + // Update metrics + Metrics.inc(Const.METRICS_ONETIMETOKENS_READ) res.send(R.ok(resp)) } @@ -192,5 +196,8 @@ export async function create (req, res, next) { Events.add(req.user, Const.EV_ACTION_CREATE, Const.EV_ENTITY_ONETIMESHARE, created.id) Events.add(req.user, Const.EV_ACTION_ITEMSHARE, Const.EV_ENTITY_ITEM, req.body.itemid) } + + // Update metrics + Metrics.inc(Const.METRICS_ONETIMETOKENS_CREATED) res.status(R.CREATED).send(R.ok({ token: newToken })) } diff --git a/docs/index.md b/docs/index.md index 14f4ee3..7edf712 100644 --- a/docs/index.md +++ b/docs/index.md @@ -309,7 +309,9 @@ If enabled in configuration, PassWeaver API export various metrics (along defaul - Users logins count (`login_users_total`) - API keys logins count (`login_apikeys_total`) - Item create, update, delete and read count (`items_created_total`, `items_updated_total`, `items_deleted_total`, `items_read_total`) + - One time tokens count (`onetimetokens_created_total`, `onetimetokens_read_total`) - KMS encryptions and decryptions count (`kms_encryptions_total`, `kms_decryptions_total`) + - KMS encryptions and descriptions for each KMS ## Cache diff --git a/lib/const.mjs b/lib/const.mjs index 23aa6cc..60abfe2 100644 --- a/lib/const.mjs +++ b/lib/const.mjs @@ -113,6 +113,8 @@ export const METRICS_ITEMS_READ = 'items_read_total' export const METRICS_ITEMS_CREATED = 'items_created_total' export const METRICS_ITEMS_UPDATED = 'items_updated_total' export const METRICS_ITEMS_DELETED = 'items_deleted_total' +export const METRICS_ONETIMETOKENS_CREATED = 'onetimetokens_created_total' +export const METRICS_ONETIMETOKENS_READ = 'onetimetokens_read_total' export const METRICS_KMS_ENCRYPTIONS = 'kms_encryptions_total' export const METRICS_KMS_DECRYPTIONS = 'kms_decryptions_total' export const METRICS_KMS_ENCRYPTIONS_PER_KMS = 'kms_encryptions_per_kms_total' diff --git a/passweaver-api.mjs b/passweaver-api.mjs index e950030..5321f3f 100644 --- a/passweaver-api.mjs +++ b/passweaver-api.mjs @@ -129,6 +129,8 @@ if (cfg?.enable_metrics) { Metrics.createCounter(Const.METRICS_ITEMS_DELETED, 'Deleted items') Metrics.createCounter(Const.METRICS_ITEMS_UPDATED, 'Updated items') Metrics.createCounter(Const.METRICS_ITEMS_READ, 'Read items') + Metrics.createCounter(Const.METRICS_ONETIMETOKENS_CREATED, 'One time tokens created') + Metrics.createCounter(Const.METRICS_ONETIMETOKENS_READ, 'One time tokens read') Metrics.createCounter(Const.METRICS_KMS_ENCRYPTIONS, 'Encryptions') Metrics.createCounter(Const.METRICS_KMS_DECRYPTIONS, 'Decryptions') Metrics.createCounter(Const.METRICS_KMS_ENCRYPTIONS_PER_KMS, 'Encryptions per KMS', 'kms_description') diff --git a/test/metrics.spec.cjs b/test/metrics.spec.cjs index 24c6a4a..8859917 100644 --- a/test/metrics.spec.cjs +++ b/test/metrics.spec.cjs @@ -18,7 +18,18 @@ describe('Metrics', function () { .catch(v => v) assert.strictEqual(res1.status, 200) - // Check that the new per-KMS metrics are included in the output + + // Check that all metrics are present + assert.match(res1.text, /login_users_total/) + assert.match(res1.text, /login_apikeys_total/) + assert.match(res1.text, /items_read_total/) + assert.match(res1.text, /items_created_total/) + assert.match(res1.text, /items_updated_total/) + assert.match(res1.text, /items_deleted_total/) + assert.match(res1.text, /onetimetokens_created_total/) + assert.match(res1.text, /onetimetokens_read_total/) + assert.match(res1.text, /kms_encryptions_total/) + assert.match(res1.text, /kms_decryptions_total/) assert.match(res1.text, /kms_encryptions_per_kms_total/) assert.match(res1.text, /kms_decryptions_per_kms_total/) }) From abdde2faec611c4231739184012d4889a2b5bd36 Mon Sep 17 00:00:00 2001 From: steunix Date: Tue, 5 Aug 2025 22:43:35 +0200 Subject: [PATCH 2/2] Add metrics for onetime tokens Fixes #326 --- api/v1/controllers/onetimetokens.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/v1/controllers/onetimetokens.mjs b/api/v1/controllers/onetimetokens.mjs index b7ff9be..bf5977e 100644 --- a/api/v1/controllers/onetimetokens.mjs +++ b/api/v1/controllers/onetimetokens.mjs @@ -114,7 +114,7 @@ export async function get (req, res, next) { } // Update metrics - Metrics.inc(Const.METRICS_ONETIMETOKENS_READ) + Metrics.counterInc(Const.METRICS_ONETIMETOKENS_READ) res.send(R.ok(resp)) } @@ -198,6 +198,6 @@ export async function create (req, res, next) { } // Update metrics - Metrics.inc(Const.METRICS_ONETIMETOKENS_CREATED) + Metrics.counterInc(Const.METRICS_ONETIMETOKENS_CREATED) res.status(R.CREATED).send(R.ok({ token: newToken })) }