Skip to content

Commit

Permalink
Add prometheus gauge metric for OBO cache (hit/miss/expired)
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed Jul 10, 2024
1 parent 1f2a902 commit dfb4820
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/src/auth/cache-gauge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { proxyRegister } from '@app/prometheus/types';
import { Gauge } from 'prom-client';

const labelNames = ['hit'] as const;

export const cacheGauge = new Gauge({
name: 'obo_cache',
help: 'Number of requests to the OBO cache. "hit" is the type of hit: "miss", "hit", or "expired".',
labelNames,
registers: [proxyRegister],
});
6 changes: 6 additions & 0 deletions server/src/auth/cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cacheGauge } from '@app/auth/cache-gauge';
import { getLogger } from '@app/logger';

const log = getLogger('obo-cache');
Expand All @@ -19,17 +20,22 @@ export class OboMemoryCache {
const value = this.cache.get(key);

if (value === undefined) {
cacheGauge.inc({ hit: 'miss' });

return null;
}

const [token, expiresAt] = value;

if (expiresAt <= now()) {
cacheGauge.inc({ hit: 'expired' });
this.cache.delete(key);

return null;
}

cacheGauge.inc({ hit: 'hit' });

return token;
}

Expand Down

0 comments on commit dfb4820

Please sign in to comment.