feat: add segment source specific identify files - #174
Conversation
fbricon
left a comment
There was a problem hiding this comment.
The core idea is sound — making the identify cache key per-Segment-source prevents cross-extension cache collisions. However a couple of issues need addressing before merge:
writeKeytyped asany— should bestring | undefinedto matchgetSegmentKey()'s return type.undefinedwriteKey produces"undefined-identify"cache key — needs a fallback to preserve backward-compatible"identify"when no writeKey exists.- Redundant parameter on private method —
getIdentifyCacheNamealways receivesthis.writeKey, so it should just read the field directly. - No tests — the identify-caching logic (skip duplicate, cache miss, per-source isolation) is behavioral and testable.
| export class Reporter implements IReporter { | ||
|
|
||
| constructor(private analytics?: CoreAnalytics, private cacheService?: CacheService) { | ||
| constructor(private analytics?: CoreAnalytics, private cacheService?: CacheService, private writeKey?: any) { |
There was a problem hiding this comment.
writeKey should be typed string | undefined instead of any — that matches getSegmentKey()'s return type and avoids hiding bugs.
| constructor(private analytics?: CoreAnalytics, private cacheService?: CacheService, private writeKey?: any) { | |
| constructor(private analytics?: CoreAnalytics, private cacheService?: CacheService, private writeKey?: string) { |
| } | ||
|
|
||
| private getIdentifyCacheName(writeKey: string): string { | ||
| return `${writeKey}-identify`; |
There was a problem hiding this comment.
Two issues here:
-
undefinedfallback missing — whenwriteKeyisundefined(no segment key in package.json), this produces the string"undefined-identify". Should fall back to"identify"to preserve backward-compatible behavior. -
Redundant parameter — this private method is only ever called with
this.writeKey. It can just read the field directly.
| return `${writeKey}-identify`; | |
| private getIdentifyCacheName(): string { | |
| return this.writeKey ? `${this.writeKey}-identify` : 'identify'; | |
| } |
(And update the two call sites to this.getIdentifyCacheName() without arguments.)
|
@coderabbitai full review |
✅ Action performedFull review finished. |
📝 WalkthroughSummary by CodeRabbit
WalkthroughChangesIdentify cache namespacing
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/common/impl/reporter.ts`:
- Around line 28-29: Wrap the identify switch case in braces so the declarations
from getIdentifyCacheName and cacheService?.get are scoped locally, and include
that case’s break inside the block; leave the other switch cases unchanged.
- Line 36: Update the cache persistence call in the reporter method containing
identifyCacheName to await cacheService.put. Keep it inside report()’s existing
try/catch so write failures are handled and propagated through the established
error path before the method returns.
- Around line 28-30: Update the identify deduplication flow around
getIdentifyCacheName and the cacheService value so it stores the send date in
the namespaced cache entry rather than only the payload hash. Compare the
current date against the cached date before sending, ensuring different identify
payloads are still suppressed on the same day, and update the entry with the
current date after a successful send.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: b87fd6e0-8c7f-464b-9884-e079b60ea98e
📒 Files selected for processing (3)
src/common/impl/reporter.tssrc/node/redHatServiceNodeProvider.tssrc/webworker/redHatServiceWebWorkerProvider.ts
| const identifyCacheName = this.getIdentifyCacheName(this.writeKey); | ||
| const cached = await this.cacheService?.get(identifyCacheName); |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Scope the identify declarations to their switch case.
Biome reports noSwitchDeclarations on Lines 28-29. Wrap the identify case in braces, including its break, so its lexical declarations cannot collide with later cases.
🧰 Tools
🪛 Biome (2.5.3)
[error] 28-28: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
(lint/correctness/noSwitchDeclarations)
[error] 29-29: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
(lint/correctness/noSwitchDeclarations)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/common/impl/reporter.ts` around lines 28 - 29, Wrap the identify switch
case in braces so the declarations from getIdentifyCacheName and
cacheService?.get are scoped locally, and include that case’s break inside the
block; leave the other switch cases unchanged.
Source: Linters/SAST tools
| const identifyCacheName = this.getIdentifyCacheName(this.writeKey); | ||
| const cached = await this.cacheService?.get(identifyCacheName); | ||
| if (hash === cached) { |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Record the send date, not only the payload hash.
If two identify payloads differ on the same day, their hashes differ and both are sent. That does not enforce the stated “at most once per day” limit per Segment destination; store and check a date in the namespaced cache value.
🧰 Tools
🪛 Biome (2.5.3)
[error] 28-28: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
(lint/correctness/noSwitchDeclarations)
[error] 29-29: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.
(lint/correctness/noSwitchDeclarations)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/common/impl/reporter.ts` around lines 28 - 30, Update the identify
deduplication flow around getIdentifyCacheName and the cacheService value so it
stores the send date in the namespaced cache entry rather than only the payload
hash. Compare the current date against the cached date before sending, ensuring
different identify payloads are still suppressed on the same day, and update the
entry with the current date after a successful send.
| Logger.log(`Sending 'identify' event with\n${payloadString}`); | ||
| await this.analytics?.identify(event); | ||
| this.cacheService?.put('identify', hash); | ||
| this.cacheService?.put(identifyCacheName, hash); |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Await the cache write before returning.
CacheService.put() is asynchronous, but this call is fire-and-forget. A failed write can leave the persisted cache stale, and its rejection bypasses report()’s try/catch; use await and handle persistence failure.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/common/impl/reporter.ts` at line 36, Update the cache persistence call in
the reporter method containing identifyCacheName to await cacheService.put. Keep
it inside report()’s existing try/catch so write failures are handled and
propagated through the established error path before the method returns.
Fixes #173