2022-07-19 15:54:14 +02:00

84 lines
2.3 KiB
Go

// 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.
package experiments
import (
"context"
configcat "github.com/configcat/go-sdk/v7"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/sirupsen/logrus"
"time"
)
const (
projectIDAttribute = "project_id"
teamIDAttribute = "team_id"
teamNameAttribute = "team_name"
)
func newConfigCatClient(sdkKey string) *configCatClient {
return &configCatClient{
client: configcat.NewCustomClient(configcat.Config{
SDKKey: sdkKey,
PollInterval: 3 * time.Minute,
HTTPTimeout: 3 * time.Second,
Logger: &configCatLogger{log.Log},
}),
}
}
var _ Client = (*configCatClient)(nil)
type configCatClient struct {
client *configcat.Client
}
func (c *configCatClient) GetBoolValue(_ context.Context, experimentName string, defaultValue bool, attributes Attributes) bool {
return c.client.GetBoolValue(experimentName, defaultValue, attributesToUser(attributes))
}
func (c *configCatClient) GetIntValue(_ context.Context, experimentName string, defaultValue int, attributes Attributes) int {
return c.client.GetIntValue(experimentName, defaultValue, attributesToUser(attributes))
}
func (c *configCatClient) GetFloatValue(_ context.Context, experimentName string, defaultValue float64, attributes Attributes) float64 {
return c.client.GetFloatValue(experimentName, defaultValue, attributesToUser(attributes))
}
func (c *configCatClient) GetStringValue(_ context.Context, experimentName string, defaultValue string, attributes Attributes) string {
return c.client.GetStringValue(experimentName, defaultValue, attributesToUser(attributes))
}
func attributesToUser(attributes Attributes) configcat.UserData {
custom := make(map[string]string)
if attributes.TeamID != "" {
custom[teamIDAttribute] = attributes.TeamID
}
if attributes.TeamName != "" {
custom[teamNameAttribute] = attributes.TeamName
}
if attributes.ProjectID != "" {
custom[projectIDAttribute] = attributes.ProjectID
}
return configcat.UserData{
Identifier: attributes.UserID,
Email: attributes.UserEmail,
Country: "",
Custom: custom,
}
}
type configCatLogger struct {
*logrus.Entry
}
func (l *configCatLogger) GetLevel() configcat.LogLevel {
return l.Level
}