Skip to content

Commit 97902a3

Browse files
committed
Add experiments using ConfigCat with PortsView
1 parent f3fac9c commit 97902a3

File tree

5 files changed

+141
-7
lines changed

5 files changed

+141
-7
lines changed

extensions/gitpod-shared/src/analytics.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export type GitpodAnalyticsEvent =
4949
}> |
5050
GAET<'ide_close_signal', {
5151
clientKind: 'vscode';
52+
}> |
53+
GAET<'vscode_experimental_ports_view', {
54+
enabled: boolean; userOverride: boolean;
5255
}>;
5356

5457
export function registerUsageAnalytics(context: GitpodExtensionContext): void {

extensions/gitpod-web/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"engines": {
1717
"vscode": "^1.58.2"
1818
},
19+
"configcatKey": "WBLaCPtkjkqKHlHedziE9g/LEAOCNkbuUKiqUZAcVg7dw",
1920
"enabledApiProposals": [
2021
"resolvers",
2122
"contribViewsRemote",
@@ -562,6 +563,7 @@
562563
"dependencies": {
563564
"@gitpod/gitpod-protocol": "main",
564565
"@gitpod/supervisor-api-grpc": "main",
566+
"configcat-node": "^8.0.0",
565567
"gitpod-shared": "0.0.1",
566568
"node-fetch": "2.6.7",
567569
"uuid": "8.1.0",
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Gitpod. All rights reserved.
3+
*--------------------------------------------------------------------------------------------*/
4+
5+
import * as vscode from 'vscode';
6+
import * as configcat from 'configcat-node';
7+
import * as configcatcommon from 'configcat-common';
8+
import * as semver from 'semver';
9+
import Log from 'gitpod-shared/src/common/logger';
10+
11+
const EXPERTIMENTAL_SETTINGS = [
12+
'gitpod.experimental.portsView.enabled',
13+
];
14+
15+
export class ExperimentalSettings {
16+
private configcatClient: configcatcommon.IConfigCatClient;
17+
private extensionVersion: semver.SemVer;
18+
19+
constructor(key: string, extensionVersion: string, private logger: Log) {
20+
this.configcatClient = configcat.createClientWithLazyLoad(key, {
21+
logger: {
22+
debug(): void { },
23+
log(): void { },
24+
info(): void { },
25+
warn(message: string): void { logger.warn(`ConfigCat: ${message}`); },
26+
error(message: string): void { logger.error(`ConfigCat: ${message}`); }
27+
},
28+
requestTimeoutMs: 1500,
29+
cacheTimeToLiveSeconds: 60
30+
});
31+
this.extensionVersion = new semver.SemVer(extensionVersion);
32+
}
33+
34+
async get<T>(key: string, userId?: string): Promise<T | undefined> {
35+
const config = vscode.workspace.getConfiguration('gitpod');
36+
const values = config.inspect<T>(key.substring('gitpod.'.length));
37+
if (!values || !EXPERTIMENTAL_SETTINGS.includes(key)) {
38+
this.logger.error(`Cannot get invalid experimental setting '${key}'`);
39+
return values?.globalValue ?? values?.defaultValue;
40+
}
41+
if (this.isPreRelease()) {
42+
// PreRelease versions always have experiments enabled by default
43+
return values.globalValue ?? values.defaultValue;
44+
}
45+
if (values.globalValue !== undefined) {
46+
// User setting have priority over configcat so return early
47+
return values.globalValue;
48+
}
49+
50+
const user = userId ? new configcatcommon.User(userId) : undefined;
51+
const configcatKey = key.replace(/\./g, '_'); // '.' are not allowed in configcat
52+
const experimentValue = (await this.configcatClient.getValueAsync(configcatKey, undefined, user)) as T | undefined;
53+
54+
return experimentValue ?? values.defaultValue;
55+
}
56+
57+
async inspect<T>(key: string, userId?: string): Promise<{ key: string; defaultValue?: T; globalValue?: T; experimentValue?: T } | undefined> {
58+
const config = vscode.workspace.getConfiguration('gitpod');
59+
const values = config.inspect<T>(key.substring('gitpod.'.length));
60+
if (!values || !EXPERTIMENTAL_SETTINGS.includes(key)) {
61+
this.logger.error(`Cannot inspect invalid experimental setting '${key}'`);
62+
return values;
63+
}
64+
65+
const user = userId ? new configcatcommon.User(userId) : undefined;
66+
const configcatKey = key.replace(/\./g, '_'); // '.' are not allowed in configcat
67+
const experimentValue = (await this.configcatClient.getValueAsync(configcatKey, undefined, user)) as T | undefined;
68+
69+
return { key, defaultValue: values.defaultValue, globalValue: values.globalValue, experimentValue };
70+
}
71+
72+
forceRefreshAsync(): Promise<void> {
73+
return this.configcatClient.forceRefreshAsync();
74+
}
75+
76+
private isPreRelease() {
77+
return this.extensionVersion.minor % 2 === 1;
78+
}
79+
80+
dispose(): void {
81+
this.configcatClient.dispose();
82+
}
83+
}
84+
85+
export function isUserOverrideSetting(key: string): boolean {
86+
const config = vscode.workspace.getConfiguration('gitpod');
87+
const values = config.inspect(key.substring('gitpod.'.length));
88+
return values?.globalValue !== undefined;
89+
}

extensions/gitpod-web/src/extension.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { getManifest } from './util/extensionManagmentUtill';
2525
import { GitpodWorkspacePort, PortInfo, iconStatusMap } from './util/port';
2626
import { registerReleaseNotesView, RELEASE_NOTES_LAST_READ_KEY } from './releaseNote';
2727
import { registerWelcomeWalkthroughContribution, WELCOME_WALKTROUGH_KEY } from './welcomeWalktrough';
28+
import { ExperimentalSettings, isUserOverrideSetting } from './experiments';
2829

2930
let gitpodContext: GitpodExtensionContext | undefined;
3031
export async function activate(context: vscode.ExtensionContext) {
@@ -516,8 +517,8 @@ function getNonce() {
516517

517518
interface PortItem { port: GitpodWorkspacePort; isWebview?: boolean }
518519

519-
function registerPorts(context: GitpodExtensionContext): void {
520-
const isPortsViewExperimentEnable = vscode.workspace.getConfiguration('gitpod.experimental.portsView').get<boolean>('enabled');
520+
async function registerPorts(context: GitpodExtensionContext): Promise<void> {
521+
const isPortsViewExperimentEnable = await getPortsViewExperimentEnable();
521522

522523
const portMap = new Map<number, GitpodWorkspacePort>();
523524
const tunnelMap = new Map<number, vscode.TunnelDescription>();
@@ -577,6 +578,27 @@ function registerPorts(context: GitpodExtensionContext): void {
577578
}
578579
});
579580
}
581+
582+
const packageJSON = context.extension.packageJSON;
583+
const experiments = new ExperimentalSettings(packageJSON.configcatKey, packageJSON.version, context.logger);
584+
context.subscriptions.push(experiments);
585+
586+
const isSaaSGitpod = context.info.getGitpodHost() === 'https://gitpod.io';
587+
async function getPortsViewExperimentEnable(): Promise<boolean> {
588+
const isEnabled = isSaaSGitpod
589+
? (await experiments.get<boolean>('gitpod.experimental.portsView.enabled', (await context.user).id))!
590+
: vscode.workspace.getConfiguration('gitpod').get<boolean>('experimental.portsView.enabled')!;
591+
const userOverride = isUserOverrideSetting('gitpod.remote.useLocalApp');
592+
context.fireAnalyticsEvent({
593+
eventName: 'vscode_experimental_ports_view',
594+
properties: {
595+
enabled: isEnabled,
596+
userOverride,
597+
}
598+
});
599+
return isEnabled;
600+
}
601+
580602
context.subscriptions.push(observePortsStatus());
581603
context.subscriptions.push(vscode.commands.registerCommand('gitpod.resolveExternalPort', (portNumber: number) => {
582604
// eslint-disable-next-line no-async-promise-executor
@@ -657,7 +679,7 @@ function registerPorts(context: GitpodExtensionContext): void {
657679

658680
const portsStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
659681
context.subscriptions.push(portsStatusBarItem);
660-
function updateStatusBar(): void {
682+
async function updateStatusBar(): Promise<void> {
661683
const exposedPorts: number[] = [];
662684

663685
for (const port of portMap.values()) {
@@ -679,8 +701,8 @@ function registerPorts(context: GitpodExtensionContext): void {
679701

680702
portsStatusBarItem.text = text;
681703
portsStatusBarItem.tooltip = tooltip;
682-
const isPortsViewExperimentEnable = vscode.workspace.getConfiguration('gitpod.experimental.portsView').get<boolean>('enabled');
683-
portsStatusBarItem.command = isPortsViewExperimentEnable ? 'gitpod.portsView.focus' : 'gitpod.ports.reveal';
704+
705+
portsStatusBarItem.command = (await getPortsViewExperimentEnable()) ? 'gitpod.portsView.focus' : 'gitpod.ports.reveal';
684706
portsStatusBarItem.show();
685707
}
686708
updateStatusBar();
@@ -820,11 +842,11 @@ function registerPorts(context: GitpodExtensionContext): void {
820842
vscode.commands.executeCommand('gitpod.api.connectLocalApp', apiPort);
821843
}
822844
}));
823-
vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => {
845+
vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => {
824846
if (!e.affectsConfiguration('gitpod.experimental.portsView.enabled')) {
825847
return;
826848
}
827-
const isPortsViewExperimentEnable = vscode.workspace.getConfiguration('gitpod.experimental.portsView').get<boolean>('enabled');
849+
const isPortsViewExperimentEnable = await getPortsViewExperimentEnable();
828850
vscode.commands.executeCommand('setContext', 'gitpod.portsView.visible', isPortsViewExperimentEnable);
829851
gitpodWorkspaceTreeDataProvider.updateIsPortsViewExperimentEnable(isPortsViewExperimentEnable ?? false);
830852
updateStatusBar();

extensions/yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,19 @@ [email protected]:
479479
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
480480
integrity sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==
481481

482+
configcat-common@^6.0.0:
483+
version "6.0.0"
484+
resolved "https://registry.yarnpkg.com/configcat-common/-/configcat-common-6.0.0.tgz#ccdb9bdafcb6a89144cac17faaab60ac960fed2a"
485+
integrity sha512-C/lCeTKiFk9kPElRF3f4zIkvVCLKgPJuzrKbIMHCru89mvfH5t4//hZ9TW8wPJOAje6xB6ZALutDiIxggwUvWA==
486+
487+
configcat-node@^8.0.0:
488+
version "8.0.0"
489+
resolved "https://registry.yarnpkg.com/configcat-node/-/configcat-node-8.0.0.tgz#6a7d2072a848552971d91e2e44c424bfda606d21"
490+
integrity sha512-4n4yLMpXWEiB4vmj0HuV3ArgImOEHgT+ZhP+y6N6zdwP1Z4KhQHA3btbDtZbqNw1meaVzhQMjRnpV+k/3Zr8XQ==
491+
dependencies:
492+
configcat-common "^6.0.0"
493+
tunnel "0.0.6"
494+
482495
cookie@^0.4.2:
483496
version "0.4.2"
484497
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
@@ -1673,6 +1686,11 @@ tr46@~0.0.3:
16731686
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
16741687
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
16751688

1689+
1690+
version "0.0.6"
1691+
resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c"
1692+
integrity sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==
1693+
16761694
16771695
version "4.8.2"
16781696
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.2.tgz#e3b33d5ccfb5914e4eeab6699cf208adee3fd790"

0 commit comments

Comments
 (0)