Skip to content
Open
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@
"command": "Tools-for-Solidity.generate.imports_graph",
"title": "Solidity: Generate Imports Graph"
},
{
"command": "Tools-for-Solidity.generate.abstraction_resolved",
"title": "Solidity: Generate Abstraction Resolved File"
},
{
"command": "Tools-for-Solidity.generate.inheritance_graph",
"title": "Solidity: Generate Inheritance Graph"
Expand Down Expand Up @@ -1115,7 +1119,7 @@
"@google-cloud/storage": "^7.11.2",
"@hpcc-js/wasm": "^2.1.0",
"@renovatebot/pep440": "^2.0.0",
"@solidity-parser/parser": "^0.19.0",
"@solidity-parser/parser": "^0.20.1",
"applicationinsights": "^2.9.0",
"camelcase-keys": "^9.1.3",
"crypto": "^1.0.1",
Expand Down
167 changes: 96 additions & 71 deletions src/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ let appInsights = require('applicationinsights');

class TelemetrySender implements vscode.TelemetrySender {
sendEventData(eventName: string, data?: Record<string, any> | undefined): void {
appInsights.defaultClient.trackEvent({
name: eventName,
properties: data
});
if (appInsights.defaultClient) {
appInsights.defaultClient.trackEvent({
name: eventName,
properties: data
});
}
}

sendErrorData(error: Error, data?: Record<string, any> | undefined): void {
appInsights.defaultClient.trackException({
exception: error,
properties: data
});
if (appInsights.defaultClient) {
appInsights.defaultClient.trackException({
exception: error,
properties: data
});
}
}
}

Expand All @@ -28,31 +32,44 @@ export class Analytics {
correctPythonPath: boolean | undefined;
correctSysPath: boolean | undefined;
installation: string | undefined;
private isInitialized: boolean = false;

public initialize(context: vscode.ExtensionContext, installation: string) {
appInsights
.setup(env.TELEMETRY_KEY)
.setAutoCollectRequests(false)
.setAutoCollectPerformance(false)
.setAutoCollectExceptions(false)
.setAutoCollectDependencies(false)
.setAutoDependencyCorrelation(false)
.setAutoCollectConsole(false)
.setUseDiskRetryCaching(true)
.start();
const { userId, sessionId } = appInsights.defaultClient.context.keys;
appInsights.defaultClient.context.tags[userId] = vscode.env.machineId;
appInsights.defaultClient.context.tags[sessionId] = vscode.env.sessionId;

this.context = context;
this.telemetryLogger = vscode.env.createTelemetryLogger(new TelemetrySender());
this.wakeVersion = undefined;
this.correctPythonPath = undefined;
this.correctSysPath = undefined;

this.installation = installation;

context.subscriptions.push(this.telemetryLogger);
try {
// Only initialize Application Insights if we have a valid telemetry key
if (env.TELEMETRY_KEY && env.TELEMETRY_KEY.trim() !== '') {
appInsights
.setup(env.TELEMETRY_KEY)
.setAutoCollectRequests(false)
.setAutoCollectPerformance(false)
.setAutoCollectExceptions(false)
.setAutoCollectDependencies(false)
.setAutoDependencyCorrelation(false)
.setAutoCollectConsole(false)
.setUseDiskRetryCaching(true)
.start();

const { userId, sessionId } = appInsights.defaultClient.context.keys;
appInsights.defaultClient.context.tags[userId] = vscode.env.machineId;
appInsights.defaultClient.context.tags[sessionId] = vscode.env.sessionId;
} else {
console.log('Telemetry key not provided, Application Insights disabled');
}

this.context = context;
this.telemetryLogger = vscode.env.createTelemetryLogger(new TelemetrySender());
this.wakeVersion = undefined;
this.correctPythonPath = undefined;
this.correctSysPath = undefined;
this.installation = installation;
this.isInitialized = true;

context.subscriptions.push(this.telemetryLogger);
} catch (error) {
console.error('Failed to initialize Analytics:', error);
// Don't throw the error - allow extension to continue without telemetry
this.isInitialized = false;
}
}

public setWakeVersion(version: string) {
Expand All @@ -76,57 +93,65 @@ export class Analytics {
}

logEvent(name: string) {
if (this.telemetryLogger === undefined || this.context === undefined) {
console.error('Cannot log event: TelemetryLogger or context is undefined');
if (!this.isInitialized || this.telemetryLogger === undefined || this.context === undefined) {
console.log(`Analytics not initialized, skipping event: ${name}`);
return;
}

this.telemetryLogger.logUsage(name, {
'common.extname': this.context.extension.packageJSON.name as string,
'common.extversion': this.context.extension.packageJSON.version as string,
'common.vscodemachineid': vscode.env.machineId,
'common.vscodeseesionid': vscode.env.sessionId,
'common.vscodeversion': vscode.version,
'common.os': process.platform.toString(),
'common.nodeArch': process.arch,
installation: this.installation,
'wake.version': this.wakeVersion || 'unknown'
});
try {
this.telemetryLogger.logUsage(name, {
'common.extname': this.context.extension.packageJSON.name as string,
'common.extversion': this.context.extension.packageJSON.version as string,
'common.vscodemachineid': vscode.env.machineId,
'common.vscodeseesionid': vscode.env.sessionId,
'common.vscodeversion': vscode.version,
'common.os': process.platform.toString(),
'common.nodeArch': process.arch,
installation: this.installation,
'wake.version': this.wakeVersion || 'unknown'
});
} catch (error) {
console.error('Failed to log event:', error);
}
}

logCrash(event: EventType, error: any) {
if (this.telemetryLogger === undefined || this.context === undefined) {
console.error('Cannot log event: TelemetryLogger or context is undefined');
if (!this.isInitialized || this.telemetryLogger === undefined || this.context === undefined) {
console.log(`Analytics not initialized, skipping crash log: ${event}`);
return;
}

let data: any = {
'common.extname': this.context.extension.packageJSON.name as string,
'common.extversion': this.context.extension.packageJSON.version as string,
'common.vscodemachineid': vscode.env.machineId,
'common.vscodeseesionid': vscode.env.sessionId,
'common.vscodeversion': vscode.version,
'common.os': process.platform.toString(),
'common.nodeArch': process.arch,
installation: this.installation,
'wake.version': this.wakeVersion || 'unknown',
error: error.toString().slice(-8100),
correctPythonPath: this.correctPythonPath,
correctSysPath: this.correctSysPath
};

if (event === EventType.ERROR_SAKE) {
const app = appState.get();
data = {
...data,
'sake.appstate.initializationState': app.initializationState,
'sake.appstate.isAnvilInstalled': app.isAnvilInstalled,
'sake.appstate.isWakeServerRunning': app.isWakeServerRunning,
'sake.appstate.isOpenWorkspace': app.isOpenWorkspace,
'sake.chainstate.currentChainId': extensionState.get().currentChainId
try {
let data: any = {
'common.extname': this.context.extension.packageJSON.name as string,
'common.extversion': this.context.extension.packageJSON.version as string,
'common.vscodemachineid': vscode.env.machineId,
'common.vscodeseesionid': vscode.env.sessionId,
'common.vscodeversion': vscode.version,
'common.os': process.platform.toString(),
'common.nodeArch': process.arch,
installation: this.installation,
'wake.version': this.wakeVersion || 'unknown',
error: error.toString().slice(-8100),
correctPythonPath: this.correctPythonPath,
correctSysPath: this.correctSysPath
};

if (event === EventType.ERROR_SAKE) {
const app = appState.get();
data = {
...data,
'sake.appstate.initializationState': app.initializationState,
'sake.appstate.isAnvilInstalled': app.isAnvilInstalled,
'sake.appstate.isWakeServerRunning': app.isWakeServerRunning,
'sake.appstate.isOpenWorkspace': app.isOpenWorkspace,
'sake.chainstate.currentChainId': extensionState.get().currentChainId
};
}
this.telemetryLogger.logError(event, data);
} catch (error) {
console.error('Failed to log crash:', error);
}
this.telemetryLogger.logError(event, data);
}
}

Expand Down
Loading