Skip to content

Commit d77ffe7

Browse files
authored
chore(telemetry): append user anonymousId & connectionId to appName passed to server COMPASS-8591 (#982)
1 parent b58030f commit d77ffe7

File tree

5 files changed

+242
-60
lines changed

5 files changed

+242
-60
lines changed

package-lock.json

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@
13151315
"@babel/parser": "^7.25.8",
13161316
"@babel/traverse": "^7.25.7",
13171317
"@mongodb-js/compass-components": "^1.34.5",
1318-
"@mongodb-js/connection-form": "1.47.2",
1318+
"@mongodb-js/connection-form": "1.47.6",
13191319
"@mongodb-js/connection-info": "^0.11.6",
13201320
"@mongodb-js/mongodb-constants": "^0.11.0",
13211321
"@mongosh/browser-runtime-electron": "^3.8.0",

src/connectionController.ts

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import LINKS from './utils/links';
3030
import { isAtlasStream } from 'mongodb-build-info';
3131
import type { ConnectionTreeItem } from './explorer';
3232
import { PresetConnectionEditedTelemetryEvent } from './telemetry';
33+
import getBuildInfo from 'mongodb-build-info';
3334

3435
// eslint-disable-next-line @typescript-eslint/no-var-requires
3536
const packageJSON = require('../package.json');
@@ -304,13 +305,6 @@ export default class ConnectionController {
304305

305306
const connectionStringData = new ConnectionString(connectionString);
306307

307-
// TODO: Allow overriding appname + use driverInfo instead
308-
// (https://jira.mongodb.org/browse/MONGOSH-1015)
309-
connectionStringData.searchParams.set(
310-
'appname',
311-
`${packageJSON.name} ${packageJSON.version}`
312-
);
313-
314308
try {
315309
const connectResult = await this.saveNewConnectionAndConnect({
316310
connectionId: uuidv4(),
@@ -362,6 +356,33 @@ export default class ConnectionController {
362356
return this._connect(connection.id, connectionType);
363357
}
364358

359+
/**
360+
* In older versions, we'd manually store a connectionString with an appended
361+
* appName with the VSCode extension name and version. This overrides the
362+
* connection string if needed and returns true if it does so, false otherwise.
363+
*/
364+
private _overrideLegacyConnectionStringAppName(
365+
connectionId: string
366+
): boolean {
367+
const connectionString = new ConnectionString(
368+
this._connections[connectionId].connectionOptions.connectionString
369+
);
370+
371+
if (
372+
connectionString.searchParams
373+
.get('appname')
374+
?.match(/mongodb-vscode \d\.\d\.\d.*/)
375+
) {
376+
connectionString.searchParams.delete('appname');
377+
378+
this._connections[connectionId].connectionOptions.connectionString =
379+
connectionString.toString();
380+
381+
return true;
382+
}
383+
return false;
384+
}
385+
365386
// eslint-disable-next-line complexity
366387
async _connect(
367388
connectionId: string,
@@ -427,10 +448,17 @@ export default class ConnectionController {
427448

428449
const connectionOptions = adjustConnectionOptionsBeforeConnect({
429450
connectionOptions: connectionInfo.connectionOptions,
430-
defaultAppName: packageJSON.name,
451+
connectionInfo: {
452+
id: connectionId,
453+
isAtlas: getBuildInfo.isAtlas(
454+
connectionInfo.connectionOptions.connectionString
455+
),
456+
},
457+
defaultAppName: `${packageJSON.name} ${packageJSON.version}`,
431458
notifyDeviceFlow,
432459
preferences: {
433460
forceConnectionOptions: [],
461+
telemetryAnonymousId: this._connectionStorage.getUserAnonymousId(),
434462
browserCommandForOIDCAuth: undefined, // We overwrite this below.
435463
},
436464
});
@@ -620,12 +648,23 @@ export default class ConnectionController {
620648
}
621649

622650
try {
623-
await this._connect(connectionId, ConnectionTypes.CONNECTION_ID);
651+
const wasOverridden =
652+
this._overrideLegacyConnectionStringAppName(connectionId);
624653

625-
return {
626-
successfullyConnected: true,
627-
connectionErrorMessage: '',
628-
};
654+
const result = await this._connect(
655+
connectionId,
656+
ConnectionTypes.CONNECTION_ID
657+
);
658+
659+
/** After successfully connecting with an overridden connection
660+
* string, save it to storage for the future. */
661+
if (result.successfullyConnected && wasOverridden) {
662+
await this._connectionStorage.saveConnection(
663+
this._connections[connectionId]
664+
);
665+
}
666+
667+
return result;
629668
} catch (error) {
630669
log.error('Failed to connect by a connection id', error);
631670
const printableError = formatError(error);

src/storage/connectionStorage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,8 @@ export class ConnectionStorage {
324324

325325
return StorageLocation.NONE;
326326
}
327+
328+
getUserAnonymousId(): string {
329+
return this._storageController.getUserIdentity().anonymousId;
330+
}
327331
}

0 commit comments

Comments
 (0)