Skip to content

Commit 711587b

Browse files
chore: adds tracking for loading of saved connections along with few relevant parameters - VSCODE-446 (#565)
Co-authored-by: Alena Khineika <[email protected]>
1 parent 3593f6c commit 711587b

File tree

4 files changed

+187
-20
lines changed

4 files changed

+187
-20
lines changed

src/connectionController.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import {
2626
} from './storage/storageController';
2727
import { StorageController, StorageVariables } from './storage';
2828
import { StatusView } from './views';
29-
import TelemetryService, {
30-
TelemetryEventTypes,
31-
} from './telemetry/telemetryService';
29+
import TelemetryService from './telemetry/telemetryService';
3230
import LINKS from './utils/links';
3331
// eslint-disable-next-line @typescript-eslint/no-var-requires
3432
const packageJSON = require('../package.json');
@@ -202,23 +200,36 @@ export default class ConnectionController {
202200
)
203201
).filter((conn): conn is FailedMigrationConnectionDescriptor => !!conn);
204202

205-
if (Object.keys(this._connections).length) {
203+
const loadedConnections = Object.values(this._connections);
204+
if (loadedConnections.length) {
206205
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
207206
}
208207

208+
this._telemetryService.trackSavedConnectionsLoaded({
209+
saved_connections: globalAndWorkspaceConnections.length,
210+
loaded_connections: loadedConnections.length,
211+
connections_with_secrets_in_keytar: loadedConnections.filter(
212+
(connection) =>
213+
connection.secretStorageLocation === SecretStorageLocation.Keytar
214+
).length,
215+
connections_with_secrets_in_secret_storage: loadedConnections.filter(
216+
(connection) =>
217+
connection.secretStorageLocation ===
218+
SecretStorageLocation.SecretStorage
219+
).length,
220+
});
221+
209222
if (connectionsThatDidNotMigrate.length) {
210223
log.error(
211224
`Could not migrate secrets for ${connectionsThatDidNotMigrate.length} connections`,
212225
connectionsThatDidNotMigrate
213226
);
214-
this._telemetryService.track(
215-
TelemetryEventTypes.KEYTAR_SECRETS_MIGRATION_FAILED,
216-
{
217-
totalConnections: globalAndWorkspaceConnections.length,
218-
connectionsWithFailedKeytarMigration:
219-
connectionsThatDidNotMigrate.length,
220-
}
221-
);
227+
this._telemetryService.trackKeytarSecretsMigrationFailed({
228+
saved_connections: globalAndWorkspaceConnections.length,
229+
loaded_connections: loadedConnections.length,
230+
connections_with_failed_keytar_migration:
231+
connectionsThatDidNotMigrate.length,
232+
});
222233
void vscode.window.showInformationMessage(
223234
keytarMigrationFailedMessage(connectionsThatDidNotMigrate.length)
224235
);

src/telemetry/telemetryService.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,21 @@ type PlaygroundLoadedTelemetryEventProperties = {
6969
};
7070

7171
type KeytarSecretsMigrationFailedProperties = {
72-
totalConnections: number;
73-
connectionsWithFailedKeytarMigration: number;
72+
saved_connections: number;
73+
loaded_connections: number;
74+
connections_with_failed_keytar_migration: number;
75+
};
76+
77+
type SavedConnectionsLoadedProperties = {
78+
// Total number of connections saved on disk
79+
saved_connections: number;
80+
// Total number of connections that extension was able to load, it might
81+
// differ from saved_connections since there might be failures in loading
82+
// secrets for a connection in which case we don't list the connections in the
83+
// list of loaded connections.
84+
loaded_connections: number;
85+
connections_with_secrets_in_keytar: number;
86+
connections_with_secrets_in_secret_storage: number;
7487
};
7588

7689
export type TelemetryEventProperties =
@@ -84,7 +97,8 @@ export type TelemetryEventProperties =
8497
| PlaygroundCreatedTelemetryEventProperties
8598
| PlaygroundSavedTelemetryEventProperties
8699
| PlaygroundLoadedTelemetryEventProperties
87-
| KeytarSecretsMigrationFailedProperties;
100+
| KeytarSecretsMigrationFailedProperties
101+
| SavedConnectionsLoadedProperties;
88102

89103
export enum TelemetryEventTypes {
90104
PLAYGROUND_CODE_EXECUTED = 'Playground Code Executed',
@@ -99,6 +113,7 @@ export enum TelemetryEventTypes {
99113
AGGREGATION_EXPORTED = 'Aggregation Exported',
100114
PLAYGROUND_CREATED = 'Playground Created',
101115
KEYTAR_SECRETS_MIGRATION_FAILED = 'Keytar Secrets Migration Failed',
116+
SAVED_CONNECTIONS_LOADED = 'Saved Connections Loaded',
102117
}
103118

104119
/**
@@ -341,4 +356,22 @@ export default class TelemetryService {
341356
playground_type: playgroundType,
342357
});
343358
}
359+
360+
trackSavedConnectionsLoaded(
361+
savedConnectionsLoadedProps: SavedConnectionsLoadedProperties
362+
) {
363+
this.track(
364+
TelemetryEventTypes.SAVED_CONNECTIONS_LOADED,
365+
savedConnectionsLoadedProps
366+
);
367+
}
368+
369+
trackKeytarSecretsMigrationFailed(
370+
keytarSecretsMigrationFailedProps: KeytarSecretsMigrationFailedProperties
371+
) {
372+
this.track(
373+
TelemetryEventTypes.KEYTAR_SECRETS_MIGRATION_FAILED,
374+
keytarSecretsMigrationFailedProps
375+
);
376+
}
344377
}

src/test/suite/connectionController.test.ts

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,10 @@ suite('Connection Controller Test Suite', function () {
16791679
secretStorageLocation: SecretStorageLocation.Keytar,
16801680
} as any)
16811681
);
1682-
const trackStub = testSandbox.stub(testTelemetryService, 'track');
1682+
const trackStub = testSandbox.stub(
1683+
testTelemetryService,
1684+
'trackKeytarSecretsMigrationFailed'
1685+
);
16831686

16841687
// Clear any connections and load so we get our stubbed connections from above.
16851688
testConnectionController.clearAllConnections();
@@ -1694,8 +1697,11 @@ suite('Connection Controller Test Suite', function () {
16941697
// Tracked
16951698
assert.strictEqual(trackStub.calledOnce, true);
16961699
assert.deepStrictEqual(trackStub.lastCall.args, [
1697-
'Keytar Secrets Migration Failed',
1698-
{ totalConnections: 2, connectionsWithFailedKeytarMigration: 1 },
1700+
{
1701+
saved_connections: 2,
1702+
loaded_connections: 2,
1703+
connections_with_failed_keytar_migration: 1,
1704+
},
16991705
]);
17001706
});
17011707

@@ -1740,7 +1746,10 @@ suite('Connection Controller Test Suite', function () {
17401746
secretStorageLocation: SecretStorageLocation.Keytar,
17411747
} as any)
17421748
);
1743-
const trackStub = testSandbox.stub(testTelemetryService, 'track');
1749+
const trackStub = testSandbox.stub(
1750+
testTelemetryService,
1751+
'trackKeytarSecretsMigrationFailed'
1752+
);
17441753

17451754
// Clear any connections and load so we get our stubbed connections from above.
17461755
testConnectionController.clearAllConnections();
@@ -1749,8 +1758,78 @@ suite('Connection Controller Test Suite', function () {
17491758
// No notification sent to the user
17501759
assert.strictEqual(showInformationMessageStub.notCalled, true);
17511760

1752-
// No tracking done
1761+
// Tracks only the saved connections laoded event
17531762
assert.strictEqual(trackStub.notCalled, true);
17541763
});
1764+
1765+
test('should track SAVED_CONNECTIONS_LOADED event on load of saved connections', async () => {
1766+
testSandbox.replace(testStorageController, 'get', (key, storage) => {
1767+
if (
1768+
storage === StorageLocation.WORKSPACE ||
1769+
key === StorageVariables.WORKSPACE_SAVED_CONNECTIONS
1770+
) {
1771+
return {};
1772+
}
1773+
1774+
return {
1775+
'random-connection-1': {
1776+
id: 'random-connection-1',
1777+
name: 'localhost:27017',
1778+
storageLocation: 'GLOBAL',
1779+
secretStorageLocation: SecretStorageLocation.SecretStorage,
1780+
connectionOptions: {
1781+
connectionString:
1782+
'mongodb://localhost:27017/?readPreference=primary&ssl=false',
1783+
},
1784+
},
1785+
'random-connection-2': {
1786+
id: 'random-connection-2',
1787+
name: 'localhost:27018',
1788+
storageLocation: 'GLOBAL',
1789+
secretStorageLocation: SecretStorageLocation.SecretStorage,
1790+
connectionOptions: {
1791+
connectionString:
1792+
'mongodb://localhost:27018/?readPreference=primary&ssl=false',
1793+
},
1794+
},
1795+
'random-connection-3': {
1796+
id: 'random-connection-3',
1797+
name: 'localhost:27018',
1798+
storageLocation: 'GLOBAL',
1799+
secretStorageLocation: SecretStorageLocation.Keytar,
1800+
connectionOptions: {
1801+
connectionString:
1802+
'mongodb://localhost:27018/?readPreference=primary&ssl=false',
1803+
},
1804+
},
1805+
} as any;
1806+
});
1807+
testSandbox.replace(
1808+
testConnectionController,
1809+
'_getConnectionInfoWithSecrets',
1810+
(connectionInfo) => Promise.resolve(connectionInfo as any)
1811+
);
1812+
const trackStub = testSandbox.stub(
1813+
testTelemetryService,
1814+
'trackSavedConnectionsLoaded'
1815+
);
1816+
1817+
// Clear any connections and load so we get our stubbed connections from above.
1818+
testConnectionController.clearAllConnections();
1819+
await testConnectionController.loadSavedConnections();
1820+
1821+
// Load connections tracked. Called once because in the current load of
1822+
// migration there were no errors and hence the error tracking event won't
1823+
// be called.
1824+
assert.strictEqual(trackStub.calledOnce, true);
1825+
assert.deepStrictEqual(trackStub.lastCall.args, [
1826+
{
1827+
connections_with_secrets_in_keytar: 1,
1828+
connections_with_secrets_in_secret_storage: 2,
1829+
saved_connections: 3,
1830+
loaded_connections: 3,
1831+
},
1832+
]);
1833+
});
17551834
});
17561835
});

src/test/suite/telemetry/telemetryService.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,4 +716,48 @@ suite('Telemetry Controller Test Suite', () => {
716716
);
717717
});
718718
});
719+
720+
test('track saved connections loaded', () => {
721+
testTelemetryService.trackSavedConnectionsLoaded({
722+
saved_connections: 3,
723+
loaded_connections: 3,
724+
connections_with_secrets_in_keytar: 0,
725+
connections_with_secrets_in_secret_storage: 3,
726+
});
727+
728+
sandbox.assert.calledWith(
729+
fakeSegmentAnalyticsTrack,
730+
sinon.match({
731+
anonymousId,
732+
event: 'Saved Connections Loaded',
733+
properties: {
734+
saved_connections: 3,
735+
loaded_connections: 3,
736+
connections_with_secrets_in_keytar: 0,
737+
connections_with_secrets_in_secret_storage: 3,
738+
},
739+
})
740+
);
741+
});
742+
743+
test('track failed keytar secrets migrations', () => {
744+
testTelemetryService.trackKeytarSecretsMigrationFailed({
745+
saved_connections: 3,
746+
loaded_connections: 3,
747+
connections_with_failed_keytar_migration: 1,
748+
});
749+
750+
sandbox.assert.calledWith(
751+
fakeSegmentAnalyticsTrack,
752+
sinon.match({
753+
anonymousId,
754+
event: 'Keytar Secrets Migration Failed',
755+
properties: {
756+
saved_connections: 3,
757+
loaded_connections: 3,
758+
connections_with_failed_keytar_migration: 1,
759+
},
760+
})
761+
);
762+
});
719763
});

0 commit comments

Comments
 (0)