Skip to content

Commit 0816d1c

Browse files
authored
chore(telemetry): refactor telemetry types VSCODE-672 (#912)
1 parent b6e36e8 commit 0816d1c

33 files changed

+1085
-723
lines changed

src/connectionController.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { createLogger } from './logging';
1919
import formatError from './utils/formatError';
2020
import type { StorageController } from './storage';
2121
import type { StatusView } from './views';
22-
import type TelemetryService from './telemetry/telemetryService';
22+
import type { TelemetryService } from './telemetry';
2323
import { openLink } from './utils/linkHelper';
2424
import type {
2525
ConnectionSource,
@@ -28,8 +28,8 @@ import type {
2828
import { ConnectionStorage } from './storage/connectionStorage';
2929
import LINKS from './utils/links';
3030
import { isAtlasStream } from 'mongodb-build-info';
31-
import { DocumentSource } from './documentSource';
3231
import type { ConnectionTreeItem } from './explorer';
32+
import { PresetConnectionEditedTelemetryEvent } from './telemetry';
3333

3434
// eslint-disable-next-line @typescript-eslint/no-var-requires
3535
const packageJSON = require('../package.json');
@@ -129,18 +129,20 @@ export default class ConnectionController {
129129
// have a setting on the system for storing credentials.
130130
// When the setting is on this `connectionMergeInfos` would have the session
131131
// credential information and merge it before connecting.
132-
connectionMergeInfos: Record<string, RecursivePartial<LoadedConnection>> =
133-
Object.create(null);
132+
private _connectionMergeInfos: Record<
133+
string,
134+
RecursivePartial<LoadedConnection>
135+
> = Object.create(null);
134136

135-
_activeDataService: DataService | null = null;
137+
private _activeDataService: DataService | null = null;
136138
_connectionStorage: ConnectionStorage;
137139
_telemetryService: TelemetryService;
138140

139141
private readonly _serviceName = 'mdb.vscode.savedConnections';
140142
private _currentConnectionId: null | string = null;
141143

142144
_connectionAttempt: null | ConnectionAttempt = null;
143-
_connectionStringInputCancellationToken: null | vscode.CancellationTokenSource =
145+
private _connectionStringInputCancellationToken: null | vscode.CancellationTokenSource =
144146
null;
145147
private _connectingConnectionId: null | string = null;
146148
private _disconnecting = false;
@@ -169,10 +171,11 @@ export default class ConnectionController {
169171
async openPresetConnectionsSettings(
170172
originTreeItem: ConnectionTreeItem | undefined
171173
): Promise<void> {
172-
this._telemetryService.trackPresetConnectionEdited({
173-
source: DocumentSource.DOCUMENT_SOURCE_TREEVIEW,
174-
source_details: originTreeItem ? 'tree_item' : 'header',
175-
});
174+
this._telemetryService.track(
175+
new PresetConnectionEditedTelemetryEvent(
176+
originTreeItem ? 'tree_item' : 'header'
177+
)
178+
);
176179
let source: ConnectionSource | undefined = originTreeItem?.source;
177180
if (!source) {
178181
const mdbConfiguration = vscode.workspace.getConfiguration('mdb');
@@ -227,7 +230,7 @@ export default class ConnectionController {
227230

228231
// TODO: re-enable with fewer 'Saved Connections Loaded' events
229232
// https://jira.mongodb.org/browse/VSCODE-462
230-
/* this._telemetryService.trackSavedConnectionsLoaded({
233+
/* this._telemetryService.track(new SavedConnectionsLoadedTelemetryEvent({
231234
saved_connections: globalAndWorkspaceConnections.length,
232235
loaded_connections: loadedConnections.length,
233236
).length,
@@ -236,7 +239,7 @@ export default class ConnectionController {
236239
connection.secretStorageLocation ===
237240
SecretStorageLocation.SecretStorage
238241
).length,
239-
}); */
242+
})); */
240243
}
241244

242245
async connectWithURI(): Promise<boolean> {
@@ -402,7 +405,7 @@ export default class ConnectionController {
402405

403406
const connectionInfo: LoadedConnection = merge(
404407
cloneDeep(this._connections[connectionId]),
405-
this.connectionMergeInfos[connectionId] ?? {}
408+
this._connectionMergeInfos[connectionId] ?? {}
406409
);
407410

408411
if (!connectionInfo.connectionOptions) {
@@ -557,8 +560,8 @@ export default class ConnectionController {
557560
mergeConnectionInfo = {
558561
connectionOptions: await dataService.getUpdatedSecrets(),
559562
};
560-
this.connectionMergeInfos[connectionInfo.id] = merge(
561-
cloneDeep(this.connectionMergeInfos[connectionInfo.id]),
563+
this._connectionMergeInfos[connectionInfo.id] = merge(
564+
cloneDeep(this._connectionMergeInfos[connectionInfo.id]),
562565
mergeConnectionInfo
563566
);
564567
}
@@ -585,8 +588,8 @@ export default class ConnectionController {
585588
connectionOptions: await dataService.getUpdatedSecrets(),
586589
};
587590
if (!mergeConnectionInfo) return;
588-
this.connectionMergeInfos[connectionInfo.id] = merge(
589-
cloneDeep(this.connectionMergeInfos[connectionInfo.id]),
591+
this._connectionMergeInfos[connectionInfo.id] = merge(
592+
cloneDeep(this._connectionMergeInfos[connectionInfo.id]),
590593
mergeConnectionInfo
591594
);
592595

src/documentSource.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export enum DocumentSource {
44
DOCUMENT_SOURCE_COLLECTIONVIEW = 'collectionview',
55
DOCUMENT_SOURCE_CODELENS = 'codelens',
66
}
7+
8+
export type DocumentSourceDetails = 'database' | 'collection' | undefined;

src/editors/editorsController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import type PlaygroundController from './playgroundController';
3131
import type PlaygroundResultProvider from './playgroundResultProvider';
3232
import { PLAYGROUND_RESULT_SCHEME } from './playgroundResultProvider';
3333
import { StatusView } from '../views';
34-
import type TelemetryService from '../telemetry/telemetryService';
34+
import type { TelemetryService } from '../telemetry';
3535
import type { QueryWithCopilotCodeLensProvider } from './queryWithCopilotCodeLensProvider';
3636

3737
const log = createLogger('editors controller');

src/editors/mongoDBDocumentService.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { DocumentSource } from '../documentSource';
77
import type { EditDocumentInfo } from '../types/editDocumentInfoType';
88
import formatError from '../utils/formatError';
99
import type { StatusView } from '../views';
10-
import type TelemetryService from '../telemetry/telemetryService';
10+
import type { TelemetryService } from '../telemetry';
1111
import { getEJSON } from '../utils/ejson';
12+
import { DocumentUpdatedTelemetryEvent } from '../telemetry';
1213

1314
const log = createLogger('document controller');
1415

@@ -50,9 +51,11 @@ export default class MongoDBDocumentService {
5051
_saveDocumentFailed(message: string): void {
5152
const errorMessage = `Unable to save document: ${message}`;
5253

53-
this._telemetryService.trackDocumentUpdated(
54-
DocumentSource.DOCUMENT_SOURCE_TREEVIEW,
55-
false
54+
this._telemetryService.track(
55+
new DocumentUpdatedTelemetryEvent(
56+
DocumentSource.DOCUMENT_SOURCE_TREEVIEW,
57+
false
58+
)
5659
);
5760

5861
throw new Error(errorMessage);
@@ -98,7 +101,9 @@ export default class MongoDBDocumentService {
98101
returnDocument: 'after',
99102
}
100103
);
101-
this._telemetryService.trackDocumentUpdated(source, true);
104+
this._telemetryService.track(
105+
new DocumentUpdatedTelemetryEvent(source, true)
106+
);
102107
} catch (error) {
103108
return this._saveDocumentFailed(formatError(error).message);
104109
} finally {

src/editors/playgroundController.ts

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ import {
3434
import playgroundSearchTemplate from '../templates/playgroundSearchTemplate';
3535
import playgroundTemplate from '../templates/playgroundTemplate';
3636
import type { StatusView } from '../views';
37-
import type TelemetryService from '../telemetry/telemetryService';
38-
import {
39-
isPlayground,
40-
getSelectedText,
41-
getAllText,
42-
getPlaygroundExtensionForTelemetry,
43-
} from '../utils/playground';
37+
import type { TelemetryService } from '../telemetry';
38+
import { isPlayground, getSelectedText, getAllText } from '../utils/playground';
4439
import type ExportToLanguageCodeLensProvider from './exportToLanguageCodeLensProvider';
4540
import { playgroundFromDatabaseTreeItemTemplate } from '../templates/playgroundFromDatabaseTreeItemTemplate';
4641
import { playgroundFromCollectionTreeItemTemplate } from '../templates/playgroundFromCollectionTreeItemTemplate';
42+
import {
43+
PlaygroundCreatedTelemetryEvent,
44+
PlaygroundExecutedTelemetryEvent,
45+
PlaygroundSavedTelemetryEvent,
46+
} from '../telemetry';
4747

4848
const log = createLogger('playground controller');
4949

@@ -135,17 +135,15 @@ export default class PlaygroundController {
135135
if (isPlayground(document.uri)) {
136136
// TODO: re-enable with fewer 'Playground Loaded' events
137137
// https://jira.mongodb.org/browse/VSCODE-432
138-
/* this._telemetryService.trackPlaygroundLoaded(
139-
getPlaygroundExtensionForTelemetry(document.uri)
140-
); */
138+
// this._telemetryService.track(new PlaygroundLoadedTelemetryEvent(document.uri));
141139
await vscode.languages.setTextDocumentLanguage(document, 'javascript');
142140
}
143141
});
144142

145143
vscode.workspace.onDidSaveTextDocument((document) => {
146144
if (isPlayground(document.uri)) {
147-
this._telemetryService.trackPlaygroundSaved(
148-
getPlaygroundExtensionForTelemetry(document.uri)
145+
this._telemetryService.track(
146+
new PlaygroundSavedTelemetryEvent(document.uri)
149147
);
150148
}
151149
});
@@ -231,7 +229,7 @@ export default class PlaygroundController {
231229
.replace('CURRENT_DATABASE', databaseName)
232230
.replace('CURRENT_COLLECTION', collectionName);
233231

234-
this._telemetryService.trackPlaygroundCreated('search');
232+
this._telemetryService.track(new PlaygroundCreatedTelemetryEvent('search'));
235233
return this._createPlaygroundFileWithContent(content);
236234
}
237235

@@ -246,9 +244,13 @@ export default class PlaygroundController {
246244
content = content
247245
.replace('NEW_DATABASE_NAME', element.databaseName)
248246
.replace('Create a new database', 'The current database to use');
249-
this._telemetryService.trackPlaygroundCreated('createCollection');
247+
this._telemetryService.track(
248+
new PlaygroundCreatedTelemetryEvent('createCollection')
249+
);
250250
} else {
251-
this._telemetryService.trackPlaygroundCreated('createDatabase');
251+
this._telemetryService.track(
252+
new PlaygroundCreatedTelemetryEvent('createDatabase')
253+
);
252254
}
253255

254256
return this._createPlaygroundFileWithContent(content);
@@ -262,7 +264,7 @@ export default class PlaygroundController {
262264
.replace('CURRENT_DATABASE', databaseName)
263265
.replace('CURRENT_COLLECTION', collectionName);
264266

265-
this._telemetryService.trackPlaygroundCreated('index');
267+
this._telemetryService.track(new PlaygroundCreatedTelemetryEvent('index'));
266268
return this._createPlaygroundFileWithContent(content);
267269
}
268270

@@ -277,7 +279,7 @@ export default class PlaygroundController {
277279
const content = useDefaultTemplate
278280
? playgroundBasicTextTemplate.replace('PLAYGROUND_CONTENT', text)
279281
: text;
280-
this._telemetryService.trackPlaygroundCreated('agent');
282+
this._telemetryService.track(new PlaygroundCreatedTelemetryEvent('agent'));
281283
return this._createPlaygroundFileWithContent(content);
282284
}
283285

@@ -291,7 +293,9 @@ export default class PlaygroundController {
291293
.replace('CURRENT_COLLECTION', collectionName)
292294
.replace('DOCUMENT_CONTENTS', documentContents);
293295

294-
this._telemetryService.trackPlaygroundCreated('cloneDocument');
296+
this._telemetryService.track(
297+
new PlaygroundCreatedTelemetryEvent('cloneDocument')
298+
);
295299
return this._createPlaygroundFileWithContent(content);
296300
}
297301

@@ -303,7 +307,9 @@ export default class PlaygroundController {
303307
.replace('CURRENT_DATABASE', databaseName)
304308
.replace('CURRENT_COLLECTION', collectionName);
305309

306-
this._telemetryService.trackPlaygroundCreated('insertDocument');
310+
this._telemetryService.track(
311+
new PlaygroundCreatedTelemetryEvent('insertDocument')
312+
);
307313
return this._createPlaygroundFileWithContent(content);
308314
}
309315

@@ -314,7 +320,9 @@ export default class PlaygroundController {
314320

315321
element.cacheIsUpToDate = false;
316322

317-
this._telemetryService.trackPlaygroundCreated('createStreamProcessor');
323+
this._telemetryService.track(
324+
new PlaygroundCreatedTelemetryEvent('createStreamProcessor')
325+
);
318326

319327
return this._createPlaygroundFileWithContent(content);
320328
}
@@ -325,13 +333,17 @@ export default class PlaygroundController {
325333
let content = '';
326334
if (treeItem instanceof DatabaseTreeItem) {
327335
content = playgroundFromDatabaseTreeItemTemplate(treeItem.databaseName);
328-
this._telemetryService.trackPlaygroundCreated('fromDatabaseTreeItem');
336+
this._telemetryService.track(
337+
new PlaygroundCreatedTelemetryEvent('fromDatabaseTreeItem')
338+
);
329339
} else if (treeItem instanceof CollectionTreeItem) {
330340
content = playgroundFromCollectionTreeItemTemplate(
331341
treeItem.databaseName,
332342
treeItem.collectionName
333343
);
334-
this._telemetryService.trackPlaygroundCreated('fromCollectionTreeItem');
344+
this._telemetryService.track(
345+
new PlaygroundCreatedTelemetryEvent('fromCollectionTreeItem')
346+
);
335347
}
336348

337349
return this._createPlaygroundFileWithContent(content);
@@ -350,7 +362,7 @@ export default class PlaygroundController {
350362
content = template;
351363
}
352364

353-
this._telemetryService.trackPlaygroundCreated('crud');
365+
this._telemetryService.track(new PlaygroundCreatedTelemetryEvent('crud'));
354366
return this._createPlaygroundFileWithContent(content);
355367
}
356368

@@ -391,10 +403,12 @@ export default class PlaygroundController {
391403
}
392404

393405
this._statusView.hideMessage();
394-
this._telemetryService.trackPlaygroundCodeExecuted(
395-
result,
396-
this._isPartialRun,
397-
result ? false : true
406+
this._telemetryService.track(
407+
new PlaygroundExecutedTelemetryEvent(
408+
result,
409+
this._isPartialRun,
410+
result ? false : true
411+
)
398412
);
399413

400414
return result;

src/editors/queryWithCopilotCodeLensProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class QueryWithCopilotCodeLensProvider
3535
isNewChat: true,
3636
telemetry: {
3737
source: DocumentSource.DOCUMENT_SOURCE_CODELENS,
38+
source_details: undefined,
3839
},
3940
};
4041

src/explorer/helpTree.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getImagesPath } from '../extensionConstants';
44
import type { TelemetryService } from '../telemetry';
55
import { openLink } from '../utils/linkHelper';
66
import LINKS from '../utils/links';
7+
import { LinkClickedTelemetryEvent } from '../telemetry';
78

89
const HELP_LINK_CONTEXT_VALUE = 'HELP_LINK';
910

@@ -144,7 +145,9 @@ export default class HelpTree
144145
telemetryService: TelemetryService
145146
): Promise<void> {
146147
if (helpItem.contextValue === HELP_LINK_CONTEXT_VALUE) {
147-
telemetryService.trackLinkClicked('helpPanel', helpItem.linkId);
148+
telemetryService.track(
149+
new LinkClickedTelemetryEvent('helpPanel', helpItem.linkId)
150+
);
148151

149152
if (helpItem.useRedirect) {
150153
try {

0 commit comments

Comments
 (0)