Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SERVICES-2575] Fix local cache misses #1456

Merged
merged 2 commits into from
Sep 10, 2024
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
23 changes: 22 additions & 1 deletion src/helpers/decorators/caching.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
formatNullOrUndefined,
parseCachedNullOrUndefined,
} from 'src/utils/cache.utils';
import { MetricsCollector } from 'src/utils/metrics.collector';

export interface ICachingOptions {
baseKey: string;
Expand Down Expand Up @@ -34,15 +35,33 @@ export function GetOrSetCache(cachingOptions: ICachingOptions) {
...args,
);

const genericCacheKey = generateCacheKeyFromParams(
cachingOptions.baseKey,
propertyKey,
);

if (context && context.deepHistoryTimestamp) {
cacheKey = `${cacheKey}.${context.deepHistoryTimestamp}`;
}

const cachingService: CacheService = this.cachingService;

const cachedValue = await cachingService.get(cacheKey);
const locallyCachedValue = await cachingService.getLocal(cacheKey);
if (locallyCachedValue !== undefined) {
MetricsCollector.incrementLocalCacheHit(genericCacheKey);

return parseCachedNullOrUndefined(locallyCachedValue);
}

const cachedValue = await cachingService.getRemote(cacheKey);
if (cachedValue !== undefined) {
MetricsCollector.incrementCachedApiHit(genericCacheKey);

cachingService.setLocal(
cacheKey,
cachedValue,
cachingOptions.localTtl,
);
return parseCachedNullOrUndefined(cachedValue);
}

Expand All @@ -62,6 +81,8 @@ export function GetOrSetCache(cachingOptions: ICachingOptions) {
localTtl,
);

MetricsCollector.incrementCacheMiss(genericCacheKey);

return value;
};
return descriptor;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/farm/specs/farm.compute.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('FarmService', () => {
const service = module.get<FarmComputeServiceV1_2>(
FarmComputeServiceV1_2,
);
const farmedTokenPriceUSD = await service.computeFarmedTokenPriceUSD(
const farmedTokenPriceUSD = await service.farmedTokenPriceUSD(
Address.fromHex(
'0000000000000000000000000000000000000000000000000000000000000021',
).bech32(),
Expand Down
33 changes: 33 additions & 0 deletions src/utils/metrics.collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class MetricsCollector {
private static guestQueriesGauge: Gauge<string>;
private static currentNonceGauge: Gauge<string>;
private static lastProcessedNonceGauge: Gauge<string>;
private static localCacheHitGauge: Gauge<string>;
private static cacheMissGauge: Gauge<string>;

private static baseMetrics = new MetricsService();

Expand Down Expand Up @@ -92,6 +94,22 @@ export class MetricsCollector {
labelNames: ['operation'],
});
}

if (!MetricsCollector.localCacheHitGauge) {
MetricsCollector.localCacheHitGauge = new Gauge({
name: 'local_cached_hits',
help: 'Number of hits for local cached data',
labelNames: ['key'],
});
}

if (!MetricsCollector.cacheMissGauge) {
MetricsCollector.cacheMissGauge = new Gauge({
name: 'cache_misses',
help: 'Number of cache misses',
labelNames: ['key'],
});
}
}

static setFieldDuration(name: string, path: string, duration: number) {
Expand Down Expand Up @@ -196,6 +214,21 @@ export class MetricsCollector {
MetricsService.setGuestHitQueries(count);
}

static incrementLocalCacheHit(key: string) {
MetricsCollector.ensureIsInitialized();
MetricsCollector.localCacheHitGauge.inc({ key });
}

static incrementCachedApiHit(endpoint: string) {
MetricsCollector.ensureIsInitialized();
MetricsCollector.baseMetrics.incrementCachedApiHit(endpoint);
}

static incrementCacheMiss(key: string) {
MetricsCollector.ensureIsInitialized();
MetricsCollector.cacheMissGauge.inc({ key });
}

static async getMetrics(): Promise<string> {
const baseMetrics = await MetricsCollector.baseMetrics.getMetrics();
const currentMetrics = await register.metrics();
Expand Down
Loading