From 58bf286ce0bf3a9d833d6206c691a1306e2c5db5 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 20 Sep 2024 12:34:54 -0500 Subject: [PATCH] Add cache metrics --- src/metrics.ts | 20 ++++++++++++++++++++ src/utils/SimpleLRU.ts | 11 ++++++++++- src/utils/favicon.ts | 3 ++- src/utils/lnurl.ts | 3 ++- src/utils/nip05.ts | 3 ++- src/utils/unfurl.ts | 2 ++ 6 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/metrics.ts b/src/metrics.ts index ac1db2ee..ccb4d382 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -84,3 +84,23 @@ export const dbQueryDurationHistogram = new Histogram({ name: 'ditto_db_query_duration_ms', help: 'Duration of database queries', }); + +export const cachedFaviconsSizeGauge = new Gauge({ + name: 'ditto_cached_favicons_size', + help: 'Number of domain favicons in cache', +}); + +export const cachedLnurlsSizeGauge = new Gauge({ + name: 'ditto_cached_lnurls_size', + help: 'Number of LNURL details in cache', +}); + +export const cachedNip05sSizeGauge = new Gauge({ + name: 'ditto_cached_nip05s_size', + help: 'Number of NIP-05 results in cache', +}); + +export const cachedLinkPreviewSizeGauge = new Gauge({ + name: 'ditto_cached_link_previews_size', + help: 'Number of link previews in cache', +}); diff --git a/src/utils/SimpleLRU.ts b/src/utils/SimpleLRU.ts index 0f2b5b37..c48b8d0c 100644 --- a/src/utils/SimpleLRU.ts +++ b/src/utils/SimpleLRU.ts @@ -1,6 +1,7 @@ // deno-lint-ignore-file ban-types import { LRUCache } from 'lru-cache'; +import { type Gauge } from 'prom-client'; type FetchFn = (key: K, opts: O) => Promise; @@ -8,6 +9,10 @@ interface FetchFnOpts { signal?: AbortSignal | null; } +type SimpleLRUOpts = LRUCache.Options & { + gauge?: Gauge; +}; + export class SimpleLRU< K extends {}, V extends {}, @@ -15,7 +20,7 @@ export class SimpleLRU< > { protected cache: LRUCache; - constructor(fetchFn: FetchFn, opts: LRUCache.Options) { + constructor(fetchFn: FetchFn, private opts: SimpleLRUOpts) { this.cache = new LRUCache({ fetchMethod: (key, _staleValue, { signal }) => fetchFn(key, { signal: signal as unknown as AbortSignal }), ...opts, @@ -24,9 +29,13 @@ export class SimpleLRU< async fetch(key: K, opts?: O): Promise { const result = await this.cache.fetch(key, opts); + + this.opts.gauge?.set(this.cache.size); + if (result === undefined) { throw new Error('SimpleLRU: fetch failed'); } + return result; } diff --git a/src/utils/favicon.ts b/src/utils/favicon.ts index 1fd0640a..49ca525a 100644 --- a/src/utils/favicon.ts +++ b/src/utils/favicon.ts @@ -2,6 +2,7 @@ import { DOMParser } from '@b-fuze/deno-dom'; import Debug from '@soapbox/stickynotes/debug'; import tldts from 'tldts'; +import { cachedFaviconsSizeGauge } from '@/metrics.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { Time } from '@/utils/time.ts'; import { fetchWorker } from '@/workers/fetch.ts'; @@ -37,7 +38,7 @@ const faviconCache = new SimpleLRU( throw new Error(`Favicon not found: ${key}`); }, - { max: 500, ttl: Time.hours(1) }, + { max: 500, ttl: Time.hours(1), gauge: cachedFaviconsSizeGauge }, ); export { faviconCache }; diff --git a/src/utils/lnurl.ts b/src/utils/lnurl.ts index ca7e1256..64e10fe3 100644 --- a/src/utils/lnurl.ts +++ b/src/utils/lnurl.ts @@ -1,6 +1,7 @@ import { LNURL, LNURLDetails } from '@nostrify/nostrify/ln'; import Debug from '@soapbox/stickynotes/debug'; +import { cachedLnurlsSizeGauge } from '@/metrics.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { Time } from '@/utils/time.ts'; import { fetchWorker } from '@/workers/fetch.ts'; @@ -20,7 +21,7 @@ const lnurlCache = new SimpleLRU( throw e; } }, - { max: 1000, ttl: Time.minutes(30) }, + { max: 1000, ttl: Time.minutes(30), gauge: cachedLnurlsSizeGauge }, ); /** Get an LNURL from a lud06 or lud16. */ diff --git a/src/utils/nip05.ts b/src/utils/nip05.ts index e9dd78cc..696b5078 100644 --- a/src/utils/nip05.ts +++ b/src/utils/nip05.ts @@ -4,6 +4,7 @@ import Debug from '@soapbox/stickynotes/debug'; import tldts from 'tldts'; import { Conf } from '@/config.ts'; +import { cachedNip05sSizeGauge } from '@/metrics.ts'; import { Storages } from '@/storages.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { Time } from '@/utils/time.ts'; @@ -43,7 +44,7 @@ const nip05Cache = new SimpleLRU( throw e; } }, - { max: 500, ttl: Time.hours(1) }, + { max: 500, ttl: Time.hours(1), gauge: cachedNip05sSizeGauge }, ); async function localNip05Lookup(store: NStore, localpart: string): Promise { diff --git a/src/utils/unfurl.ts b/src/utils/unfurl.ts index 8123c423..e749dbcb 100644 --- a/src/utils/unfurl.ts +++ b/src/utils/unfurl.ts @@ -5,6 +5,7 @@ import { unfurl } from 'unfurl.js'; import { Conf } from '@/config.ts'; import { PreviewCard } from '@/entities/PreviewCard.ts'; +import { cachedLinkPreviewSizeGauge } from '@/metrics.ts'; import { Time } from '@/utils/time.ts'; import { fetchWorker } from '@/workers/fetch.ts'; @@ -67,6 +68,7 @@ function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Prom } else { const card = unfurlCard(url, signal); previewCardCache.set(url, card); + cachedLinkPreviewSizeGauge.set(previewCardCache.size); return card; } }