Skip to content

Commit f9a46f9

Browse files
committed
chore: fix lint and tests
1 parent 807109d commit f9a46f9

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/telemetry/eventCache.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { BaseEvent } from "./types.js";
2+
3+
/**
4+
* Singleton class for in-memory telemetry event caching
5+
* Provides a central storage for telemetry events that couldn't be sent
6+
*/
7+
export class EventCache {
8+
private static instance: EventCache;
9+
private events: BaseEvent[] = [];
10+
11+
private constructor() {}
12+
13+
/**
14+
* Gets the singleton instance of EventCache
15+
* @returns The EventCache instance
16+
*/
17+
public static getInstance(): EventCache {
18+
if (!EventCache.instance) {
19+
EventCache.instance = new EventCache();
20+
}
21+
return EventCache.instance;
22+
}
23+
24+
/**
25+
* Gets a copy of the currently cached events
26+
* @returns Array of cached BaseEvent objects
27+
*/
28+
public getEvents(): BaseEvent[] {
29+
return [...this.events];
30+
}
31+
32+
/**
33+
* Sets the cached events, replacing any existing events
34+
* @param events - The events to cache
35+
*/
36+
public setEvents(events: BaseEvent[]): void {
37+
this.events = [...events];
38+
}
39+
40+
/**
41+
* Clears all cached events
42+
*/
43+
public clearEvents(): void {
44+
this.events = [];
45+
}
46+
}

src/telemetry/telemetry.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class Telemetry {
9191
* Falls back to caching if both attempts fail
9292
*/
9393
private async emit(events: BaseEvent[]): Promise<void> {
94-
const cachedEvents = await this.readCache();
94+
const cachedEvents = this.readCache();
9595
const allEvents = [...cachedEvents, ...events];
9696

9797
logger.debug(
@@ -102,13 +102,13 @@ export class Telemetry {
102102

103103
const result = await this.sendEvents(this.session.apiClient, allEvents);
104104
if (result.success) {
105-
await this.clearCache();
105+
this.clearCache();
106106
logger.debug(mongoLogId(1_000_000), "telemetry", `Sent ${allEvents.length} events successfully`);
107107
return;
108108
}
109109

110110
logger.warning(mongoLogId(1_000_000), "telemetry", `Error sending event to client: ${result.error}`);
111-
await this.cacheEvents(allEvents);
111+
this.cacheEvents(allEvents);
112112
}
113113

114114
/**
@@ -130,7 +130,7 @@ export class Telemetry {
130130
* Reads cached events from memory
131131
* Returns empty array if no cache exists
132132
*/
133-
private async readCache(): Promise<BaseEvent[]> {
133+
private readCache(): BaseEvent[] {
134134
try {
135135
return EventCache.getInstance().getEvents();
136136
} catch (error) {
@@ -146,7 +146,7 @@ export class Telemetry {
146146
/**
147147
* Caches events in memory for later sending
148148
*/
149-
private async cacheEvents(events: BaseEvent[]): Promise<void> {
149+
private cacheEvents(events: BaseEvent[]): void {
150150
try {
151151
EventCache.getInstance().setEvents(events);
152152
logger.debug(
@@ -166,7 +166,7 @@ export class Telemetry {
166166
/**
167167
* Clears the event cache after successful sending
168168
*/
169-
private async clearCache(): Promise<void> {
169+
private clearCache(): void {
170170
try {
171171
EventCache.getInstance().clearEvents();
172172
logger.debug(mongoLogId(1_000_000), "telemetry", "In-memory telemetry cache cleared");

0 commit comments

Comments
 (0)