Milan Pavlik 50e09cce76
[experiments] Add fields to logcontext (#16841)
* Fix

* Fix

* Fix

* Fix

* Fix

* [public-api] Use context logger

* fix

* Fix

* fix

* Fix

* fix

* fix

* fix

* [experiments] Add fields to logcontext

* fix
2023-03-21 09:23:13 +01:00

104 lines
2.9 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"
"fmt"
configcat "github.com/configcat/go-sdk/v7"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/sirupsen/logrus"
)
const (
userIDAttribute = "user_id"
projectIDAttribute = "project_id"
teamIDAttribute = "team_id"
teamNameAttribute = "team_name"
vscodeClientIDAttribute = "vscode_client_id"
)
func newConfigCatClient(config configcat.Config) *configCatClient {
return &configCatClient{
client: configcat.NewCustomClient(config),
}
}
var _ Client = (*configCatClient)(nil)
type configCatClient struct {
client *configcat.Client
}
func (c *configCatClient) GetBoolValue(ctx context.Context, experimentName string, defaultValue bool, attributes Attributes) bool {
value := c.client.GetBoolValue(experimentName, defaultValue, attributesToUser(attributes))
log.AddFields(ctx, logField(experimentName, value))
return value
}
func (c *configCatClient) GetIntValue(ctx context.Context, experimentName string, defaultValue int, attributes Attributes) int {
value := c.client.GetIntValue(experimentName, defaultValue, attributesToUser(attributes))
log.AddFields(ctx, logField(experimentName, value))
return value
}
func (c *configCatClient) GetFloatValue(ctx context.Context, experimentName string, defaultValue float64, attributes Attributes) float64 {
value := c.client.GetFloatValue(experimentName, defaultValue, attributesToUser(attributes))
log.AddFields(ctx, logField(experimentName, value))
return value
}
func (c *configCatClient) GetStringValue(ctx context.Context, experimentName string, defaultValue string, attributes Attributes) string {
value := c.client.GetStringValue(experimentName, defaultValue, attributesToUser(attributes))
log.AddFields(ctx, logField(experimentName, value))
return value
}
func attributesToUser(attributes Attributes) *configcat.UserData {
custom := make(map[string]string)
if attributes.UserID != "" {
custom[userIDAttribute] = attributes.UserID
}
if attributes.TeamID != "" {
custom[teamIDAttribute] = attributes.TeamID
}
if attributes.TeamName != "" {
custom[teamNameAttribute] = attributes.TeamName
}
if attributes.ProjectID != "" {
custom[projectIDAttribute] = attributes.ProjectID
}
if attributes.VSCodeClientID != "" {
custom[vscodeClientIDAttribute] = attributes.VSCodeClientID
}
return &configcat.UserData{
Identifier: attributes.UserID,
Email: attributes.UserEmail,
Country: "",
Custom: custom,
}
}
type configCatLogger struct {
*logrus.Entry
}
func (l *configCatLogger) GetLevel() configcat.LogLevel {
return configcat.LogLevelError
}
func logField(experimentName, value interface{}) logrus.Fields {
return logrus.Fields{
fmt.Sprintf("experiments.%s", experimentName): value,
}
}