-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[experiments] Add abstraction for configcat to work in self-hosted #10807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
components/gitpod-protocol/src/experiments/configcat-server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { Client } from "./types"; | ||
import * as configcat from "configcat-node"; | ||
import { LogLevel } from "configcat-common"; | ||
import { ConfigCatClient } from "./configcat"; | ||
import { newAlwaysReturningDefaultValueClient } from "./always-default"; | ||
|
||
let client: Client | undefined; | ||
|
||
export function getExperimentsClientForBackend(): Client { | ||
// We have already instantiated a client, we can just re-use it. | ||
if (client !== undefined) { | ||
return client; | ||
} | ||
|
||
// Retrieve SDK key from ENV Variable | ||
const sdkKey = process.env.CONFIGCAT_SDK_KEY; | ||
|
||
// Self-hosted installations do not set the ConfigCat SDK key, so always use a client which returns the default value. | ||
if (sdkKey === undefined || sdkKey === "") { | ||
client = newAlwaysReturningDefaultValueClient(); | ||
return client; | ||
} | ||
|
||
const configCatClient = configcat.createClient(sdkKey, { | ||
logger: configcat.createConsoleLogger(LogLevel.Error), | ||
maxInitWaitTimeSeconds: 0, | ||
}); | ||
|
||
client = new ConfigCatClient(configCatClient); | ||
return client; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { Attributes, Client } from "./types"; | ||
import { User } from "configcat-common/lib/RolloutEvaluator"; | ||
import { IConfigCatClient } from "configcat-common/lib/ConfigCatClient"; | ||
|
||
export const PROJECT_ID_ATTRIBUTE = "project_id"; | ||
export const TEAM_ID_ATTRIBUTE = "team_id"; | ||
export const TEAM_IDS_ATTRIBUTE = "team_ids"; | ||
export const TEAM_NAME_ATTRIBUTE = "team_name"; | ||
export const TEAM_NAMES_ATTRIBUTE = "team_names"; | ||
|
||
export class ConfigCatClient implements Client { | ||
private client: IConfigCatClient; | ||
|
||
constructor(cc: IConfigCatClient) { | ||
this.client = cc; | ||
} | ||
|
||
getValueAsync<T>(experimentName: string, defaultValue: T, attributes: Attributes): Promise<T> { | ||
return this.client.getValueAsync(experimentName, defaultValue, attributesToUser(attributes)); | ||
} | ||
|
||
dispose(): void { | ||
return this.client.dispose(); | ||
} | ||
} | ||
|
||
export function attributesToUser(attributes: Attributes): User { | ||
const userId = attributes.userId || ""; | ||
const email = attributes.email || ""; | ||
|
||
const custom: { [key: string]: string } = {}; | ||
if (attributes.projectId) { | ||
custom[PROJECT_ID_ATTRIBUTE] = attributes.projectId; | ||
} | ||
if (attributes.teamId) { | ||
custom[TEAM_ID_ATTRIBUTE] = attributes.teamId; | ||
} | ||
if (attributes.teamName) { | ||
custom[TEAM_NAME_ATTRIBUTE] = attributes.teamName; | ||
} | ||
if (attributes.teams) { | ||
custom[TEAM_NAMES_ATTRIBUTE] = attributes.teams.map((t) => t.name).join(","); | ||
custom[TEAM_IDS_ATTRIBUTE] = attributes.teams.map((t) => t.id).join(","); | ||
} | ||
|
||
return new User(userId, email, "", custom); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { Team } from "../teams-projects-protocol"; | ||
|
||
// Attributes define attributes which can be used to segment audiences. | ||
// Set the attributes which you want to use to group audiences into. | ||
export interface Attributes { | ||
userId?: string; | ||
email?: string; | ||
|
||
// Currently selected Gitpod Project ID | ||
projectId?: string; | ||
|
||
// Currently selected Gitpod Team ID | ||
teamId?: string; | ||
// Currently selected Gitpod Team Name | ||
teamName?: string; | ||
|
||
// All the Gitpod Teams that the user is a member (or owner) of | ||
teams?: Array<Team>; | ||
} | ||
|
||
export interface Client { | ||
getValueAsync<T>(experimentName: string, defaultValue: T, attributes: Attributes): Promise<T>; | ||
|
||
// dispose will dispose of the client, no longer retrieving flags | ||
dispose(): void; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jankeromnes I've pulled in your change into my refactor as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Is this ready for review?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is :)