Skip to content

Commit

Permalink
add telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrivachev committed Oct 28, 2021
1 parent 4668c12 commit 66a03ed
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
14 changes: 14 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"vscode": "^1.52.0"
},
"dependencies": {
"vscode-extension-telemetry": "^0.4.2",
"vscode-languageclient": "^7.0.0"
},
"devDependencies": {
"@google-cloud/bigquery": "^5.9.1",
"@types/vscode": "^1.61.0",
"@vscode/test-electron": "^1.6.2",
"@google-cloud/bigquery": "^5.9.1"
"@vscode/test-electron": "^1.6.2"
}
}
33 changes: 33 additions & 0 deletions client/src/TelemetryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path = require('path');
import * as fs from 'fs';
import { ExtensionContext } from 'vscode';
import TelemetryReporter from 'vscode-extension-telemetry';

export class TelemetryClient {
private static client: TelemetryReporter;

static sendEvent(eventName: string, properties?: { [key: string]: string }): void {
if (TelemetryClient.client) {
TelemetryClient.client.sendTelemetryEvent(eventName, properties);
}
}

static sendException(error: Error, properties?: { [key: string]: string }) {
if (TelemetryClient.client) {
TelemetryClient.client.sendTelemetryException(error, properties);
}
}

static activate(context: ExtensionContext): TelemetryReporter {
var extensionPath = path.join(context.extensionPath, 'package.json');
var packageJson = JSON.parse(fs.readFileSync(extensionPath, 'utf8'));

// const packageJson = extensions.getExtension('dbt-language-server').packageJSON;
if (packageJson.name && packageJson.version && packageJson.aiKey) {
TelemetryClient.client = new TelemetryReporter(packageJson.name, packageJson.version, packageJson.aiKey);
} else {
console.log('Telemetry was not activated');
}
return TelemetryClient.client;
}
}
16 changes: 15 additions & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LanguageClient, LanguageClientOptions, ServerOptions, State, TransportK
import { ProgressHandler } from './ProgressHandler';
import { PythonExtension } from './PythonExtension';
import SqlPreviewContentProvider from './SqlPreviewContentProvider';
import { TelemetryClient } from './TelemetryClient';

let client: LanguageClient;

Expand All @@ -27,7 +28,6 @@ export function activate(context: ExtensionContext): void {
};

const clientOptions: LanguageClientOptions = {
// Register the server for sql documents
documentSelector: [
{ scheme: 'file', language: 'sql' },
{ scheme: 'file', language: 'jinja-sql' },
Expand All @@ -44,8 +44,15 @@ export function activate(context: ExtensionContext): void {

const progressHandler = new ProgressHandler();
progressHandler.begin();

client.onTelemetry(([name, properties]) => {
TelemetryClient.sendEvent(name, properties);
});

client.onDidChangeState(e => {
if (e.newState === State.Running) {
TelemetryClient.sendEvent('activate');

client.onNotification('custom/updateQueryPreview', ([uri, text]) => {
SqlPreviewContentProvider.update(uri, text);
});
Expand All @@ -63,6 +70,12 @@ export function activate(context: ExtensionContext): void {
}
});

client.onReady().catch(reason => {
if (reason && reason.name && reason.message) {
TelemetryClient.sendException(reason);
}
});

window.onDidChangeActiveTextEditor(e => {
if (!e || e.document.uri.toString() === SqlPreviewContentProvider.uri.toString()) {
return;
Expand Down Expand Up @@ -98,6 +111,7 @@ export function activate(context: ExtensionContext): void {
});
commands.executeCommand('editor.action.triggerParameterHints');
}),
TelemetryClient.activate(context),
);

// Start the client. This will also launch the server
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
]
}
},
"aiKey": "0a32611a-7944-4c4d-93c9-5c260a61395b",
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -b",
Expand Down
6 changes: 6 additions & 0 deletions server/src/LspServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ResponseError,
SignatureHelp,
SignatureHelpParams,
TelemetryEventNotification,
TextDocumentSyncKind,
_Connection,
} from 'vscode-languageserver';
Expand Down Expand Up @@ -93,9 +94,14 @@ export class LspServer {
this.updateModels();
}

sendTelemetry(name: string, properties?: { [key: string]: string }): void {
this.connection.sendNotification(TelemetryEventNotification.type, [name, properties]);
}

async startDbtRpc(): Promise<void> {
try {
await this.dbtServer.startDbtRpc(() => this.connection.sendRequest('custom/getPython'));
this.sendTelemetry('log', { dbt_version: this.dbtServer.dbtVersion ?? 'undefined', python: this.dbtServer.python ?? 'undefined' });
} catch (e) {
console.log(e);
this.connection.window.showErrorMessage(
Expand Down

0 comments on commit 66a03ed

Please sign in to comment.