Skip to content

chore: switch to @mongodb-js/device-id #2446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/logging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"node": ">=14.15.1"
},
"dependencies": {
"@mongodb-js/device-id": "^0.2.0",
"@mongodb-js/devtools-connect": "^3.4.1",
"@mongosh/errors": "2.4.0",
"@mongosh/history": "2.4.6",
Expand Down
17 changes: 6 additions & 11 deletions packages/logging/src/logging-and-telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import type { Writable } from 'stream';
import type { MongoshLoggingAndTelemetry } from '.';
import { setupLoggingAndTelemetry } from '.';
import type { LoggingAndTelemetry } from './logging-and-telemetry';
import { getDeviceId } from './logging-and-telemetry';
import sinon from 'sinon';
import type { MongoshLoggingAndTelemetryArguments } from './types';
import { getDeviceId } from '@mongodb-js/device-id';
import { getMachineId } from 'native-machine-id';

describe('MongoshLoggingAndTelemetry', function () {
let logOutput: any[];
Expand Down Expand Up @@ -264,7 +265,10 @@ describe('MongoshLoggingAndTelemetry', function () {

bus.emit('mongosh:new-user', { userId, anonymousId: userId });

const deviceId = await getDeviceId();
const deviceId = await getDeviceId({
getMachineId: () => getMachineId({ raw: true }),
isNodeMachineId: false,
}).value;

await (loggingAndTelemetry as LoggingAndTelemetry).setupTelemetryPromise;

Expand Down Expand Up @@ -1202,13 +1206,4 @@ describe('MongoshLoggingAndTelemetry', function () {
],
]);
});

describe('getDeviceId()', function () {
it('is consistent on the same machine', async function () {
const idA = await getDeviceId();
const idB = await getDeviceId();

expect(idA).equals(idB);
});
});
});
54 changes: 11 additions & 43 deletions packages/logging/src/logging-and-telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,7 @@ import type {
MongoshLoggingAndTelemetryArguments,
MongoshTrackingProperties,
} from './types';
import { createHmac } from 'crypto';

/**
* @returns A hashed, unique identifier for the running device or `"unknown"` if not known.
*/
export async function getDeviceId({
onError,
}: {
onError?: (error: Error) => void;
} = {}): Promise<string | 'unknown'> {
try {
// Create a hashed format from the all uppercase version of the machine ID
// to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
const originalId: string =
// eslint-disable-next-line @typescript-eslint/no-var-requires
await require('native-machine-id').getMachineId({
raw: true,
});

if (!originalId) {
return 'unknown';
}
const hmac = createHmac('sha256', originalId);

/** This matches the message used to create the hashes in Atlas CLI */
const DEVICE_ID_HASH_MESSAGE = 'atlascli';

hmac.update(DEVICE_ID_HASH_MESSAGE);
return hmac.digest('hex');
} catch (error) {
onError?.(error as Error);
return 'unknown';
}
}
import { getDeviceId } from '@mongodb-js/device-id';

export function setupLoggingAndTelemetry(
props: MongoshLoggingAndTelemetryArguments
Expand Down Expand Up @@ -171,15 +138,16 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {

private async setupTelemetry(): Promise<void> {
if (!this.deviceId) {
this.deviceId = await Promise.race([
getDeviceId({
onError: (error) =>
this.bus.emit('mongosh:error', error, 'telemetry'),
}),
new Promise<string>((resolve) => {
this.resolveDeviceId = resolve;
}),
]);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const getMachineId = require('native-machine-id').getMachineId;
const { value: deviceId, resolve: resolveDeviceId } = getDeviceId({
getMachineId: () => getMachineId({ raw: true }),
isNodeMachineId: false,
onError: (error) => this.bus.emit('mongosh:error', error, 'telemetry'),
});

this.resolveDeviceId = resolveDeviceId;
this.deviceId = await deviceId;
}

this.runAndClearPendingTelemetryEvents();
Expand Down