-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add and wire OverflowDetection, report a metric only for now
- Loading branch information
Showing
6 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
plugin-server/src/main/ingestion-queues/session-recording/services/overflow-detection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import LRUCache from 'lru-cache' | ||
import { Gauge } from 'prom-client' | ||
|
||
import { Limiter } from '../../../../utils/token-bucket' | ||
|
||
export enum OverflowState { | ||
Okay, | ||
Triggered, // Recently triggered the overflow detection | ||
Cooldown, // Already triggered the overflow detection earlier than cooldownSeconds | ||
} | ||
|
||
export const overflowTriggeredGauge = new Gauge({ | ||
name: 'overflow_detection_triggered_total', | ||
help: 'Number of entities that triggered overflow detection.', | ||
}) | ||
|
||
/** | ||
* OverflowDetection handles consumer-side detection of hot partitions by | ||
* accounting for data volumes per entity (a session_id, a distinct_id...). | ||
* | ||
* The first time that the observed spike crosses the thresholds set via burstCapacity | ||
* and replenishRate, observe returns Triggered. Subsequent calls will return Cooldown | ||
* until cooldownSeconds is reached. | ||
*/ | ||
export class OverflowDetection { | ||
private limiter: Limiter | ||
private triggered: LRUCache<string, boolean> | ||
|
||
constructor(burstCapacity: number, replenishRate: number, cooldownSeconds: number) { | ||
this.limiter = new Limiter(burstCapacity, replenishRate) | ||
this.triggered = new LRUCache({ max: 1_000_000, maxAge: cooldownSeconds * 1000 }) | ||
} | ||
|
||
public observe(key: string, quantity: number, now?: number): OverflowState { | ||
if (this.triggered.has(key)) { | ||
return OverflowState.Cooldown | ||
} | ||
if (this.limiter.consume(key, quantity, now)) { | ||
return OverflowState.Okay | ||
} | ||
this.triggered.set(key, true) | ||
overflowTriggeredGauge.inc(1) | ||
return OverflowState.Triggered | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters