Skip to content

Commit 00b572c

Browse files
committed
fix: update to latest structure
1 parent 0eb06be commit 00b572c

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

packages/logging/src/logging-and-telemetry.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,50 @@ describe('MongoshLoggingAndTelemetry', function () {
289289
]);
290290
});
291291

292+
it('resolves device ID setup when flushed', async function () {
293+
const loggingAndTelemetry = setupLoggingAndTelemetry({
294+
...testLoggingArguments,
295+
bus,
296+
deviceId: undefined,
297+
});
298+
sinon
299+
// eslint-disable-next-line @typescript-eslint/no-var-requires
300+
.stub(require('native-machine-id'), 'getMachineId')
301+
.resolves(
302+
new Promise((resolve) => setTimeout(resolve, 10_000).unref())
303+
);
304+
305+
loggingAndTelemetry.attachLogger(logger);
306+
307+
// Start the device ID setup
308+
const setupPromise = (loggingAndTelemetry as LoggingAndTelemetry)
309+
.setupTelemetryPromise;
310+
311+
// Flush before it completes
312+
loggingAndTelemetry.flush();
313+
314+
// Emit an event that would trigger analytics
315+
bus.emit('mongosh:new-user', { userId, anonymousId: userId });
316+
317+
await setupPromise;
318+
319+
// Should still identify but with unknown device ID
320+
expect(analyticsOutput).deep.equal([
321+
[
322+
'identify',
323+
{
324+
deviceId: 'unknown',
325+
anonymousId: userId,
326+
traits: {
327+
platform: process.platform,
328+
arch: process.arch,
329+
session_id: logId,
330+
},
331+
},
332+
],
333+
]);
334+
});
335+
292336
it('only delays analytic outputs, not logging', async function () {
293337
// eslint-disable-next-line @typescript-eslint/no-empty-function
294338
let resolveTelemetry: (value: unknown) => void = () => {};

packages/logging/src/logging-and-telemetry.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
9292
private isBufferingTelemetryEvents = false;
9393

9494
private deviceId: string | undefined;
95-
/** @internal */
95+
96+
/** @internal Used for awaiting the telemetry setup in tests. */
9697
public setupTelemetryPromise: Promise<void> = Promise.resolve();
9798

98-
private readonly abortController: AbortController = new AbortController();
99+
private readonly telemetrySetup: AbortController = new AbortController();
99100

100101
constructor({
101102
bus,
@@ -126,13 +127,12 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
126127
}
127128

128129
public flush(): void {
129-
// Run any telemetry events even if device ID hasn't been resolved yet
130-
this.runAndClearPendingTelemetryEvents();
131-
132130
// Run any other pending events with the set or dummy log for telemetry purposes.
133131
this.runAndClearPendingBusEvents();
134132

135-
this.abortController.abort();
133+
// Abort setup, which will cause the device ID to be set to 'unknown'
134+
// and run any remaining telemetry events
135+
this.telemetrySetup.abort();
136136
}
137137

138138
private async setupTelemetry(): Promise<void> {
@@ -143,7 +143,7 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
143143
getMachineId: () => getMachineId({ raw: true }),
144144
onError: (_, error) =>
145145
this.bus.emit('mongosh:error', error, 'telemetry'),
146-
abortSignal: this.abortController.signal,
146+
abortSignal: this.telemetrySetup.signal,
147147
});
148148
}
149149

0 commit comments

Comments
 (0)