Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/TracingLanguageClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { Event, EventEmitter } from "vscode";
import { CancellationToken, LanguageClient, LanguageClientOptions, ProtocolRequestType, ProtocolRequestType0, RequestType, RequestType0, ServerOptions } from "vscode-languageclient/node";
import { TraceEvent } from "./extension.api";

const requestEventEmitter = new EventEmitter<TraceEvent>();
export const onDidRequestEnd: Event<TraceEvent> = requestEventEmitter.event;

export class TracingLanguageClient extends LanguageClient {
private isStarted: boolean = false;

constructor(id: string, name: string, serverOptions: ServerOptions, clientOptions: LanguageClientOptions, forceDebug?: boolean) {
super(id, name, serverOptions, clientOptions, forceDebug);
}

start(): Promise<void> {
const isFirstTimeStart: boolean = !this.isStarted;
this.isStarted = true;
const startAt: number = Date.now();
return super.start().then(value => {
if (isFirstTimeStart) {
this.fireTraceEvent("initialize", startAt);
}
return value;
}, reason => {
if (isFirstTimeStart) {
this.fireTraceEvent("initialize", startAt, reason);
}
throw reason;
});
}

stop(timeout?: number): Promise<void> {
this.isStarted = false;
return super.stop(timeout);
}

sendRequest<R, PR, E, RO>(type: ProtocolRequestType0<R, PR, E, RO>, token?: CancellationToken): Promise<R>;
sendRequest<P, R, PR, E, RO>(type: ProtocolRequestType<P, R, PR, E, RO>, params: P, token?: CancellationToken): Promise<R>;
sendRequest<R, E>(type: RequestType0<R, E>, token?: CancellationToken): Promise<R>;
sendRequest<P, R, E>(type: RequestType<P, R, E>, params: P, token?: CancellationToken): Promise<R>;
sendRequest<R>(method: string, token?: CancellationToken): Promise<R>;
sendRequest<R>(method: string, param: any, token?: CancellationToken): Promise<R>;
sendRequest(method: any, ...args) {
const startAt: number = Date.now();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to use performance.now(). See: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

Also, Date.now() may have been impacted by system and user clock adjustments, clock skew, etc. as it is relative to the Unix epoch (1970-01-01T00:00:00Z) and dependent on the system clock. The performance.now() method on the other hand is relative to the timeOrigin property which is a monotonic clock: its current time never decreases and isn't subject to adjustments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. Good to know the new API.

The data will like textDocument/completion - 1359.5962999993935 ms with performance.now api. Then the downstream tracer can decide whether to convert the floating point number to integer.

const requestType: string = this.getRequestType(method, ...args);
return this.sendRequest0(method, ...args).then(value => {
this.fireTraceEvent(requestType, startAt);
return value;
}, reason => {
this.fireTraceEvent(requestType, startAt, reason);
throw reason;
});
}

private sendRequest0(method: any, ...args) {
if (!args || !args.length) {
return super.sendRequest(method);
}

const first = args[0];
const last = args[args.length - 1];
if (CancellationToken.is(last)) {
if (first === last) {
return super.sendRequest(method, last);
} else {
return super.sendRequest(method, first, last);
}
}

return super.sendRequest(method, first);
}

private getRequestType(method: any, ...args): string {
let requestType: string;
if (typeof method === 'string' || method instanceof String) {
requestType = String(method);
} else {
requestType = method?.method;
}

if (requestType === "workspace/executeCommand") {
if (args && args[0]?.command) {
requestType = `workspace/executeCommand/${args[0].command}`;
}
}

return requestType;
}

private fireTraceEvent(type: string, startAt: number, reason?: any): void {
const duration: number = Date.now() - startAt;
requestEventEmitter.fire({
type,
duration,
error: reason,
});
}
}
2 changes: 2 additions & 0 deletions src/apiManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Commands } from "./commands";
import { Emitter } from "vscode-languageclient";
import { ServerMode } from "./settings";
import { registerHoverCommand } from "./hoverAction";
import { onDidRequestEnd } from "./TracingLanguageClient";

class ApiManager {

Expand Down Expand Up @@ -60,6 +61,7 @@ class ApiManager {
onDidServerModeChange,
onDidProjectsImport,
serverReady,
onDidRequestEnd,
};
}

Expand Down
24 changes: 23 additions & 1 deletion src/extension.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,22 @@ export enum ClientStatus {
stopping = "Stopping",
}

export const extensionApiVersion = '0.7';
export interface TraceEvent {
/**
* Request type.
*/
type: string;
/**
* Time (in milliseconds) taken to process a request.
*/
duration: number;
/**
* Error message that occurs while processing a request.
*/
error?: string;
}

export const extensionApiVersion = '0.8';

export interface ExtensionAPI {
readonly apiVersion: string;
Expand Down Expand Up @@ -118,4 +133,11 @@ export interface ExtensionAPI {
* @since extension version 1.7.0
*/
readonly serverReady: () => Promise<boolean>;

/**
* An event that's fired when a request has been responded.
* @since API version 0.8
* @since extension version 1.16.0
*/
readonly onDidRequestEnd: Event<TraceEvent>;
}
3 changes: 2 additions & 1 deletion src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { excludeProjectSettingsFiles, ServerMode, setGradleWrapperChecksum } fro
import { snippetCompletionProvider } from "./snippetCompletionProvider";
import * as sourceAction from './sourceAction';
import { askForProjects, projectConfigurationUpdate, upgradeGradle } from "./standardLanguageClientUtils";
import { TracingLanguageClient } from './TracingLanguageClient';
import { TypeHierarchyDirection, TypeHierarchyItem } from "./typeHierarchy/protocol";
import { typeHierarchyTree } from "./typeHierarchy/typeHierarchyTree";
import { getAllJavaProjects, getJavaConfig, getJavaConfiguration } from "./utils";
Expand Down Expand Up @@ -103,7 +104,7 @@ export class StandardLanguageClient {
}

// Create the language client and start the client.
this.languageClient = new LanguageClient('java', extensionName, serverOptions, clientOptions);
this.languageClient = new TracingLanguageClient('java', extensionName, serverOptions, clientOptions);

this.registerCommandsForStandardServer(context, jdtEventEmitter);
fileEventHandler.registerFileEventHandlers(this.languageClient, context);
Expand Down