@@ -30,6 +30,7 @@ import LINKS from './utils/links';
3030import { isAtlasStream } from 'mongodb-build-info' ;
3131import type { ConnectionTreeItem } from './explorer' ;
3232import { PresetConnectionEditedTelemetryEvent } from './telemetry' ;
33+ import getBuildInfo from 'mongodb-build-info' ;
3334
3435// eslint-disable-next-line @typescript-eslint/no-var-requires
3536const 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 ( / m o n g o d b - v s c o d e \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 ) ;
0 commit comments