Skip to content

feat(telemetry): add device ID logging COMPASS-8443 #2411

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

Merged
merged 17 commits into from
May 1, 2025
Merged
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
4 changes: 4 additions & 0 deletions .depalignrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"@typescript-eslint/parser": [
"^5.59.0",
"^4.33.0"
],
"node-fetch": [
"^3.3.2",
"2.6.12"
]
}
}
140 changes: 138 additions & 2 deletions package-lock.json

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

5 changes: 5 additions & 0 deletions packages/build/src/compile/signable-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export class SignableCompiler {
path: await findModulePath('cli-repl', 'glibc-version'),
requireRegexp: /\bglibc_version\.node$/,
};
const nativeMachineIdAddon = {
path: await findModulePath('logging', 'native-machine-id'),
requireRegexp: /\bnative_machine_id\.node$/,
};
// Warning! Until https://jira.mongodb.org/browse/MONGOSH-990,
// packages/service-provider-node-driver *also* has a copy of these.
// We use the versions included in packages/cli-repl here, so these
Expand Down Expand Up @@ -186,6 +190,7 @@ export class SignableCompiler {
kerberosAddon,
cryptLibraryVersionAddon,
glibcVersionAddon,
nativeMachineIdAddon,
]
.concat(winCAAddon ? [winCAAddon] : [])
.concat(winConsoleProcessListAddon ? [winConsoleProcessListAddon] : [])
Expand Down
4 changes: 4 additions & 0 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,8 @@ export class CliRepl implements MongoshIOProvider {
const analytics = this.toggleableAnalytics;
let flushError: string | null = null;
let flushDuration: number | null = null;
this.loggingAndTelemetry?.flush();

if (analytics) {
const flushStart = Date.now();
try {
Expand All @@ -1194,6 +1196,7 @@ export class CliRepl implements MongoshIOProvider {
}
);
await this.logWriter?.flush();

markTime(TimingCategories.Logging, 'flushed log writer');
this.bus.emit('mongosh:closed');
})());
Expand All @@ -1211,6 +1214,7 @@ export class CliRepl implements MongoshIOProvider {
// onExit never returns. If it does, that's a bug.
const error = new MongoshInternalError('onExit() unexpectedly returned');
this.bus.emit('mongosh:error', error, 'fatal');

throw error;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/connectivity-tests/test/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fi
git clone https://github.com/mongodb-js/devtools-docker-test-envs.git test-envs
cd test-envs

git checkout ac8e6675fcd769f0bfc868c3ac73cdd8bcdcc792
git checkout cbec3a65efe9def50638994ae6b36ffa3b4708ee

"$CONNECTIVITY_TEST_SOURCE_DIR/ldap.sh"
"$CONNECTIVITY_TEST_SOURCE_DIR/localhost.sh"
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/test/e2e-snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('e2e snapshot support', function () {
);
verifyAllInCategoryMatch(
'nodb-eval',
/^node_modules\/(kerberos|mongodb-client-encryption|glibc-version|@mongodb-js\/devtools-proxy-support|@mongodb-js\/socksv5|agent-base|(win|macos)-export-certificate-and-key|@tootallnate\/quickjs-emscripten)\//
/^node_modules\/(kerberos|native-machine-id|mongodb-client-encryption|glibc-version|@mongodb-js\/devtools-proxy-support|@mongodb-js\/socksv5|agent-base|(win|macos)-export-certificate-and-key|@tootallnate\/quickjs-emscripten)\//
);
if (process.arch !== 's390x') {
// quickjs is in the list above but should be exlucded anywhere but on s390x
Expand Down
19 changes: 19 additions & 0 deletions packages/e2e-tests/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,25 @@ describe('e2e', function () {
).to.include('string');
});

it('sets device ID for telemetry', async function () {
const deviceId = (
await shell.executeLine(
'db._mongo._instanceState.evaluationListener.ioProvider.loggingAndTelemetry.deviceId'
)
)
.split('\n')[0]
// Remove all whitespace
.replace(/\s+/g, '');

expect(deviceId).not.to.equal('unknown');
// Our hashed key is 64 hex chars

expect(deviceId).to.match(
/^[a-f0-9]{64}$/,
`deviceId did not match: |${deviceId}|`
);
});

context('post-4.2', function () {
skipIfServerVersion(testServer, '< 4.4');
it('allows calling convertShardKeyToHashed() as a global function', async function () {
Expand Down
6 changes: 4 additions & 2 deletions packages/logging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@
"@mongosh/history": "2.4.6",
"@mongosh/types": "3.6.0",
"mongodb-log-writer": "^2.3.1",
"mongodb-redact": "^1.1.5"
"mongodb-redact": "^1.1.5",
"native-machine-id": "^0.1.1"
},
"devDependencies": {
"@mongodb-js/eslint-config-mongosh": "^1.0.0",
"@mongodb-js/prettier-config-devtools": "^1.0.1",
"@mongodb-js/tsconfig-mongosh": "^1.0.0",
"depcheck": "^1.4.7",
"eslint": "^7.25.0",
"prettier": "^2.8.8"
"prettier": "^2.8.8",
"sinon": "^19.0.4"
},
"scripts": {
"test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 15000 -r ts-node/register --reporter \"../../configs/mocha-config-mongosh/reporter.ts\" \"./src/**/*.spec.ts\"",
Expand Down
4 changes: 3 additions & 1 deletion packages/logging/src/analytics-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import path from 'path';

export type MongoshAnalyticsIdentity =
| {
deviceId?: string;
userId: string;
anonymousId?: never;
}
| {
deviceId?: string;
userId?: never;
anonymousId: string;
};
Expand All @@ -16,7 +18,7 @@ export type AnalyticsIdentifyMessage = MongoshAnalyticsIdentity & {
timestamp?: Date;
};

type AnalyticsTrackMessage = MongoshAnalyticsIdentity & {
export type AnalyticsTrackMessage = MongoshAnalyticsIdentity & {
event: string;
properties: {
mongosh_version: string;
Expand Down
Loading