-
Notifications
You must be signed in to change notification settings - Fork 70
/
split_service.go
83 lines (70 loc) · 2.25 KB
/
split_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package split
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/splitio/go-client/v6/splitio/client"
"github.com/splitio/go-client/v6/splitio/conf"
"github.com/newrelic/newrelic-cli/internal/config"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
)
// Split.io client API keys
// These keys are client-facing and are compiled in via Github secrets and ldflags.
// There is no security risk in exposing these keys, as the only purpose they
// serve is to retrieve experiments and can not be used to modify anything
// within our internal Split.io system.
var (
prodKey = "localhost"
stagingKey = "localhost"
accountID = configAPI.GetActiveProfileAccountID()
splitConfig = conf.Default()
)
type Srvc struct {
client *client.SplitClient
}
// Creates a new instance of a Split Factory
// Using "localhost" as the apiKey allows us to use Split.io
// in localhost mode as defined in their documentation
// If the service is not available we return nil on these methods:
// - Get
// - GetAll
func NewService(region string) (*Srvc, error) {
var apiKey = getAPIKeyByRegion(region)
if region == "localhost" {
apiKey = "localhost"
}
splitConfig.LoggerConfig.Prefix = "[splitio/client]"
splitConfig.LoggerConfig.WarningWriter = config.Logger.WriterLevel(log.DebugLevel)
splitConfig.LoggerConfig.ErrorWriter = config.Logger.WriterLevel(log.DebugLevel)
factory, err := client.NewSplitFactory(apiKey, splitConfig)
if err != nil {
return nil, fmt.Errorf("split SDK init error: %s", err)
}
client := factory.Client()
err = client.BlockUntilReady(10)
if err != nil {
return nil, fmt.Errorf("split SDK timeout: %s", err)
}
return &Srvc{client: client}, nil
}
// Get a treatment from Split.io given the name of the split
func (s *Srvc) Get(split string) string {
if s == nil {
return ""
}
return s.client.Treatment(accountID, split, nil)
}
// Get all treatments from Split.io given a list of splits
func (s *Srvc) GetAll(splits []string) map[string]string {
if s == nil {
return nil
}
log.Debugf("Retrieving treatments for splits: %s", splits)
return s.client.Treatments(accountID, splits, nil)
}
func getAPIKeyByRegion(region string) string {
if strings.EqualFold(region, "staging") {
return stagingKey
}
return prodKey
}