|
| 1 | +import { |
| 2 | + cacheEntries, |
| 3 | + cacheEvictionsTotal, |
| 4 | + cacheRequestsTotal, |
| 5 | + cacheSizeBytes, |
| 6 | + isMetricEnabled, |
| 7 | + meter, |
| 8 | +} from '@internal/monitoring/metrics' |
| 9 | +import { Attributes } from '@opentelemetry/api' |
| 10 | +import { CacheLookupOptions, OutcomeAwareCache } from './adapter' |
| 11 | +import { CacheName } from './names' |
| 12 | + |
| 13 | +type CacheDisposeHandler<K, V, R extends string> = (value: V, key: K, reason: R) => void |
| 14 | + |
| 15 | +type MonitorCacheOptions = { |
| 16 | + purgeStale?: () => void |
| 17 | +} |
| 18 | + |
| 19 | +function cacheAttrs( |
| 20 | + cache: CacheName, |
| 21 | + attrs?: Record<string, string | number | boolean> |
| 22 | +): Attributes { |
| 23 | + return attrs ? { cache, ...attrs } : { cache } |
| 24 | +} |
| 25 | + |
| 26 | +export function withCacheEvictionMetrics<K, V, R extends string>( |
| 27 | + cacheName: CacheName, |
| 28 | + dispose?: CacheDisposeHandler<K, V, R> |
| 29 | +): CacheDisposeHandler<K, V, R> { |
| 30 | + return (value, key, reason) => { |
| 31 | + if (reason === 'evict') { |
| 32 | + cacheEvictionsTotal.add(1, cacheAttrs(cacheName)) |
| 33 | + } |
| 34 | + |
| 35 | + dispose?.(value, key, reason) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class MonitoredCache<K, V, SetOptions = undefined> implements OutcomeAwareCache<K, V, SetOptions> { |
| 40 | + constructor( |
| 41 | + private readonly name: CacheName, |
| 42 | + private readonly cache: OutcomeAwareCache<K, V, SetOptions>, |
| 43 | + options?: MonitorCacheOptions |
| 44 | + ) { |
| 45 | + meter.addBatchObservableCallback( |
| 46 | + (observer) => { |
| 47 | + const cacheEntriesEnabled = isMetricEnabled('cache_entries') |
| 48 | + const cacheSizeBytesEnabled = isMetricEnabled('cache_size_bytes') |
| 49 | + |
| 50 | + if (!cacheEntriesEnabled && !cacheSizeBytesEnabled) { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + options?.purgeStale?.() |
| 55 | + const stats = this.cache.getStats() |
| 56 | + const attrs = cacheAttrs(this.name) |
| 57 | + |
| 58 | + if (cacheEntriesEnabled) { |
| 59 | + observer.observe(cacheEntries, stats.entries, attrs) |
| 60 | + } |
| 61 | + |
| 62 | + if (cacheSizeBytesEnabled) { |
| 63 | + observer.observe(cacheSizeBytes, stats.sizeBytes, attrs) |
| 64 | + } |
| 65 | + }, |
| 66 | + [cacheEntries, cacheSizeBytes] |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + get(key: K, options?: CacheLookupOptions): V | undefined { |
| 71 | + if (options?.recordMetrics === false) { |
| 72 | + return this.cache.get(key, options) |
| 73 | + } |
| 74 | + |
| 75 | + const { value, outcome } = this.cache.getWithOutcome(key) |
| 76 | + cacheRequestsTotal.add(1, cacheAttrs(this.name, { outcome })) |
| 77 | + |
| 78 | + return value |
| 79 | + } |
| 80 | + |
| 81 | + getWithOutcome(key: K) { |
| 82 | + return this.cache.getWithOutcome(key) |
| 83 | + } |
| 84 | + |
| 85 | + set(key: K, value: V, options?: SetOptions): void { |
| 86 | + this.cache.set(key, value, options) |
| 87 | + } |
| 88 | + |
| 89 | + delete(key: K): boolean { |
| 90 | + return this.cache.delete(key) |
| 91 | + } |
| 92 | + |
| 93 | + getStats() { |
| 94 | + return this.cache.getStats() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export function monitorCache<K, V, SetOptions = undefined>( |
| 99 | + cacheName: CacheName, |
| 100 | + cache: OutcomeAwareCache<K, V, SetOptions>, |
| 101 | + options?: MonitorCacheOptions |
| 102 | +): OutcomeAwareCache<K, V, SetOptions> { |
| 103 | + return new MonitoredCache(cacheName, cache, options) |
| 104 | +} |
0 commit comments