Skip to content

Commit cf2fcc3

Browse files
chore: add language server logs VSCODE-447, VSCODE-448, VSCODE-449 (#563)
* chore: add language server logs VSCODE-447 * chore: update log message * docs: update comment * feat: grab the active connection if ls recovers from a failure VSCODE-448 * refactor: update output length * docs: update error message * docs: update comments * refactor: remove ls extra initialisation from playground controller * refactor: address PR comments * refactor: rename * test: try to skip the failing test * test: stub trackNewConnection * test: stub trackNewConnection in playground tests * test: more trackNewConnection stubs * feat: add connection id to tree logs * test: skip telemetry test with live connection * docs: link jira ticket * test: clean up
1 parent 3aa18ec commit cf2fcc3

31 files changed

+463
-302
lines changed

package-lock.json

Lines changed: 7 additions & 7 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
@@ -986,7 +986,7 @@
986986
"micromatch": "^4.0.5",
987987
"mongodb": "^5.6.0",
988988
"mongodb-build-info": "^1.5.0",
989-
"mongodb-cloud-info": "^2.0.1",
989+
"mongodb-cloud-info": "^2.1.0",
990990
"mongodb-connection-string-url": "^2.6.0",
991991
"mongodb-data-service": "^22.8.0",
992992
"mongodb-query-parser": "^2.5.0",

src/connectionController.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export default class ConnectionController {
110110
} = Object.create(null);
111111
_activeDataService: DataService | null = null;
112112
_storageController: StorageController;
113+
_telemetryService: TelemetryService;
113114

114115
private readonly _serviceName = 'mdb.vscode.savedConnections';
115116
private _currentConnectionId: null | string = null;
@@ -125,7 +126,6 @@ export default class ConnectionController {
125126
private _disconnecting = false;
126127

127128
private _statusView: StatusView;
128-
private _telemetryService: TelemetryService;
129129

130130
// Used by other parts of the extension that respond to changes in the connections.
131131
private eventEmitter: EventEmitter = new EventEmitter();
@@ -509,10 +509,18 @@ export default class ConnectionController {
509509
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
510510

511511
if (this._activeDataService) {
512+
log.info('Disconnecting from the previous connection...', {
513+
connectionId: this._currentConnectionId,
514+
});
512515
await this.disconnect();
513516
}
514517

515518
this._statusView.showMessage('Connecting to MongoDB...');
519+
log.info('Connecting to MongoDB...', {
520+
connectionInfo: JSON.stringify(
521+
extractSecrets(this._connections[connectionId]).connectionInfo
522+
),
523+
});
516524

517525
const connectionOptions = this._connections[connectionId].connectionOptions;
518526

@@ -551,7 +559,7 @@ export default class ConnectionController {
551559
throw connectError;
552560
}
553561

554-
log.info('Successfully connected');
562+
log.info('Successfully connected', { connectionId });
555563
void vscode.window.showInformationMessage('MongoDB connection successful.');
556564

557565
this._activeDataService = dataService;

src/editors/playgroundController.ts

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export default class PlaygroundController {
152152
this._connectionController.addEventListener(
153153
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
154154
() => {
155-
void this._connectToServiceProvider();
155+
void this._activeConnectionChanged();
156156
}
157157
);
158158

@@ -163,22 +163,26 @@ export default class PlaygroundController {
163163
this._playgroundResultViewColumn = editor.viewColumn;
164164
this._playgroundResultTextDocument = editor?.document;
165165
}
166+
const isPlaygroundEditor = isPlayground(editor?.document.uri);
166167

167168
void vscode.commands.executeCommand(
168169
'setContext',
169170
'mdb.isPlayground',
170-
isPlayground(editor?.document.uri)
171+
isPlaygroundEditor
171172
);
172173

173-
if (editor?.document.languageId !== 'Log') {
174+
if (isPlaygroundEditor) {
174175
this._activeTextEditor = editor;
175176
this._activeConnectionCodeLensProvider.setActiveTextEditor(
176177
this._activeTextEditor
177178
);
178179
this._playgroundSelectedCodeActionProvider.setActiveTextEditor(
179180
this._activeTextEditor
180181
);
181-
log.info('Active editor path', editor?.document.uri?.path);
182+
log.info('Active editor', {
183+
documentPath: editor?.document.uri?.path,
184+
documentLanguageId: editor?.document.languageId,
185+
});
182186
}
183187
};
184188

@@ -245,35 +249,24 @@ export default class PlaygroundController {
245249
);
246250
}
247251

248-
async _connectToServiceProvider(): Promise<void> {
249-
// Disconnect if already connected.
250-
await this._languageServerController.disconnectFromServiceProvider();
251-
252+
async _activeConnectionChanged(): Promise<void> {
252253
const dataService = this._connectionController.getActiveDataService();
253254
const connectionId = this._connectionController.getActiveConnectionId();
255+
let mongoClientOption;
254256

255-
if (!dataService || !connectionId) {
256-
this._activeConnectionCodeLensProvider.refresh();
257-
258-
return;
259-
}
260-
261-
const mongoClientOption =
262-
this._connectionController.getMongoClientConnectionOptions();
263-
264-
if (!mongoClientOption) {
265-
this._activeConnectionCodeLensProvider.refresh();
257+
this._activeConnectionCodeLensProvider.refresh();
266258

267-
return;
259+
if (dataService && connectionId) {
260+
mongoClientOption =
261+
this._connectionController.getMongoClientConnectionOptions();
268262
}
269263

270-
await this._languageServerController.connectToServiceProvider({
264+
// The connectionId is null when disconnecting.
265+
await this._languageServerController.activeConnectionChanged({
271266
connectionId,
272-
connectionString: mongoClientOption.url,
273-
connectionOptions: mongoClientOption.options,
267+
connectionString: mongoClientOption?.url,
268+
connectionOptions: mongoClientOption?.options,
274269
});
275-
276-
this._activeConnectionCodeLensProvider.refresh();
277270
}
278271

279272
async _createPlaygroundFileWithContent(
@@ -420,36 +413,28 @@ export default class PlaygroundController {
420413

421414
this._statusView.showMessage('Getting results...');
422415

416+
let result: ShellEvaluateResult;
423417
try {
424418
// Send a request to the language server to execute scripts from a playground.
425-
const result: ShellEvaluateResult =
426-
await this._languageServerController.evaluate({
427-
codeToEvaluate,
428-
connectionId,
429-
});
430-
431-
this._statusView.hideMessage();
432-
this._telemetryService.trackPlaygroundCodeExecuted(
433-
result,
434-
this._isPartialRun,
435-
result ? false : true
436-
);
437-
438-
return result;
439-
} catch (err: any) {
440-
// We re-initialize the language server when we encounter an error.
441-
// This happens when the language server worker runs out of memory, can't be revitalized, and restarts.
442-
if (err?.code === -32097) {
443-
void vscode.window.showErrorMessage(
444-
'An error occurred when running the playground. This can occur when the playground runner runs out of memory.'
445-
);
419+
result = await this._languageServerController.evaluate({
420+
codeToEvaluate,
421+
connectionId,
422+
});
423+
} catch (error) {
424+
const msg =
425+
'An internal error has occurred. The playground services have been restored. This can occur when the playground runner runs out of memory.';
426+
log.error(msg, error);
427+
void vscode.window.showErrorMessage(msg);
428+
}
446429

447-
await this._languageServerController.startLanguageServer();
448-
void this._connectToServiceProvider();
449-
}
430+
this._statusView.hideMessage();
431+
this._telemetryService.trackPlaygroundCodeExecuted(
432+
result,
433+
this._isPartialRun,
434+
result ? false : true
435+
);
450436

451-
throw err;
452-
}
437+
return result;
453438
}
454439

455440
_getAllText(): string {

src/explorer/explorerController.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import ConnectionController, {
55
} from '../connectionController';
66
import ExplorerTreeController from './explorerTreeController';
77

8-
import { createLogger } from '../logging';
9-
10-
const log = createLogger('explorer controller');
11-
128
export default class ExplorerController {
139
private _connectionController: ConnectionController;
1410
private _treeController: ExplorerTreeController;
@@ -38,14 +34,12 @@ export default class ExplorerController {
3834
};
3935

4036
activateConnectionsTreeView(): void {
41-
log.info('Activating explorer controller...');
4237
// Listen for a change in connections to occur before we create the tree
4338
// so that we show the `viewsWelcome` before any connections are added.
4439
this._connectionController.addEventListener(
4540
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
4641
this.createTreeView
4742
);
48-
log.info('Explorer controller activated');
4943
}
5044

5145
deactivate(): void {

src/explorer/explorerTreeController.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export default class ExplorerTreeController
6464
});
6565

6666
treeView.onDidExpandElement(async (event: any): Promise<void> => {
67-
log.info('Tree item was expanded', event.element.label);
67+
log.info('Connection tree item was expanded', {
68+
connectionId: event.element.connectionId,
69+
connectionName: event.element.label,
70+
isExpanded: event.element.isExpanded,
71+
});
6872

6973
if (!event.element.onDidExpand) {
7074
return;

src/explorer/helpExplorer.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import * as vscode from 'vscode';
22
import HelpTree from './helpTree';
3-
import { createLogger } from '../logging';
43
import { TelemetryService } from '../telemetry';
54

6-
const log = createLogger('help and info explorer');
7-
85
export default class HelpExplorer {
96
_treeController: HelpTree;
107
_treeView?: vscode.TreeView<vscode.TreeItem>;
@@ -15,15 +12,13 @@ export default class HelpExplorer {
1512

1613
activateHelpTreeView(telemetryService: TelemetryService): void {
1714
if (!this._treeView) {
18-
log.info('Activating help explorer...');
1915
this._treeView = vscode.window.createTreeView('mongoDBHelpExplorer', {
2016
treeDataProvider: this._treeController,
2117
});
2218
this._treeController.activateTreeViewEventHandlers(
2319
this._treeView,
2420
telemetryService
2521
);
26-
log.info('Help explorer activated');
2722
}
2823
}
2924

src/explorer/playgroundsExplorer.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import * as vscode from 'vscode';
22
import PlaygroundsTree from './playgroundsTree';
3-
import { createLogger } from '../logging';
4-
5-
const log = createLogger('playgrounds explorer');
63

74
export default class PlaygroundsExplorer {
85
private _treeController: PlaygroundsTree;
@@ -25,9 +22,7 @@ export default class PlaygroundsExplorer {
2522
};
2623

2724
public activatePlaygroundsTreeView(): void {
28-
log.info('Activating playgrounds explorer...');
2925
this.createPlaygroundsTreeView();
30-
log.info('Playgrounds explorer activated');
3126
}
3227

3328
public deactivate(): void {

src/explorer/playgroundsTree.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export default class PlaygroundsTree
5050
});
5151

5252
treeView.onDidExpandElement(async (event: any): Promise<void> => {
53-
log.info('Tree item was expanded', event.element.label);
53+
log.info('Playground tree item was expanded', {
54+
playgroundName: event.element.label,
55+
});
5456

5557
if (!event.element.onDidExpand) {
5658
return;

src/extension.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as vscode from 'vscode';
55
import { ext } from './extensionConstants';
66
import { createKeytar } from './utils/keytar';
77
import { createLogger } from './logging';
8+
// eslint-disable-next-line @typescript-eslint/no-var-requires
9+
const { version } = require('../package.json');
810

911
const log = createLogger('extension');
1012

@@ -27,28 +29,50 @@ let mdbExtension: MDBExtensionController;
2729
export async function activate(
2830
context: vscode.ExtensionContext
2931
): Promise<void> {
30-
log.info('Activating extension...');
3132
ext.context = context;
33+
let hasKeytar = false;
3234

3335
try {
3436
ext.keytarModule = createKeytar();
37+
hasKeytar = true;
3538
} catch (err) {
3639
// Couldn't load keytar, proceed without storing & loading connections.
3740
}
3841

42+
const defaultConnectionSavingLocation = vscode.workspace
43+
.getConfiguration('mdb.connectionSaving')
44+
.get('defaultConnectionSavingLocation');
45+
46+
log.info('Activating extension...', {
47+
id: context.extension.id,
48+
version: version,
49+
mode: vscode.ExtensionMode[context.extensionMode],
50+
kind: vscode.ExtensionKind[context.extension.extensionKind],
51+
extensionPath: context.extensionPath,
52+
logPath: context.logUri.path,
53+
workspaceStoragePath: context.storageUri?.path,
54+
globalStoragePath: context.globalStorageUri.path,
55+
defaultConnectionSavingLocation,
56+
hasKeytar,
57+
buildInfo: {
58+
nodeVersion: process.version,
59+
runtimePlatform: process.platform,
60+
runtimeArch: process.arch,
61+
},
62+
});
63+
3964
mdbExtension = new MDBExtensionController(context, {
4065
shouldTrackTelemetry: true,
4166
});
4267
await mdbExtension.activate();
4368

4469
// Add our extension to a list of disposables for when we are deactivated.
4570
context.subscriptions.push(mdbExtension);
46-
47-
log.info('Extension activated');
4871
}
4972

5073
// Called when our extension is deactivated.
5174
export async function deactivate(): Promise<void> {
75+
log.info('Deactivating extension...');
5276
if (mdbExtension) {
5377
await mdbExtension.deactivate();
5478
}

0 commit comments

Comments
 (0)