forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartupTelemetry.ts
More file actions
153 lines (143 loc) · 6.86 KB
/
startupTelemetry.ts
File metadata and controls
153 lines (143 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { IWorkspaceService } from './common/application/types';
import { isTestExecution } from './common/constants';
import { DeprecatePythonPath } from './common/experiments/groups';
import { traceError } from './common/logger';
import { ITerminalHelper } from './common/terminal/types';
import {
IConfigurationService,
IExperimentsManager,
IInterpreterPathService,
InspectInterpreterSettingType,
Resource,
} from './common/types';
import { IStopWatch } from './common/utils/stopWatch';
import {
AutoSelectionRule,
IInterpreterAutoSelectionRule,
IInterpreterAutoSelectionService,
} from './interpreter/autoSelection/types';
import { ICondaService, IInterpreterService } from './interpreter/contracts';
import { IServiceContainer } from './ioc/types';
import { PythonEnvironment } from './pythonEnvironments/info';
import { sendTelemetryEvent } from './telemetry';
import { EventName } from './telemetry/constants';
import { EditorLoadTelemetry } from './telemetry/types';
import { IStartupDurations } from './types';
export async function sendStartupTelemetry(
activatedPromise: Promise<any>,
durations: IStartupDurations,
stopWatch: IStopWatch,
serviceContainer: IServiceContainer,
) {
if (isTestExecution()) {
return;
}
try {
await activatedPromise;
durations.totalNonBlockingActivateTime = stopWatch.elapsedTime - durations.startActivateTime;
const props = await getActivationTelemetryProps(serviceContainer);
sendTelemetryEvent(EventName.EDITOR_LOAD, durations, props);
} catch (ex) {
traceError('sendStartupTelemetry() failed.', ex);
}
}
export async function sendErrorTelemetry(
ex: Error,
durations: IStartupDurations,
serviceContainer?: IServiceContainer,
) {
try {
let props: any = {};
if (serviceContainer) {
try {
props = await getActivationTelemetryProps(serviceContainer);
} catch (ex) {
traceError('getActivationTelemetryProps() failed.', ex);
}
}
sendTelemetryEvent(EventName.EDITOR_LOAD, durations, props, ex);
} catch (exc2) {
traceError('sendErrorTelemetry() failed.', exc2);
}
}
function isUsingGlobalInterpreterInWorkspace(currentPythonPath: string, serviceContainer: IServiceContainer): boolean {
const service = serviceContainer.get<IInterpreterAutoSelectionService>(IInterpreterAutoSelectionService);
const globalInterpreter = service.getAutoSelectedInterpreter(undefined);
if (!globalInterpreter) {
return false;
}
return currentPythonPath === globalInterpreter.path;
}
export function hasUserDefinedPythonPath(resource: Resource, serviceContainer: IServiceContainer) {
const abExperiments = serviceContainer.get<IExperimentsManager>(IExperimentsManager);
const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
const interpreterPathService = serviceContainer.get<IInterpreterPathService>(IInterpreterPathService);
let settings: InspectInterpreterSettingType;
if (abExperiments.inExperiment(DeprecatePythonPath.experiment)) {
settings = interpreterPathService.inspect(resource);
} else {
settings = workspaceService.getConfiguration('python', resource)!.inspect<string>('pythonPath')!;
}
abExperiments.sendTelemetryIfInExperiment(DeprecatePythonPath.control);
return (settings.workspaceFolderValue && settings.workspaceFolderValue !== 'python') ||
(settings.workspaceValue && settings.workspaceValue !== 'python') ||
(settings.globalValue && settings.globalValue !== 'python')
? true
: false;
}
function getPreferredWorkspaceInterpreter(resource: Resource, serviceContainer: IServiceContainer) {
const workspaceInterpreterSelector = serviceContainer.get<IInterpreterAutoSelectionRule>(
IInterpreterAutoSelectionRule,
AutoSelectionRule.workspaceVirtualEnvs,
);
const interpreter = workspaceInterpreterSelector.getPreviouslyAutoSelectedInterpreter(resource);
return interpreter ? interpreter.path : undefined;
}
async function getActivationTelemetryProps(serviceContainer: IServiceContainer): Promise<EditorLoadTelemetry> {
// TODO: Not all of this data is showing up in the database...
// TODO: If any one of these parts fails we send no info. We should
// be able to partially populate as much as possible instead
// (through granular try-catch statements).
const terminalHelper = serviceContainer.get<ITerminalHelper>(ITerminalHelper);
const terminalShellType = terminalHelper.identifyTerminalShell();
const condaLocator = serviceContainer.get<ICondaService>(ICondaService);
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
const configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
const mainWorkspaceUri = workspaceService.hasWorkspaceFolders
? workspaceService.workspaceFolders![0].uri
: undefined;
const settings = configurationService.getSettings(mainWorkspaceUri);
const [condaVersion, interpreter, interpreters] = await Promise.all([
condaLocator
.getCondaVersion()
.then((ver) => (ver ? ver.raw : ''))
.catch<string>(() => ''),
interpreterService.getActiveInterpreter().catch<PythonEnvironment | undefined>(() => undefined),
interpreterService.getInterpreters(mainWorkspaceUri).catch<PythonEnvironment[]>(() => []),
]);
const workspaceFolderCount = workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders!.length : 0;
const pythonVersion = interpreter && interpreter.version ? interpreter.version.raw : undefined;
const interpreterType = interpreter ? interpreter.envType : undefined;
const usingUserDefinedInterpreter = hasUserDefinedPythonPath(mainWorkspaceUri, serviceContainer);
const preferredWorkspaceInterpreter = getPreferredWorkspaceInterpreter(mainWorkspaceUri, serviceContainer);
const usingGlobalInterpreter = isUsingGlobalInterpreterInWorkspace(settings.pythonPath, serviceContainer);
const usingAutoSelectedWorkspaceInterpreter = preferredWorkspaceInterpreter
? settings.pythonPath === getPreferredWorkspaceInterpreter(mainWorkspaceUri, serviceContainer)
: false;
const hasPython3 =
interpreters!.filter((item) => (item && item.version ? item.version.major === 3 : false)).length > 0;
return {
condaVersion,
terminal: terminalShellType,
pythonVersion,
interpreterType,
workspaceFolderCount,
hasPython3,
usingUserDefinedInterpreter,
usingAutoSelectedWorkspaceInterpreter,
usingGlobalInterpreter,
};
}