Skip to content

Commit

Permalink
Merge pull request #650 from Shopify/vs/send_code_lens_telemetry
Browse files Browse the repository at this point in the history
Send code lens telemetry
  • Loading branch information
vinistock authored Jun 30, 2023
2 parents 85e1048 + 0e23b63 commit da04c43
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
19 changes: 18 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export default class Client implements ClientInterface {
})
);

this.telemetry.serverVersion = await this.getServerVersion();
await this.client.start();
await this.determineFormatter();

Expand Down Expand Up @@ -621,6 +622,21 @@ export default class Client implements ClientInterface {
});
}

private async getServerVersion(): Promise<string> {
const result = await asyncExec(
`BUNDLE_GEMFILE=${path.join(
this.workingFolder,
"Gemfile"
)} bundle exec ruby -e "require 'ruby-lsp'; print RubyLsp::VERSION"`,
{
cwd: this.workingFolder,
env: this.ruby.env,
}
);

return result.stdout;
}

// If the `.git` folder exists and `.git/rebase-merge` or `.git/rebase-apply` exists, then we're in the middle of a
// rebase
private rebaseInProgress() {
Expand All @@ -633,7 +649,8 @@ export default class Client implements ClientInterface {
);
}

private openLink(link: string) {
private async openLink(link: string) {
await this.telemetry.sendCodeLensEvent("link");
vscode.env.openExternal(vscode.Uri.parse(link));
}
}
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export async function activate(context: vscode.ExtensionContext) {
testController = new TestController(
context,
vscode.workspace.workspaceFolders![0].uri.fsPath,
ruby
ruby,
telemetry
);

client = new Client(context, telemetry, ruby, testController);
Expand Down
12 changes: 11 additions & 1 deletion src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export interface ConfigurationEvent {
value: string;
}

export type TelemetryEvent = RequestEvent | ConfigurationEvent;
export interface CodeLensEvent {
type: "test" | "debug" | "test_in_terminal" | "link";
lspVersion: string;
}

export type TelemetryEvent = RequestEvent | ConfigurationEvent | CodeLensEvent;

const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;

Expand All @@ -35,6 +40,7 @@ class DevelopmentApi implements TelemetryApi {
}

export class Telemetry {
public serverVersion?: string;
private api?: TelemetryApi;
private context: vscode.ExtensionContext;

Expand Down Expand Up @@ -103,6 +109,10 @@ export class Telemetry {
);
}

async sendCodeLensEvent(type: CodeLensEvent["type"]) {
await this.sendEvent({ type, lspVersion: this.serverVersion! });
}

private async initialize(): Promise<boolean> {
try {
if (!this.api) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/suite/telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TelemetryApi,
TelemetryEvent,
ConfigurationEvent,
CodeLensEvent,
} from "../../telemetry";

class FakeApi implements TelemetryApi {
Expand Down Expand Up @@ -98,4 +99,25 @@ suite("Telemetry", () => {
assert.strictEqual(typeof (event as ConfigurationEvent).value, "string");
});
});

test("Send code lens event includes configured server version", async () => {
const api = new FakeApi();
const telemetry = new Telemetry(
{
extensionMode: vscode.ExtensionMode.Production,
globalState: {
get: () => undefined,
update: () => Promise.resolve(),
} as unknown,
} as vscode.ExtensionContext,
api
);

telemetry.serverVersion = "1.0.0";
await telemetry.sendCodeLensEvent("test");

const codeLensEvent = api.sentEvents[0] as CodeLensEvent;
assert.strictEqual(codeLensEvent.type, "test");
assert.strictEqual(codeLensEvent.lspVersion, "1.0.0");
});
});
16 changes: 14 additions & 2 deletions src/testController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CodeLens } from "vscode-languageclient/node";

import { Ruby } from "./ruby";
import { Command } from "./status";
import { Telemetry } from "./telemetry";

const asyncExec = promisify(exec);

Expand All @@ -18,14 +19,17 @@ export class TestController {
private workingFolder: string;
private terminal: vscode.Terminal | undefined;
private ruby: Ruby;
private telemetry: Telemetry;

constructor(
context: vscode.ExtensionContext,
workingFolder: string,
ruby: Ruby
ruby: Ruby,
telemetry: Telemetry
) {
this.workingFolder = workingFolder;
this.ruby = ruby;
this.telemetry = telemetry;

this.testController = vscode.tests.createTestController(
"rubyTests",
Expand Down Expand Up @@ -141,7 +145,13 @@ export class TestController {
});
}

private runTestInTerminal(_path: string, _name: string, command: string) {
private async runTestInTerminal(
_path: string,
_name: string,
command: string
) {
await this.telemetry.sendCodeLensEvent("test_in_terminal");

if (this.terminal === undefined) {
this.terminal = vscode.window.createTerminal({ name: "Run test" });
}
Expand All @@ -153,6 +163,7 @@ export class TestController {
request: vscode.TestRunRequest,
_token: vscode.CancellationToken
) {
await this.telemetry.sendCodeLensEvent("debug");
const run = this.testController.createTestRun(request, undefined, true);
const test = request.include![0];

Expand All @@ -166,6 +177,7 @@ export class TestController {
request: vscode.TestRunRequest,
token: vscode.CancellationToken
) {
await this.telemetry.sendCodeLensEvent("test");
const run = this.testController.createTestRun(request, undefined, true);
const queue: vscode.TestItem[] = [];

Expand Down

0 comments on commit da04c43

Please sign in to comment.