From 0b9863814c00637ffdade4331639e34bb325d846 Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Tue, 13 Jan 2026 20:32:46 -0800 Subject: [PATCH] fix: shard app usages properly by user (#2279) --- .../services/MeteringService/MeteringService.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/backend/src/services/MeteringService/MeteringService.ts b/src/backend/src/services/MeteringService/MeteringService.ts index 0fcc7b825..3b4b3e2e2 100644 --- a/src/backend/src/services/MeteringService/MeteringService.ts +++ b/src/backend/src/services/MeteringService/MeteringService.ts @@ -51,14 +51,8 @@ export class MeteringService { return key; } - /** - * adds some randomized number from 0-99 to the usage key to help spread writes - * @param appId - * @param currentMonth - * @returns - */ - #generateAppUsageKey (appId: string, currentMonth: string) { - const hashOfApp = murmurhash.v3(`${appId}`) % MeteringService.APP_SHARD_COUNT; + #generateAppUsageKey (appId: string, userId: string, currentMonth: string) { + const hashOfApp = murmurhash.v3(`${appId}${userId}`) % MeteringService.APP_SHARD_COUNT; const key = `${METRICS_PREFIX}:app:${appId}:${hashOfApp}:${currentMonth}`; return key; } @@ -127,7 +121,7 @@ export class MeteringService { }); if ( appId !== GLOBAL_APP_KEY ) { - const appUsageKey = this.#generateAppUsageKey(appId, currentMonth); + const appUsageKey = this.#generateAppUsageKey(appId, userId, currentMonth); this.#kvStore.incr({ key: appUsageKey, pathAndAmountMap, @@ -283,7 +277,7 @@ export class MeteringService { console.warn('Failed to increment aux usage data \'actorAppUsageKey\' with error: ', e); }); - const appUsageKey = this.#generateAppUsageKey(appId, currentMonth); + const appUsageKey = this.#generateAppUsageKey(appId, userId, currentMonth); this.#kvStore.incr({ key: appUsageKey, pathAndAmountMap: aggregatedPathAndAmountMap,