Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/common-go/experiments/configcat.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package experiments
6
7
import (
8
"context"
9
"fmt"
10
11
configcat "github.com/configcat/go-sdk/v7"
12
"github.com/gitpod-io/gitpod/common-go/log"
13
"github.com/sirupsen/logrus"
14
)
15
16
const (
17
userIDAttribute = "user_id"
18
projectIDAttribute = "project_id"
19
teamIDAttribute = "team_id"
20
teamNameAttribute = "team_name"
21
vscodeClientIDAttribute = "vscode_client_id"
22
gitpodHost = "gitpod_host"
23
component = "component"
24
)
25
26
func newConfigCatClient(config configcat.Config) *configCatClient {
27
return &configCatClient{
28
client: configcat.NewCustomClient(config),
29
}
30
}
31
32
var _ Client = (*configCatClient)(nil)
33
34
type configCatClient struct {
35
client *configcat.Client
36
}
37
38
func (c *configCatClient) GetBoolValue(ctx context.Context, experimentName string, defaultValue bool, attributes Attributes) bool {
39
value := c.client.GetBoolValue(experimentName, defaultValue, attributesToUser(attributes))
40
log.AddFields(ctx, logField(experimentName, value))
41
return value
42
}
43
44
func (c *configCatClient) GetIntValue(ctx context.Context, experimentName string, defaultValue int, attributes Attributes) int {
45
value := c.client.GetIntValue(experimentName, defaultValue, attributesToUser(attributes))
46
log.AddFields(ctx, logField(experimentName, value))
47
return value
48
}
49
50
func (c *configCatClient) GetFloatValue(ctx context.Context, experimentName string, defaultValue float64, attributes Attributes) float64 {
51
value := c.client.GetFloatValue(experimentName, defaultValue, attributesToUser(attributes))
52
log.AddFields(ctx, logField(experimentName, value))
53
return value
54
}
55
56
func (c *configCatClient) GetStringValue(ctx context.Context, experimentName string, defaultValue string, attributes Attributes) string {
57
value := c.client.GetStringValue(experimentName, defaultValue, attributesToUser(attributes))
58
log.AddFields(ctx, logField(experimentName, value))
59
return value
60
}
61
62
func attributesToUser(attributes Attributes) *configcat.UserData {
63
custom := make(map[string]string)
64
65
if attributes.UserID != "" {
66
custom[userIDAttribute] = attributes.UserID
67
}
68
69
if attributes.TeamID != "" {
70
custom[teamIDAttribute] = attributes.TeamID
71
}
72
73
if attributes.TeamName != "" {
74
custom[teamNameAttribute] = attributes.TeamName
75
}
76
77
if attributes.ProjectID != "" {
78
custom[projectIDAttribute] = attributes.ProjectID
79
}
80
81
if attributes.VSCodeClientID != "" {
82
custom[vscodeClientIDAttribute] = attributes.VSCodeClientID
83
}
84
85
if attributes.GitpodHost != "" {
86
custom[gitpodHost] = attributes.GitpodHost
87
}
88
89
if attributes.Component != "" {
90
custom[component] = attributes.Component
91
}
92
93
return &configcat.UserData{
94
Identifier: attributes.UserID,
95
Email: attributes.UserEmail,
96
Country: "",
97
Custom: custom,
98
}
99
}
100
101
type configCatLogger struct {
102
*logrus.Entry
103
}
104
105
func (l *configCatLogger) GetLevel() configcat.LogLevel {
106
return configcat.LogLevelError
107
}
108
109
func (l *configCatLogger) Debugf(format string, args ...interface{}) {}
110
func (l *configCatLogger) Infof(format string, args ...interface{}) {}
111
func (l *configCatLogger) Warnf(format string, args ...interface{}) {}
112
113
func logField(experimentName, value interface{}) logrus.Fields {
114
return logrus.Fields{
115
fmt.Sprintf("experiments.%s", experimentName): value,
116
}
117
}
118
119