Skip to content

Commit d97aa0c

Browse files
committed
fix: add an e2e test and catch errors inside getDeviceId
1 parent 7da490c commit d97aa0c

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

packages/e2e-tests/test/e2e.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,20 @@ describe('e2e', function () {
771771
).to.include('string');
772772
});
773773

774+
it('sets device ID for telemetry', async function () {
775+
const deviceId = (
776+
await shell.executeLine(
777+
'db._mongo._instanceState.evaluationListener.ioProvider.loggingAndTelemetry.deviceId'
778+
)
779+
)
780+
.replace(/test>/g, '')
781+
.trim();
782+
783+
expect(deviceId).not.to.equal('unknown');
784+
// Our hashed key is 64 hex chars
785+
expect(deviceId).to.match(/^[a-f0-9]{64}$/);
786+
});
787+
774788
context('post-4.2', function () {
775789
skipIfServerVersion(testServer, '< 4.4');
776790
it('allows calling convertShardKeyToHashed() as a global function', async function () {

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

+35-33
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,6 @@ export function setupLoggingAndTelemetry(
6464
return loggingAndTelemetry;
6565
}
6666

67-
/**
68-
* @returns A hashed, unique identifier for the running device or `undefined` if not known.
69-
*/
70-
export async function getDeviceId(): Promise<string | 'unknown'> {
71-
// Create a hashed format from the all uppercase version of the machine ID
72-
// to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
73-
const originalId: string = (
74-
await require('native-machine-id').getMachineId({ raw: true })
75-
)?.toUpperCase();
76-
77-
if (!originalId) {
78-
return 'unknown';
79-
}
80-
const hmac = createHmac('sha256', originalId);
81-
82-
/** This matches the message used to create the hashes in Atlas CLI */
83-
const DEVICE_ID_HASH_MESSAGE = 'atlascli';
84-
85-
hmac.update(DEVICE_ID_HASH_MESSAGE);
86-
return hmac.digest('hex');
87-
}
88-
8967
/** @internal */
9068
export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
9169
private static dummyLogger = new MongoLogWriter(
@@ -158,19 +136,43 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
158136
this.resolveDeviceId('unknown');
159137
}
160138

139+
/**
140+
* @returns A hashed, unique identifier for the running device or `"unknown"` if not known.
141+
*/
142+
private async getDeviceId(): Promise<string | 'unknown'> {
143+
try {
144+
// Create a hashed format from the all uppercase version of the machine ID
145+
// to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
146+
const originalId: string =
147+
// eslint-disable-next-line @typescript-eslint/no-var-requires
148+
await require('native-machine-id').getMachineId({
149+
raw: true,
150+
});
151+
152+
if (!originalId) {
153+
return 'unknown';
154+
}
155+
const hmac = createHmac('sha256', originalId);
156+
157+
/** This matches the message used to create the hashes in Atlas CLI */
158+
const DEVICE_ID_HASH_MESSAGE = 'atlascli';
159+
160+
hmac.update(DEVICE_ID_HASH_MESSAGE);
161+
return hmac.digest('hex');
162+
} catch (error) {
163+
this.bus.emit('mongosh:error', error as Error, 'telemetry');
164+
return 'unknown';
165+
}
166+
}
167+
161168
private async setupTelemetry(): Promise<void> {
162169
if (!this.deviceId) {
163-
try {
164-
this.deviceId ??= await Promise.race([
165-
getDeviceId(),
166-
new Promise<string>((resolve) => {
167-
this.resolveDeviceId = resolve;
168-
}),
169-
]);
170-
} catch (error) {
171-
this.bus.emit('mongosh:error', error as Error, 'telemetry');
172-
this.deviceId = 'unknown';
173-
}
170+
this.deviceId = await Promise.race([
171+
this.getDeviceId(),
172+
new Promise<string>((resolve) => {
173+
this.resolveDeviceId = resolve;
174+
}),
175+
]);
174176
}
175177

176178
this.runAndClearPendingTelemetryEvents();

0 commit comments

Comments
 (0)