Skip to content

Commit 5ea73bd

Browse files
committed
Add experiments using ConfigCat with PortsView
1 parent cf7bec5 commit 5ea73bd

File tree

6 files changed

+146
-14
lines changed

6 files changed

+146
-14
lines changed

extensions/gitpod-shared/src/analytics.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ export type GitpodAnalyticsEvent =
3838
action: 'share' | 'stop-sharing' | 'stop' | 'snapshot' | 'extend-timeout';
3939
}> |
4040
GAET<'vscode_execute_command_gitpod_ports', {
41-
action: 'private' | 'public' | 'preview' | 'openBrowser';
41+
action: 'private' | 'public' | 'preview' | 'openBrowser' | 'urlCopy';
4242
isWebview?: boolean;
43+
userOverride?: string; // 'true' | 'false'
4344
}> |
4445
GAET<'vscode_execute_command_gitpod_config', {
4546
action: 'remove' | 'add';

extensions/gitpod-shared/src/features.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require('reflect-metadata');
88
import { GitpodClient, GitpodServer, GitpodServiceImpl, WorkspaceInstanceUpdateListener } from '@gitpod/gitpod-protocol/lib/gitpod-service';
99
import { JsonRpcProxyFactory } from '@gitpod/gitpod-protocol/lib/messaging/proxy-factory';
1010
import { NavigatorContext, User } from '@gitpod/gitpod-protocol/lib/protocol';
11+
import { Team } from '@gitpod/gitpod-protocol/lib/teams-projects-protocol';
1112
import { ErrorCodes } from '@gitpod/gitpod-protocol/lib/messaging/error';
1213
import { GitpodHostUrl } from '@gitpod/gitpod-protocol/lib/util/gitpod-host-url';
1314
import { ControlServiceClient } from '@gitpod/supervisor-api-grpc/lib/control_grpc_pb';
@@ -70,7 +71,7 @@ export class SupervisorConnection {
7071
}
7172
}
7273

73-
type UsedGitpodFunction = ['getWorkspace', 'openPort', 'stopWorkspace', 'setWorkspaceTimeout', 'getWorkspaceTimeout', 'getLoggedInUser', 'takeSnapshot', 'waitForSnapshot', 'controlAdmission', 'sendHeartBeat', 'trackEvent'];
74+
type UsedGitpodFunction = ['getWorkspace', 'openPort', 'stopWorkspace', 'setWorkspaceTimeout', 'getWorkspaceTimeout', 'getLoggedInUser', 'takeSnapshot', 'waitForSnapshot', 'controlAdmission', 'sendHeartBeat', 'trackEvent', 'getTeams'];
7475
type Union<Tuple extends any[], Union = never> = Tuple[number] | Union;
7576
export type GitpodConnection = Omit<GitpodServiceImpl<GitpodClient, GitpodServer>, 'server'> & {
7677
server: Pick<GitpodServer, Union<UsedGitpodFunction>>;
@@ -93,6 +94,7 @@ export class GitpodExtensionContext implements vscode.ExtensionContext {
9394
readonly info: WorkspaceInfoResponse,
9495
readonly owner: Promise<User>,
9596
readonly user: Promise<User>,
97+
readonly userTeams: Promise<Team[]>,
9698
readonly instanceListener: Promise<WorkspaceInstanceUpdateListener>,
9799
readonly workspaceOwned: Promise<boolean>,
98100
readonly logger: Log,
@@ -241,7 +243,7 @@ export async function createGitpodExtensionContext(context: vscode.ExtensionCont
241243
const gitpodApi = workspaceInfo.getGitpodApi()!;
242244

243245
const factory = new JsonRpcProxyFactory<GitpodServer>();
244-
const gitpodFunctions: UsedGitpodFunction = ['getWorkspace', 'openPort', 'stopWorkspace', 'setWorkspaceTimeout', 'getWorkspaceTimeout', 'getLoggedInUser', 'takeSnapshot', 'waitForSnapshot', 'controlAdmission', 'sendHeartBeat', 'trackEvent'];
246+
const gitpodFunctions: UsedGitpodFunction = ['getWorkspace', 'openPort', 'stopWorkspace', 'setWorkspaceTimeout', 'getWorkspaceTimeout', 'getLoggedInUser', 'takeSnapshot', 'waitForSnapshot', 'controlAdmission', 'sendHeartBeat', 'trackEvent', 'getTeams'];
245247
const gitpodService: GitpodConnection = new GitpodServiceImpl<GitpodClient, GitpodServer>(factory.createProxy()) as any;
246248
const gitpodScopes = new Set<string>([
247249
'resource:workspace::' + workspaceId + '::get/update',
@@ -302,6 +304,7 @@ export async function createGitpodExtensionContext(context: vscode.ExtensionCont
302304
}
303305
return vscode.commands.executeCommand('gitpod.api.getLoggedInUser') as typeof pendingGetOwner;
304306
})();
307+
const pendingGetUserTeams = gitpodService.server.getTeams();
305308
const pendingInstanceListener = gitpodService.listenToInstance(workspaceId);
306309
const pendingWorkspaceOwned = (async () => {
307310
const owner = await pendingGetOwner;
@@ -325,6 +328,7 @@ export async function createGitpodExtensionContext(context: vscode.ExtensionCont
325328
workspaceInfo,
326329
pendingGetOwner,
327330
pendingGetUser,
331+
pendingGetUserTeams,
328332
pendingInstanceListener,
329333
pendingWorkspaceOwned,
330334
logger,

extensions/gitpod-web/package.json

+2
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",
+89
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/out/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, custom?: { [key: string]: 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, undefined, custom) : 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, custom?: { [key: string]: 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, undefined, custom) : 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

+29-11
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) {
@@ -497,6 +498,10 @@ class GitpodPortViewProvider implements vscode.WebviewViewProvider {
497498
if (!port) { return; }
498499
if (message.command === 'urlCopy' && port.status.exposed) {
499500
await vscode.env.clipboard.writeText(port.status.exposed.url);
501+
this.context.fireAnalyticsEvent({
502+
eventName: 'vscode_execute_command_gitpod_ports',
503+
properties: { action: 'urlCopy', isWebview: true, userOverride: String(isUserOverrideSetting('gitpod.experimental.portsView.enabled')) }
504+
});
500505
return;
501506
}
502507
vscode.commands.executeCommand('gitpod.ports.' + message.command, { port, isWebview: true });
@@ -516,8 +521,20 @@ function getNonce() {
516521

517522
interface PortItem { port: GitpodWorkspacePort; isWebview?: boolean }
518523

519-
function registerPorts(context: GitpodExtensionContext): void {
520-
const isPortsViewExperimentEnable = vscode.workspace.getConfiguration('gitpod.experimental.portsView').get<boolean>('enabled');
524+
async function registerPorts(context: GitpodExtensionContext): Promise<void> {
525+
526+
const isSaaSGitpod = context.info.getGitpodHost() === 'https://gitpod.io';
527+
const packageJSON = context.extension.packageJSON;
528+
const experiments = new ExperimentalSettings(process.env['GITPOD_CONFIGCAT_SDK_KEY'] ?? packageJSON.configcatKey, packageJSON.version, context.logger);
529+
context.subscriptions.push(experiments);
530+
async function getPortsViewExperimentEnable(): Promise<boolean> {
531+
const ret = isSaaSGitpod
532+
? (await experiments.get<boolean>('gitpod.experimental.portsView.enabled', (await context.user).id, { team_ids: (await context.userTeams).map(e => e.id).join(','), }))!
533+
: vscode.workspace.getConfiguration('gitpod').get<boolean>('experimental.portsView.enabled')!;
534+
return ret;
535+
}
536+
537+
const isPortsViewExperimentEnable = await getPortsViewExperimentEnable();
521538

522539
const portMap = new Map<number, GitpodWorkspacePort>();
523540
const tunnelMap = new Map<number, vscode.TunnelDescription>();
@@ -577,6 +594,7 @@ function registerPorts(context: GitpodExtensionContext): void {
577594
}
578595
});
579596
}
597+
580598
context.subscriptions.push(observePortsStatus());
581599
context.subscriptions.push(vscode.commands.registerCommand('gitpod.resolveExternalPort', (portNumber: number) => {
582600
// eslint-disable-next-line no-async-promise-executor
@@ -616,14 +634,14 @@ function registerPorts(context: GitpodExtensionContext): void {
616634
context.subscriptions.push(vscode.commands.registerCommand('gitpod.ports.makePrivate', ({ port, isWebview }: PortItem) => {
617635
context.fireAnalyticsEvent({
618636
eventName: 'vscode_execute_command_gitpod_ports',
619-
properties: { action: 'private', isWebview: !!isWebview }
637+
properties: { action: 'private', isWebview: !!isWebview, userOverride: String(isUserOverrideSetting('gitpod.experimental.portsView.enabled')) }
620638
});
621639
return port.setPortVisibility('private');
622640
}));
623641
context.subscriptions.push(vscode.commands.registerCommand('gitpod.ports.makePublic', ({ port, isWebview }: PortItem) => {
624642
context.fireAnalyticsEvent({
625643
eventName: 'vscode_execute_command_gitpod_ports',
626-
properties: { action: 'public', isWebview: !!isWebview }
644+
properties: { action: 'public', isWebview: !!isWebview, userOverride: String(isUserOverrideSetting('gitpod.experimental.portsView.enabled')) }
627645
});
628646
return port.setPortVisibility('public');
629647
}));
@@ -636,14 +654,14 @@ function registerPorts(context: GitpodExtensionContext): void {
636654
context.subscriptions.push(vscode.commands.registerCommand('gitpod.ports.preview', ({ port, isWebview }: PortItem) => {
637655
context.fireAnalyticsEvent({
638656
eventName: 'vscode_execute_command_gitpod_ports',
639-
properties: { action: 'preview', isWebview: !!isWebview }
657+
properties: { action: 'preview', isWebview: !!isWebview, userOverride: String(isUserOverrideSetting('gitpod.experimental.portsView.enabled')) }
640658
});
641659
return openPreview(port);
642660
}));
643661
context.subscriptions.push(vscode.commands.registerCommand('gitpod.ports.openBrowser', ({ port, isWebview }: PortItem) => {
644662
context.fireAnalyticsEvent({
645663
eventName: 'vscode_execute_command_gitpod_ports',
646-
properties: { action: 'openBrowser', isWebview: !!isWebview }
664+
properties: { action: 'openBrowser', isWebview: !!isWebview, userOverride: String(isUserOverrideSetting('gitpod.experimental.portsView.enabled')) }
647665
});
648666
return openExternal(port);
649667
}));
@@ -657,7 +675,7 @@ function registerPorts(context: GitpodExtensionContext): void {
657675

658676
const portsStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
659677
context.subscriptions.push(portsStatusBarItem);
660-
function updateStatusBar(): void {
678+
async function updateStatusBar(): Promise<void> {
661679
const exposedPorts: number[] = [];
662680

663681
for (const port of portMap.values()) {
@@ -679,8 +697,8 @@ function registerPorts(context: GitpodExtensionContext): void {
679697

680698
portsStatusBarItem.text = text;
681699
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';
700+
701+
portsStatusBarItem.command = (await getPortsViewExperimentEnable()) ? 'gitpod.portsView.focus' : 'gitpod.ports.reveal';
684702
portsStatusBarItem.show();
685703
}
686704
updateStatusBar();
@@ -820,11 +838,11 @@ function registerPorts(context: GitpodExtensionContext): void {
820838
vscode.commands.executeCommand('gitpod.api.connectLocalApp', apiPort);
821839
}
822840
}));
823-
vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => {
841+
vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => {
824842
if (!e.affectsConfiguration('gitpod.experimental.portsView.enabled')) {
825843
return;
826844
}
827-
const isPortsViewExperimentEnable = vscode.workspace.getConfiguration('gitpod.experimental.portsView').get<boolean>('enabled');
845+
const isPortsViewExperimentEnable = await getPortsViewExperimentEnable();
828846
vscode.commands.executeCommand('setContext', 'gitpod.portsView.visible', isPortsViewExperimentEnable);
829847
gitpodWorkspaceTreeDataProvider.updateIsPortsViewExperimentEnable(isPortsViewExperimentEnable ?? false);
830848
updateStatusBar();

extensions/yarn.lock

+18
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)