Skip to content

Commit 30194a5

Browse files
committed
add logs, fix a race condition with event sending
1 parent 724add9 commit 30194a5

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/common/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const LogId = {
3535
telemetryMetadataError: mongoLogId(1_002_005),
3636
deviceIdResolutionError: mongoLogId(1_002_006),
3737
deviceIdTimeout: mongoLogId(1_002_007),
38+
telemetryClose: mongoLogId(1_002_008),
3839

3940
toolExecute: mongoLogId(1_003_001),
4041
toolExecuteFailure: mongoLogId(1_003_002),

src/telemetry/eventCache.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@ export class EventCache {
3434
}
3535

3636
/**
37-
* Gets a copy of the currently cached events
37+
* Gets the number of currently cached events
38+
*/
39+
public get size(): number {
40+
return this.cache.size;
41+
}
42+
43+
/**
44+
* Gets a copy of the currently cached events along with their ids
3845
* @returns Array of cached BaseEvent objects
3946
*/
40-
public getEvents(): BaseEvent[] {
41-
return Array.from(this.cache.values());
47+
public getEvents(): { id: number; event: BaseEvent }[] {
48+
return Array.from(this.cache.entries()).map(([id, event]) => ({ id, event }));
4249
}
4350

4451
/**
@@ -53,10 +60,11 @@ export class EventCache {
5360
}
5461

5562
/**
56-
* Clears all cached events
63+
* Removes cached events by their ids
5764
*/
58-
public clearEvents(): void {
59-
this.cache.clear();
60-
this.nextId = 0;
65+
public removeEvents(ids: number[]): void {
66+
for (const id of ids) {
67+
this.cache.delete(id);
68+
}
6169
}
6270
}

src/telemetry/telemetry.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,27 @@ export class Telemetry {
8787
public async close(): Promise<void> {
8888
this.isBufferingEvents = false;
8989

90+
this.session.logger.debug({
91+
id: LogId.telemetryClose,
92+
message: `Closing telemetry and flushing ${this.eventCache.size} events`,
93+
context: "telemetry",
94+
});
95+
9096
// Wait up to 5 seconds for events to be sent before closing, but don't throw if it times out
91-
await Promise.race([new Promise((resolve) => setTimeout(resolve, 5000)), this.emit([])]);
97+
const flushTimeout = 5000;
98+
await Promise.race([
99+
new Promise<void>((resolve) =>
100+
setTimeout(() => {
101+
this.session.logger.debug({
102+
id: LogId.telemetryClose,
103+
message: `Failed to flush remaining events within ${flushTimeout}ms timeout`,
104+
context: "telemetry",
105+
});
106+
resolve();
107+
}, flushTimeout)
108+
),
109+
this.emit([]),
110+
]);
92111
}
93112

94113
/**
@@ -151,7 +170,7 @@ export class Telemetry {
151170

152171
try {
153172
const cachedEvents = this.eventCache.getEvents();
154-
const allEvents = [...cachedEvents, ...events];
173+
const allEvents = [...cachedEvents.map((e) => e.event), ...events];
155174

156175
this.session.logger.debug({
157176
id: LogId.telemetryEmitStart,
@@ -161,11 +180,11 @@ export class Telemetry {
161180

162181
const result = await this.sendEvents(this.session.apiClient, allEvents);
163182
if (result.success) {
164-
this.eventCache.clearEvents();
183+
this.eventCache.removeEvents(cachedEvents.map((e) => e.id));
165184
this.session.logger.debug({
166185
id: LogId.telemetryEmitSuccess,
167186
context: "telemetry",
168-
message: `Sent ${allEvents.length} events successfully: ${JSON.stringify(allEvents, null, 2)}`,
187+
message: `Sent ${allEvents.length} events successfully: ${JSON.stringify(allEvents)}`,
169188
});
170189
this.events.emit("events-emitted");
171190
return;

0 commit comments

Comments
 (0)