Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API onWillRequestStart to track request send event #3316

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 16 additions & 4 deletions src/TracingLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ 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;
const requestStartEventEmitter = new EventEmitter<TraceEvent>();
const requestEndEventEmitter = new EventEmitter<TraceEvent>();
export const onWillRequestStart: Event<TraceEvent> = requestStartEventEmitter.event;
export const onDidRequestEnd: Event<TraceEvent> = requestEndEventEmitter.event;

export class TracingLanguageClient extends LanguageClient {
private isStarted: boolean = false;
Expand All @@ -17,6 +19,9 @@ export class TracingLanguageClient extends LanguageClient {
const isFirstTimeStart: boolean = !this.isStarted;
this.isStarted = true;
const startAt: number = performance.now();
if (isFirstTimeStart) {
this.fireRequestStartTraceEvent("initialize");
}
return super.start().then(value => {
if (isFirstTimeStart) {
this.fireSuccessTraceEvent("initialize", startAt, undefined);
Expand Down Expand Up @@ -51,6 +56,7 @@ export class TracingLanguageClient extends LanguageClient {
triggerCharacter: args[0].context.triggerCharacter,
};
}
this.fireRequestStartTraceEvent(requestType);
return this.sendRequest0(method, ...args).then((value: any) => {
if (data && value?.itemDefaults?.data?.completionKinds) {
// Include the completionKinds from the completion response.
Expand Down Expand Up @@ -99,9 +105,15 @@ export class TracingLanguageClient extends LanguageClient {
return requestType;
}

private fireRequestStartTraceEvent(type: string): void {
requestStartEventEmitter.fire({
type,
});
}

private fireSuccessTraceEvent(type: string, startAt: number, resultLength: number | undefined, data?: any): void {
const duration: number = performance.now() - startAt;
requestEventEmitter.fire({
requestEndEventEmitter.fire({
type,
duration,
resultLength,
Expand All @@ -111,7 +123,7 @@ export class TracingLanguageClient extends LanguageClient {

private fireFailureTraceEvent(type: string, startAt: number, error: any, data?: any): void {
const duration: number = performance.now() - startAt;
requestEventEmitter.fire({
requestEndEventEmitter.fire({
type,
duration,
error,
Expand Down
3 changes: 2 additions & 1 deletion src/apiManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Commands } from "./commands";
import { Emitter } from "vscode-languageclient";
import { ServerMode } from "./settings";
import { registerHoverCommand } from "./hoverAction";
import { onDidRequestEnd } from "./TracingLanguageClient";
import { onDidRequestEnd, onWillRequestStart } from "./TracingLanguageClient";

class ApiManager {

Expand Down Expand Up @@ -64,6 +64,7 @@ class ApiManager {
onDidServerModeChange,
onDidProjectsImport,
serverReady,
onWillRequestStart,
onDidRequestEnd,
trackEvent: traceEvent,
onDidSourceInvalidate: this.sourceInvalidatedEventEmitter.event,
Expand Down
11 changes: 9 additions & 2 deletions src/extension.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface TraceEvent {
/**
* Time (in milliseconds) taken to process a request.
*/
duration: number;
duration?: number;
/**
* Error that occurs while processing a request.
*/
Expand All @@ -107,7 +107,7 @@ export interface SourceInvalidatedEvent {
affectedEditorDocuments?: Uri[];
}

export const extensionApiVersion = '0.11';
export const extensionApiVersion = '0.12';

export interface ExtensionAPI {
readonly apiVersion: string;
Expand Down Expand Up @@ -153,6 +153,13 @@ export interface ExtensionAPI {
*/
readonly serverReady: () => Promise<boolean>;

/**
* An event that's fired when a request is about to send to language server.
* @since API version 0.12
* @since extension version 1.23.0
*/
readonly onWillRequestStart: Event<TraceEvent>;

/**
* An event that's fired when a request has been responded.
* @since API version 0.8
Expand Down