From 27a6708f1fe32fde26754bf7eff751b65278e7ba Mon Sep 17 00:00:00 2001 From: Jinbo Wang Date: Thu, 20 Aug 2020 17:16:46 +0800 Subject: [PATCH] Migrate the legacy log to the telemetry wrapper (#866) --- src/configurationProvider.ts | 16 +++++---- src/extension.ts | 13 ++++--- src/javaDebugAdapterDescriptorFactory.ts | 17 +++------ src/javaLogger.ts | 30 ++++++++++++++++ src/utility.ts | 44 ++++++++++++++++++------ 5 files changed, 86 insertions(+), 34 deletions(-) create mode 100644 src/javaLogger.ts diff --git a/src/configurationProvider.ts b/src/configurationProvider.ts index a038728..47a0433 100644 --- a/src/configurationProvider.ts +++ b/src/configurationProvider.ts @@ -6,7 +6,7 @@ import * as os from "os"; import * as path from "path"; import * as vscode from "vscode"; -import { instrumentOperation } from "vscode-extension-telemetry-wrapper"; +import { instrumentOperation, sendInfo } from "vscode-extension-telemetry-wrapper"; import * as anchor from "./anchor"; import { buildWorkspace } from "./build"; import { populateStepFilters, substituteFilterVariables } from "./classFilter"; @@ -75,6 +75,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration return this.resolveAndValidateDebugConfiguration(folder, config); } catch (ex) { utility.showErrorMessage({ + type: Type.EXCEPTION, message: String((ex && ex.message) || ex), }); return undefined; @@ -296,12 +297,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration return undefined; } - const errorMessage = (ex && ex.message) || ex; - utility.showErrorMessageWithTroubleshooting({ - message: String(errorMessage), - type: Type.EXCEPTION, - details: utility.formatErrorProperties(ex), - }); + utility.showErrorMessageWithTroubleshooting(utility.convertErrorToMessage(ex)); return undefined; } } @@ -392,6 +388,12 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration const selectedFix: lsPlugin.IMainClassOption = await this.showMainClassQuickPick(pickItems, "Please select main class.", false); if (selectedFix) { + sendInfo(null, { + fix: "yes", + fixMessage: errors.join(os.EOL), + }); + + // Deprecated logger.log(Type.USAGEDATA, { fix: "yes", fixMessage: errors.join(os.EOL), diff --git a/src/extension.ts b/src/extension.ts index 58f4800..0f7f8b7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -13,6 +13,7 @@ import { NotificationBar } from "./customWidget"; import { initializeCodeLensProvider, startDebugging } from "./debugCodeLensProvider"; import { handleHotCodeReplaceCustomEvent, initializeHotCodeReplace, NO_BUTTON, YES_BUTTON } from "./hotCodeReplace"; import { JavaDebugAdapterDescriptorFactory } from "./javaDebugAdapterDescriptorFactory"; +import { logJavaException, logJavaInfo } from "./javaLogger"; import { IMainMethod, resolveMainMethod } from "./languageServerPlugin"; import { logger, Type } from "./logger"; import { pickJavaProcess } from "./processPicker"; @@ -27,11 +28,8 @@ export async function activate(context: vscode.ExtensionContext) { } function initializeExtension(operationId: string, context: vscode.ExtensionContext) { + // Deprecated logger.initialize(context, true); - logger.log(Type.ACTIVATEEXTENSION, {}); // TODO: Activation belongs to usage data, remove this line. - logger.log(Type.USAGEDATA, { - description: "activateExtension", - }); registerDebugEventListener(context); context.subscriptions.push(logger); @@ -90,6 +88,13 @@ function registerDebugEventListener(context: vscode.ExtensionContext) { commonProperties[key] = String(entry[key]); } } + if (entry.scope === "exception") { + logJavaException(commonProperties); + } else { + logJavaInfo(commonProperties, measureProperties); + } + + // Deprecated logger.log(entry.scope === "exception" ? Type.EXCEPTION : Type.USAGEDATA, commonProperties, measureProperties); }); } diff --git a/src/javaDebugAdapterDescriptorFactory.ts b/src/javaDebugAdapterDescriptorFactory.ts index d288036..8939d8b 100644 --- a/src/javaDebugAdapterDescriptorFactory.ts +++ b/src/javaDebugAdapterDescriptorFactory.ts @@ -5,7 +5,7 @@ import { DebugAdapterDescriptor, DebugAdapterDescriptorFactory, DebugAdapterExec import { startDebugSession } from "./languageServerPlugin"; import { Type } from "./logger"; -import { formatErrorProperties, showErrorMessageWithTroubleshooting } from "./utility"; +import { convertErrorToMessage, showErrorMessageWithTroubleshooting } from "./utility"; export class JavaDebugAdapterDescriptorFactory implements DebugAdapterDescriptorFactory { public async createDebugAdapterDescriptor(session: DebugSession, executable: DebugAdapterExecutable): Promise { @@ -23,17 +23,10 @@ export class JavaDebugAdapterDescriptorFactory implements DebugAdapterDescriptor error = err; } - let errorMessage = "Failed to start debug server."; - let details = {}; - if (error) { - errorMessage = error.message || String(error); - details = formatErrorProperties(error); - } - - showErrorMessageWithTroubleshooting({ - message: errorMessage, + const message = error ? convertErrorToMessage(error) : { type: Type.EXCEPTION, - details, - }); + message: "Failed to start debug server.", + }; + showErrorMessageWithTroubleshooting(message); } } diff --git a/src/javaLogger.ts b/src/javaLogger.ts new file mode 100644 index 0000000..bcafac9 --- /dev/null +++ b/src/javaLogger.ts @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +import { sendError, sendInfo, sendOperationError } from "vscode-extension-telemetry-wrapper"; + +export function logJavaException(errorProperties: any): void { + /** + * A sample errorProperties from Java code. + * { + * "description": "Failed to attach to remote debuggee VM. Reason: java.net.ConnectException: Connection refused: connect", + * "message": "Failed to attach to remote debuggee VM. Reason: java.net.ConnectException: Connection refused: connect", + * "stackTrace": "[{\"declaringClass\":\"com.microsoft.java.debug.core.adapter.AdapterUtils\", ...]", + * "debugSessionid": "5680f12b-5b5f-4ac0-bda3-d1dbc3c12c10", + * } + */ + const { debugSessionId, description, message, stackTrace } = errorProperties; + sendOperationError(debugSessionId, "debugSession", { + name: "JavaException", + message: description || message, + stack: stackTrace, + }); +} + +export function logJavaInfo(commonProperties: any, measureProperties?: any): void { + if (measureProperties && measureProperties.duration !== undefined) { + sendInfo(commonProperties.debugSessionId, commonProperties, measureProperties); + } else { + sendInfo(commonProperties.debugSessionId, commonProperties); + } +} diff --git a/src/utility.ts b/src/utility.ts index 5afbe33..f8fb8d0 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -3,7 +3,7 @@ import * as path from "path"; import * as vscode from "vscode"; -import { setUserError } from "vscode-extension-telemetry-wrapper"; +import { sendError, sendInfo, setUserError } from "vscode-extension-telemetry-wrapper"; import { logger, Type } from "./logger"; const TROUBLESHOOTING_LINK = "https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md"; @@ -28,14 +28,10 @@ export class JavaExtensionNotEnabledError extends Error { } } -interface IProperties { - [key: string]: string; -} - interface ILoggingMessage { - message: string; type?: Type; - details?: IProperties; + message: string; + stack?: string; } interface ITroubleshootingMessage extends ILoggingMessage { @@ -47,11 +43,22 @@ function logMessage(message: ILoggingMessage): void { return; } - if (message.details) { - logger.log(message.type, message.details); + if (message.type === Type.EXCEPTION || message.type === Type.USAGEERROR) { + const error: Error = { + name: "error", + message: message.message, + stack: message.stack, + }; + if (message.type === Type.USAGEERROR) { + setUserError(error); + } + sendError(error); } else { - logger.logMessage(message.type, message.message); + sendInfo(null, { message: message.message }); } + + // Deprecated + logger.log(message.type, { message: message.message, stack: message.stack }); } export async function showInformationMessage(message: ILoggingMessage, ...items: string[]): Promise { @@ -95,6 +102,12 @@ function handleTroubleshooting(choice: string, message: string, anchor: string): export function openTroubleshootingPage(message: string, anchor: string) { vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(anchor ? `${TROUBLESHOOTING_LINK}#${anchor}` : TROUBLESHOOTING_LINK)); + sendInfo(null, { + troubleshooting: "yes", + troubleshootingMessage: message, + }); + + // Deprecated logger.log(Type.USAGEDATA, { troubleshooting: "yes", troubleshootingMessage: message, @@ -122,7 +135,16 @@ async function installJavaExtension() { } } -export function formatErrorProperties(ex: any): IProperties { +export function convertErrorToMessage(err: Error): ILoggingMessage { + const properties = formatErrorProperties(err); + return { + type: Type.EXCEPTION, + message: properties.message, + stack: properties.stackTrace, + }; +} + +function formatErrorProperties(ex: any): any { const exception = (ex && ex.data && ex.data.cause) || { stackTrace: (ex && ex.stack), detailMessage: String((ex && ex.message) || ex || "Unknown exception") };