Skip to content

Commit 27a6708

Browse files
Migrate the legacy log to the telemetry wrapper (#866)
1 parent 766b602 commit 27a6708

5 files changed

Lines changed: 86 additions & 34 deletions

File tree

src/configurationProvider.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as os from "os";
66
import * as path from "path";
77
import * as vscode from "vscode";
88

9-
import { instrumentOperation } from "vscode-extension-telemetry-wrapper";
9+
import { instrumentOperation, sendInfo } from "vscode-extension-telemetry-wrapper";
1010
import * as anchor from "./anchor";
1111
import { buildWorkspace } from "./build";
1212
import { populateStepFilters, substituteFilterVariables } from "./classFilter";
@@ -75,6 +75,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
7575
return this.resolveAndValidateDebugConfiguration(folder, config);
7676
} catch (ex) {
7777
utility.showErrorMessage({
78+
type: Type.EXCEPTION,
7879
message: String((ex && ex.message) || ex),
7980
});
8081
return undefined;
@@ -296,12 +297,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
296297
return undefined;
297298
}
298299

299-
const errorMessage = (ex && ex.message) || ex;
300-
utility.showErrorMessageWithTroubleshooting({
301-
message: String(errorMessage),
302-
type: Type.EXCEPTION,
303-
details: utility.formatErrorProperties(ex),
304-
});
300+
utility.showErrorMessageWithTroubleshooting(utility.convertErrorToMessage(ex));
305301
return undefined;
306302
}
307303
}
@@ -392,6 +388,12 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
392388
const selectedFix: lsPlugin.IMainClassOption =
393389
await this.showMainClassQuickPick(pickItems, "Please select main class<project name>.", false);
394390
if (selectedFix) {
391+
sendInfo(null, {
392+
fix: "yes",
393+
fixMessage: errors.join(os.EOL),
394+
});
395+
396+
// Deprecated
395397
logger.log(Type.USAGEDATA, {
396398
fix: "yes",
397399
fixMessage: errors.join(os.EOL),

src/extension.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { NotificationBar } from "./customWidget";
1313
import { initializeCodeLensProvider, startDebugging } from "./debugCodeLensProvider";
1414
import { handleHotCodeReplaceCustomEvent, initializeHotCodeReplace, NO_BUTTON, YES_BUTTON } from "./hotCodeReplace";
1515
import { JavaDebugAdapterDescriptorFactory } from "./javaDebugAdapterDescriptorFactory";
16+
import { logJavaException, logJavaInfo } from "./javaLogger";
1617
import { IMainMethod, resolveMainMethod } from "./languageServerPlugin";
1718
import { logger, Type } from "./logger";
1819
import { pickJavaProcess } from "./processPicker";
@@ -27,11 +28,8 @@ export async function activate(context: vscode.ExtensionContext) {
2728
}
2829

2930
function initializeExtension(operationId: string, context: vscode.ExtensionContext) {
31+
// Deprecated
3032
logger.initialize(context, true);
31-
logger.log(Type.ACTIVATEEXTENSION, {}); // TODO: Activation belongs to usage data, remove this line.
32-
logger.log(Type.USAGEDATA, {
33-
description: "activateExtension",
34-
});
3533

3634
registerDebugEventListener(context);
3735
context.subscriptions.push(logger);
@@ -90,6 +88,13 @@ function registerDebugEventListener(context: vscode.ExtensionContext) {
9088
commonProperties[key] = String(entry[key]);
9189
}
9290
}
91+
if (entry.scope === "exception") {
92+
logJavaException(commonProperties);
93+
} else {
94+
logJavaInfo(commonProperties, measureProperties);
95+
}
96+
97+
// Deprecated
9398
logger.log(entry.scope === "exception" ? Type.EXCEPTION : Type.USAGEDATA, commonProperties, measureProperties);
9499
});
95100
}

src/javaDebugAdapterDescriptorFactory.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { DebugAdapterDescriptor, DebugAdapterDescriptorFactory, DebugAdapterExec
55

66
import { startDebugSession } from "./languageServerPlugin";
77
import { Type } from "./logger";
8-
import { formatErrorProperties, showErrorMessageWithTroubleshooting } from "./utility";
8+
import { convertErrorToMessage, showErrorMessageWithTroubleshooting } from "./utility";
99

1010
export class JavaDebugAdapterDescriptorFactory implements DebugAdapterDescriptorFactory {
1111
public async createDebugAdapterDescriptor(session: DebugSession, executable: DebugAdapterExecutable): Promise<DebugAdapterDescriptor> {
@@ -23,17 +23,10 @@ export class JavaDebugAdapterDescriptorFactory implements DebugAdapterDescriptor
2323
error = err;
2424
}
2525

26-
let errorMessage = "Failed to start debug server.";
27-
let details = {};
28-
if (error) {
29-
errorMessage = error.message || String(error);
30-
details = formatErrorProperties(error);
31-
}
32-
33-
showErrorMessageWithTroubleshooting({
34-
message: errorMessage,
26+
const message = error ? convertErrorToMessage(error) : {
3527
type: Type.EXCEPTION,
36-
details,
37-
});
28+
message: "Failed to start debug server.",
29+
};
30+
showErrorMessageWithTroubleshooting(message);
3831
}
3932
}

src/javaLogger.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { sendError, sendInfo, sendOperationError } from "vscode-extension-telemetry-wrapper";
5+
6+
export function logJavaException(errorProperties: any): void {
7+
/**
8+
* A sample errorProperties from Java code.
9+
* {
10+
* "description": "Failed to attach to remote debuggee VM. Reason: java.net.ConnectException: Connection refused: connect",
11+
* "message": "Failed to attach to remote debuggee VM. Reason: java.net.ConnectException: Connection refused: connect",
12+
* "stackTrace": "[{\"declaringClass\":\"com.microsoft.java.debug.core.adapter.AdapterUtils\", ...]",
13+
* "debugSessionid": "5680f12b-5b5f-4ac0-bda3-d1dbc3c12c10",
14+
* }
15+
*/
16+
const { debugSessionId, description, message, stackTrace } = errorProperties;
17+
sendOperationError(debugSessionId, "debugSession", {
18+
name: "JavaException",
19+
message: description || message,
20+
stack: stackTrace,
21+
});
22+
}
23+
24+
export function logJavaInfo(commonProperties: any, measureProperties?: any): void {
25+
if (measureProperties && measureProperties.duration !== undefined) {
26+
sendInfo(commonProperties.debugSessionId, commonProperties, measureProperties);
27+
} else {
28+
sendInfo(commonProperties.debugSessionId, commonProperties);
29+
}
30+
}

src/utility.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import * as path from "path";
55
import * as vscode from "vscode";
6-
import { setUserError } from "vscode-extension-telemetry-wrapper";
6+
import { sendError, sendInfo, setUserError } from "vscode-extension-telemetry-wrapper";
77
import { logger, Type } from "./logger";
88

99
const TROUBLESHOOTING_LINK = "https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md";
@@ -28,14 +28,10 @@ export class JavaExtensionNotEnabledError extends Error {
2828
}
2929
}
3030

31-
interface IProperties {
32-
[key: string]: string;
33-
}
34-
3531
interface ILoggingMessage {
36-
message: string;
3732
type?: Type;
38-
details?: IProperties;
33+
message: string;
34+
stack?: string;
3935
}
4036

4137
interface ITroubleshootingMessage extends ILoggingMessage {
@@ -47,11 +43,22 @@ function logMessage(message: ILoggingMessage): void {
4743
return;
4844
}
4945

50-
if (message.details) {
51-
logger.log(message.type, message.details);
46+
if (message.type === Type.EXCEPTION || message.type === Type.USAGEERROR) {
47+
const error: Error = {
48+
name: "error",
49+
message: message.message,
50+
stack: message.stack,
51+
};
52+
if (message.type === Type.USAGEERROR) {
53+
setUserError(error);
54+
}
55+
sendError(error);
5256
} else {
53-
logger.logMessage(message.type, message.message);
57+
sendInfo(null, { message: message.message });
5458
}
59+
60+
// Deprecated
61+
logger.log(message.type, { message: message.message, stack: message.stack });
5562
}
5663

5764
export async function showInformationMessage(message: ILoggingMessage, ...items: string[]): Promise<string | undefined> {
@@ -95,6 +102,12 @@ function handleTroubleshooting(choice: string, message: string, anchor: string):
95102

96103
export function openTroubleshootingPage(message: string, anchor: string) {
97104
vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(anchor ? `${TROUBLESHOOTING_LINK}#${anchor}` : TROUBLESHOOTING_LINK));
105+
sendInfo(null, {
106+
troubleshooting: "yes",
107+
troubleshootingMessage: message,
108+
});
109+
110+
// Deprecated
98111
logger.log(Type.USAGEDATA, {
99112
troubleshooting: "yes",
100113
troubleshootingMessage: message,
@@ -122,7 +135,16 @@ async function installJavaExtension() {
122135
}
123136
}
124137

125-
export function formatErrorProperties(ex: any): IProperties {
138+
export function convertErrorToMessage(err: Error): ILoggingMessage {
139+
const properties = formatErrorProperties(err);
140+
return {
141+
type: Type.EXCEPTION,
142+
message: properties.message,
143+
stack: properties.stackTrace,
144+
};
145+
}
146+
147+
function formatErrorProperties(ex: any): any {
126148
const exception = (ex && ex.data && ex.data.cause)
127149
|| { stackTrace: (ex && ex.stack), detailMessage: String((ex && ex.message) || ex || "Unknown exception") };
128150

0 commit comments

Comments
 (0)