Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ If enabled in configuration, PassWeaver API export various metrics (along with d
- 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 (`kms_encryptions_per_kms_total`, `kms_decryptions_per_kms_total`)
- Cache hits and misses (`cache_hits_total`, `cache_misses_total`)

## Cache

Expand Down
6 changes: 5 additions & 1 deletion lib/cache.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import * as Config from './config.mjs'
import * as CacheNode from './cache/cache-node.mjs'
import * as CacheRedis from './cache/cache-redis.mjs'
import * as Metrics from './metrics.mjs'
import * as Const from './const.mjs'

let Cache

Expand Down Expand Up @@ -86,7 +88,9 @@ export async function resetItemTypes () {
* @param {string} key Key to retreive
*/
export async function get (user, key) {
return await Cache.get(`${prefix}:${key}.${user}.`)
const ret = await Cache.get(`${prefix}:${key}.${user}.`)
Metrics.counterInc(ret ? Const.METRICS_CACHE_HITS : Const.METRICS_CACHE_MISSES)
return ret
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/const.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,5 @@ export const METRICS_KMS_DECRYPTIONS = 'kms_decryptions_total'
export const METRICS_KMS_ENCRYPTIONS_PER_KMS = 'kms_encryptions_per_kms_total'
export const METRICS_KMS_DECRYPTIONS_PER_KMS = 'kms_decryptions_per_kms_total'
export const METRICS_LOGIN_APIKEYS_PER_KEY = 'login_apikeys_per_key_total'
export const METRICS_CACHE_HITS = 'cache_hits_total'
export const METRICS_CACHE_MISSES = 'cache_misses_total'
2 changes: 2 additions & 0 deletions passweaver-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ if (cfg?.enable_metrics) {
Metrics.createCounter(Const.METRICS_KMS_ENCRYPTIONS_PER_KMS, 'Encryptions per KMS', 'kms_description')
Metrics.createCounter(Const.METRICS_KMS_DECRYPTIONS_PER_KMS, 'Decryptions per KMS', 'kms_description')
Metrics.createCounter(Const.METRICS_LOGIN_APIKEYS_PER_KEY, 'Login per API key', 'apikey_description')
Metrics.createCounter(Const.METRICS_CACHE_HITS, 'Cache hits')
Metrics.createCounter(Const.METRICS_CACHE_MISSES, 'Cache misses')
}

// HTTP(S) server start
Expand Down
2 changes: 2 additions & 0 deletions test/metrics.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ describe('Metrics', function () {
assert.match(res1.text, /kms_encryptions_per_kms_total/)
assert.match(res1.text, /kms_decryptions_per_kms_total/)
assert.match(res1.text, /login_apikeys_per_key_total/)
assert.match(res1.text, /cache_hits_total/)
assert.match(res1.text, /cache_misses_total/)
})

it('Check per-KMS metrics', async () => {
Expand Down