From fcdbc4e8781303b340afd3710d4ccd7b63f0f6af Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Thu, 12 Mar 2026 20:16:01 +0100 Subject: [PATCH] fix metrics cardinality and stream key cache counts --- apps/chat/src/index.ts | 7 ++++++- apps/web/src/lib/instrumentation/syncStreamKeys.ts | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/chat/src/index.ts b/apps/chat/src/index.ts index f560bd9..407474d 100644 --- a/apps/chat/src/index.ts +++ b/apps/chat/src/index.ts @@ -768,7 +768,12 @@ app.get( chatUser.isBot ? 'bot' : 'user', Buffer.byteLength(message) ); - recordUniqueChatter(chatUser.isBot ? 'bot' : 'user'); + const isFirstMessageFromUser = + (await redis.sadd(`chat:unique-chatters:${targetUsername}`, chatUser.id)) === 1; + + if (isFirstMessageFromUser) { + recordUniqueChatter(chatUser.isBot ? 'bot' : 'user'); + } outcome = 'broadcast'; } if (msg.type === 'emojiMsg') { diff --git a/apps/web/src/lib/instrumentation/syncStreamKeys.ts b/apps/web/src/lib/instrumentation/syncStreamKeys.ts index b240ebd..d1f10db 100644 --- a/apps/web/src/lib/instrumentation/syncStreamKeys.ts +++ b/apps/web/src/lib/instrumentation/syncStreamKeys.ts @@ -12,6 +12,7 @@ export default async function syncStreamKeys() { }); if (keys.length === 0) { + setCacheEntryCount('stream_keys', 0); console.log('No stream keys found to sync.'); return; } @@ -19,15 +20,17 @@ export default async function syncStreamKeys() { const redis = getRedisConnection(); const pipeline = redis.pipeline(); + let syncedKeyCount = 0; for (const key of keys) { if (key.channel && key.channel.name) { pipeline.set(`streamKey:${key.channel.name}`, key.key); + syncedKeyCount += 1; } } await pipeline.exec(); - setCacheEntryCount('stream_keys', keys.length); - console.log(`Synced ${keys.length} stream keys to Redis`); + setCacheEntryCount('stream_keys', syncedKeyCount); + console.log(`Synced ${syncedKeyCount} stream keys to Redis`); }); } catch (error) { console.error('Failed to sync stream keys to Redis:', error);