8000 parallel and test · mongodb-js/mongodb-mcp-server@1dba800 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1dba800

Browse files
committed
parallel and test
1 parent 814ccb9 commit 1dba800

File tree

2 files changed

+66
-37
lines changed

src/telemetry/telemetry.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,38 +106,39 @@ export class Telemetry {
106106
private async getCommonProperties(): Promise<CommonProperties> {
107107
if (!this.cachedCommonProperties) {
108108
let deviceId: string | undefined;
109-
try {
110-
deviceId = await getDeviceId({
111-
getMachineId: () => this.getRawMachineId(),
112-
onError: (reason, error) => {
113-
switch (reason) {
114-
case "resolutionError":
115-
logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", String(error));
116-
break;
117-
case "timeout":
118-
logger.debug(
119-
LogId.telemetryDeviceIdTimeout,
120-
"telemetry",
121-
"Device ID retrieval timed out"
122-
);
123-
break;
124-
case "abort":
125-
// No need to log in the case of aborts
126-
break;
127-
}
128-
},
129-
abortSignal: this. 10000 deviceIdAbortController.signal,
130-
});
131-
} catch (error: unknown) {
132-
const err = error instanceof Error ? error : new Error(String(error));
133-
logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", err.message);
134-
}
135109
let containerEnv: boolean | undefined;
136110
try {
137-
containerEnv = await this.getContainerEnv();
111+
await Promise.all([
112+
getDeviceId({
113+
getMachineId: () => this.getRawMachineId(),
114+
onError: (reason, error) => {
115+
switch (reason) {
116+
case "resolutionError":
117+
logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", String(error));
118+
break;
119+
case "timeout":
120+
logger.debug(
121+
LogId.telemetryDeviceIdTimeout,
122+
"telemetry",
123+
"Device ID retrieval timed out"
124+
);
125+
break;
126+
case "abort":
127+
// No need to log in the case of aborts
128+
break;
129+
}
130+
},
131+
abortSignal: this.deviceIdAbortController.signal,
132+
}).then((id) => {
133+
deviceId = id;
134+
}),
135+
this.getContainerEnv().then((env) => {
136+
containerEnv = env;
137+
}),
138+
]);
138139
} catch (error: unknown) {
139140
const err = error instanceof Error ? error : new Error(String(error));
140-
logger.debug(LogId.telemetryContainerEnvFailure, "telemetry", err.message);
141+
logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", err.message);
141142
}
142143
this.cachedCommonProperties = {
143144
...MACHINE_METADATA,
@@ -183,6 +184,9 @@ export class Telemetry {
183184

184185
if (this.flushing) {
185186
this.eventCache.appendEvents(events ?? []);
187+
process.nextTick(() => { // try again if in the middle of a flush
188+
this.flush();
189+
});
186190
return;
187191
}
188192

@@ -191,6 +195,10 @@ export class Telemetry {
191195
try {
192196
const cachedEvents = this.eventCache.getEvents();
193197
const allEvents = [...cachedEvents, ...(events ?? [])];
198+
if (allEvents.length <= 0) {
199+
this.flushing = false;
200+
return;
201+
}
194202

195203
logger.debug(
196204
LogId.telemetryEmitStart,
@@ -212,6 +220,9 @@ export class Telemetry {
212220
`Error sending event to client: ${error instanceof Error ? error.message : String(error)}`
213221
);
214222
this.eventCache.appendEvents(events ?? []);
223+
process.nextTick(() => { // try again
224+
this.flush();
225+
});
215226
}
216227

217228
this.flushing = false;

tests/unit/telemetry.test.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,11 @@ describe("Telemetry", () => {
8484
expect(appendEvents.length).toBe(appendEventsCalls);
8585

8686
if (sendEventsCalledWith) {
87-
expect(sendEvents[0]?.[0]).toMatchObject(
88-
sendEventsCalledWith.map((event) => ({
89-
...event,
90-
properties: {
91-
...event.properties,
92-
},
93-
}))
94-
);
87+
expect(sendEvents[0]?.[0]).toMatchObject(sendEventsCalledWith);
9588
}
9689

9790
if (appendEventsCalledWith) {
98-
expect(appendEvents[0]?.[0]).toEqual(appendEventsCalledWith);
91+
expect(appendEvents[0]?.[0]).toMatchObject(appendEventsCalledWith);
9992
}
10093
}
10194

@@ -227,6 +220,31 @@ describe("Telemetry", () => {
227220
});
228221
});
229222

223+
it("should send cache new event while sending another event", async () => {
224+
const newEvent = createTestEvent({
225+
command: "new-command",
226+
component: "new-component",
227+
});
228+
229+
const newEvent2 = createTestEvent({
230+
command: "new-command-2",
231+
component: "new-component-2",
232+
});
233+
234+
telemetry.emitEvents([newEvent]);
235+
telemetry.emitEvents([newEvent2]);
236+
237+
await nextTick(); // wait for the event to be sent
238+
239+
verifyMockCalls({
240+
sendEventsCalls: 1,
241+
clearEventsCalls: 1,
242+
appendEventsCalls: 1,
243+
sendEventsCalledWith: [newEvent],
244+
appendEventsCalledWith: [newEvent2],
245+
});
246+
});
247+
230248
describe("machine ID resolution", () => {
231249
it("should successfully resolve the machine ID", async () => {
232250
const testEvent = createTestEvent();

0 commit comments

Comments
 (0)
0