Path: blob/main/components/common-go/experiments/configcat.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package experiments56import (7"context"8"fmt"910configcat "github.com/configcat/go-sdk/v7"11"github.com/gitpod-io/gitpod/common-go/log"12"github.com/sirupsen/logrus"13)1415const (16userIDAttribute = "user_id"17projectIDAttribute = "project_id"18teamIDAttribute = "team_id"19teamNameAttribute = "team_name"20vscodeClientIDAttribute = "vscode_client_id"21gitpodHost = "gitpod_host"22component = "component"23)2425func newConfigCatClient(config configcat.Config) *configCatClient {26return &configCatClient{27client: configcat.NewCustomClient(config),28}29}3031var _ Client = (*configCatClient)(nil)3233type configCatClient struct {34client *configcat.Client35}3637func (c *configCatClient) GetBoolValue(ctx context.Context, experimentName string, defaultValue bool, attributes Attributes) bool {38value := c.client.GetBoolValue(experimentName, defaultValue, attributesToUser(attributes))39log.AddFields(ctx, logField(experimentName, value))40return value41}4243func (c *configCatClient) GetIntValue(ctx context.Context, experimentName string, defaultValue int, attributes Attributes) int {44value := c.client.GetIntValue(experimentName, defaultValue, attributesToUser(attributes))45log.AddFields(ctx, logField(experimentName, value))46return value47}4849func (c *configCatClient) GetFloatValue(ctx context.Context, experimentName string, defaultValue float64, attributes Attributes) float64 {50value := c.client.GetFloatValue(experimentName, defaultValue, attributesToUser(attributes))51log.AddFields(ctx, logField(experimentName, value))52return value53}5455func (c *configCatClient) GetStringValue(ctx context.Context, experimentName string, defaultValue string, attributes Attributes) string {56value := c.client.GetStringValue(experimentName, defaultValue, attributesToUser(attributes))57log.AddFields(ctx, logField(experimentName, value))58return value59}6061func attributesToUser(attributes Attributes) *configcat.UserData {62custom := make(map[string]string)6364if attributes.UserID != "" {65custom[userIDAttribute] = attributes.UserID66}6768if attributes.TeamID != "" {69custom[teamIDAttribute] = attributes.TeamID70}7172if attributes.TeamName != "" {73custom[teamNameAttribute] = attributes.TeamName74}7576if attributes.ProjectID != "" {77custom[projectIDAttribute] = attributes.ProjectID78}7980if attributes.VSCodeClientID != "" {81custom[vscodeClientIDAttribute] = attributes.VSCodeClientID82}8384if attributes.GitpodHost != "" {85custom[gitpodHost] = attributes.GitpodHost86}8788if attributes.Component != "" {89custom[component] = attributes.Component90}9192return &configcat.UserData{93Identifier: attributes.UserID,94Email: attributes.UserEmail,95Country: "",96Custom: custom,97}98}99100type configCatLogger struct {101*logrus.Entry102}103104func (l *configCatLogger) GetLevel() configcat.LogLevel {105return configcat.LogLevelError106}107108func (l *configCatLogger) Debugf(format string, args ...interface{}) {}109func (l *configCatLogger) Infof(format string, args ...interface{}) {}110func (l *configCatLogger) Warnf(format string, args ...interface{}) {}111112func logField(experimentName, value interface{}) logrus.Fields {113return logrus.Fields{114fmt.Sprintf("experiments.%s", experimentName): value,115}116}117118119