Skip to content

Commit 477df76

Browse files
authored
feat(telemetry): add device ID logging COMPASS-8443 (#2411)
Also includes a drive-by fix which adjusts the connectivity commit.
1 parent f3ff07c commit 477df76

File tree

12 files changed

+527
-89
lines changed

12 files changed

+527
-89
lines changed

.depalignrc.json

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"@typescript-eslint/parser": [
1919
"^5.59.0",
2020
"^4.33.0"
21+
],
22+
"node-fetch": [
23+
"^3.3.2",
24+
"2.6.12"
2125
]
2226
}
2327
}

package-lock.json

+138-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/build/src/compile/signable-compiler.ts

+5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export class SignableCompiler {
132132
path: await findModulePath('cli-repl', 'glibc-version'),
133133
requireRegexp: /\bglibc_version\.node$/,
134134
};
135+
const nativeMachineIdAddon = {
136+
path: await findModulePath('logging', 'native-machine-id'),
137+
requireRegexp: /\bnative_machine_id\.node$/,
138+
};
135139
// Warning! Until https://jira.mongodb.org/browse/MONGOSH-990,
136140
// packages/service-provider-node-driver *also* has a copy of these.
137141
// We use the versions included in packages/cli-repl here, so these
@@ -186,6 +190,7 @@ export class SignableCompiler {
186190
kerberosAddon,
187191
cryptLibraryVersionAddon,
188192
glibcVersionAddon,
193+
nativeMachineIdAddon,
189194
]
190195
.concat(winCAAddon ? [winCAAddon] : [])
191196
.concat(winConsoleProcessListAddon ? [winConsoleProcessListAddon] : [])

packages/cli-repl/src/cli-repl.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,8 @@ export class CliRepl implements MongoshIOProvider {
11721172
const analytics = this.toggleableAnalytics;
11731173
let flushError: string | null = null;
11741174
let flushDuration: number | null = null;
1175+
this.loggingAndTelemetry?.flush();
1176+
11751177
if (analytics) {
11761178
const flushStart = Date.now();
11771179
try {
@@ -1194,6 +1196,7 @@ export class CliRepl implements MongoshIOProvider {
11941196
}
11951197
);
11961198
await this.logWriter?.flush();
1199+
11971200
markTime(TimingCategories.Logging, 'flushed log writer');
11981201
this.bus.emit('mongosh:closed');
11991202
})());
@@ -1211,6 +1214,7 @@ export class CliRepl implements MongoshIOProvider {
12111214
// onExit never returns. If it does, that's a bug.
12121215
const error = new MongoshInternalError('onExit() unexpectedly returned');
12131216
this.bus.emit('mongosh:error', error, 'fatal');
1217+
12141218
throw error;
12151219
}
12161220

packages/connectivity-tests/test/all.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fi
2020
git clone https://github.com/mongodb-js/devtools-docker-test-envs.git test-envs
2121
cd test-envs
2222

23-
git checkout ac8e6675fcd769f0bfc868c3ac73cdd8bcdcc792
23+
git checkout cbec3a65efe9def50638994ae6b36ffa3b4708ee
2424

2525
"$CONNECTIVITY_TEST_SOURCE_DIR/ldap.sh"
2626
"$CONNECTIVITY_TEST_SOURCE_DIR/localhost.sh"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe('e2e snapshot support', function () {
144144
);
145145
verifyAllInCategoryMatch(
146146
'nodb-eval',
147-
/^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)\//
147+
/^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)\//
148148
);
149149
if (process.arch !== 's390x') {
150150
// quickjs is in the list above but should be exlucded anywhere but on s390x

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

+19
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,25 @@ 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+
.split('\n')[0]
781+
// Remove all whitespace
782+
.replace(/\s+/g, '');
783+
784+
expect(deviceId).not.to.equal('unknown');
785+
// Our hashed key is 64 hex chars
786+
787+
expect(deviceId).to.match(
788+
/^[a-f0-9]{64}$/,
789+
`deviceId did not match: |${deviceId}|`
790+
);
791+
});
792+
774793
context('post-4.2', function () {
775794
skipIfServerVersion(testServer, '< 4.4');
776795
it('allows calling convertShardKeyToHashed() as a global function', async function () {

packages/logging/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
"@mongosh/history": "2.4.6",
2323
"@mongosh/types": "3.6.0",
2424
"mongodb-log-writer": "^2.3.1",
25-
"mongodb-redact": "^1.1.5"
25+
"mongodb-redact": "^1.1.5",
26+
"native-machine-id": "^0.1.1"
2627
},
2728
"devDependencies": {
2829
"@mongodb-js/eslint-config-mongosh": "^1.0.0",
2930
"@mongodb-js/prettier-config-devtools": "^1.0.1",
3031
"@mongodb-js/tsconfig-mongosh": "^1.0.0",
3132
"depcheck": "^1.4.7",
3233
"eslint": "^7.25.0",
33-
"prettier": "^2.8.8"
34+
"prettier": "^2.8.8",
35+
"sinon": "^19.0.4"
3436
},
3537
"scripts": {
3638
"test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 15000 -r ts-node/register --reporter \"../../configs/mocha-config-mongosh/reporter.ts\" \"./src/**/*.spec.ts\"",

packages/logging/src/analytics-helpers.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import path from 'path';
33

44
export type MongoshAnalyticsIdentity =
55
| {
6+
deviceId?: string;
67
userId: string;
78
anonymousId?: never;
89
}
910
| {
11+
deviceId?: string;
1012
userId?: never;
1113
anonymousId: string;
1214
};
@@ -16,7 +18,7 @@ export type AnalyticsIdentifyMessage = MongoshAnalyticsIdentity & {
1618
timestamp?: Date;
1719
};
1820

19-
type AnalyticsTrackMessage = MongoshAnalyticsIdentity & {
21+
export type AnalyticsTrackMessage = MongoshAnalyticsIdentity & {
2022
event: string;
2123
properties: {
2224
mongosh_version: string;

0 commit comments

Comments
 (0)