Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for multiple exporters #2535

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions cmd/relayproxy/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ type Config struct {
// Exporter is the configuration on how to export data
Exporter *ExporterConf `mapstructure:"exporter" koanf:"exporter"`

// Exporters is the exact same things than Exporter but allows to give more than 1 exporter at the time.
Exporters *[]ExporterConf `mapstructure:"exporters" koanf:"exporters"`

// Notifiers is the configuration on where to notify a flag change
Notifiers []NotifierConf `mapstructure:"notifier" koanf:"notifier"`

Expand Down Expand Up @@ -362,6 +365,28 @@ func (c *Config) IsValid() error {
return fmt.Errorf("invalid port %d", c.ListenPort)
}

if err := c.validateRetrievers(); err != nil {
return err
}

if err := c.validateExporters(); err != nil {
return err
}

if err := c.validateNotifiers(); err != nil {
return err
}

if c.LogLevel != "" {
if _, err := zapcore.ParseLevel(c.LogLevel); err != nil {
return err
}
}

return nil
}

func (c *Config) validateRetrievers() error {
if c.Retriever == nil && c.Retrievers == nil {
return fmt.Errorf("no retriever available in the configuration")
}
Expand All @@ -380,26 +405,35 @@ func (c *Config) IsValid() error {
}
}

// Exporter is optional
return nil
}

func (c *Config) validateExporters() error {
if c.Exporter != nil {
if err := c.Exporter.IsValid(); err != nil {
return err
}
}

if c.Exporters != nil {
for _, exporter := range *c.Exporters {
if err := exporter.IsValid(); err != nil {
return err
}
}
}

return nil
}

func (c *Config) validateNotifiers() error {
if c.Notifiers != nil {
for _, notif := range c.Notifiers {
if err := notif.IsValid(); err != nil {
return err
}
}
}
if c.LogLevel != "" {
if _, err := zapcore.ParseLevel(c.LogLevel); err != nil {
return err
}
}

return nil
}

Expand Down
102 changes: 102 additions & 0 deletions cmd/relayproxy/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,86 @@ func TestParseConfig_fileFromPflag(t *testing.T) {
},
wantErr: assert.NoError,
},
{
name: "Valid yaml file with multiple exporters",
fileLocation: "../testdata/config/valid-yaml-multiple-exporters.yaml",
want: &config.Config{
ListenPort: 1031,
PollingInterval: 1000,
FileFormat: "yaml",
Host: "localhost",
Retriever: &config.RetrieverConf{
Kind: "http",
URL: "https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/examples/retriever_file/flags.goff.yaml",
},
Exporters: &[]config.ExporterConf{
{
Kind: "log",
},
{
Kind: "file",
OutputDir: "./",
},
},
StartWithRetrieverError: false,
RestAPITimeout: 5000,
Version: "1.X.X",
EnableSwagger: true,
AuthorizedKeys: config.APIKeys{
Admin: []string{
"apikey3",
},
Evaluation: []string{
"apikey1",
"apikey2",
},
},
LogLevel: "info",
},
wantErr: assert.NoError,
},
{
name: "Valid yaml file with both exporter and exporters",
fileLocation: "../testdata/config/valid-yaml-exporter-and-exporters.yaml",
want: &config.Config{
ListenPort: 1031,
PollingInterval: 1000,
FileFormat: "yaml",
Host: "localhost",
Retriever: &config.RetrieverConf{
Kind: "http",
URL: "https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/examples/retriever_file/flags.goff.yaml",
},
Exporter: &config.ExporterConf{
Kind: "log",
},
Exporters: &[]config.ExporterConf{
{
Kind: "webhook",
EndpointURL: "https://example.com/webhook",
},
{
Kind: "file",
OutputDir: "./",
},
},
StartWithRetrieverError: false,
RestAPITimeout: 5000,
Version: "1.X.X",
EnableSwagger: true,
AuthorizedKeys: config.APIKeys{
Admin: []string{
"apikey3",
},
Evaluation: []string{
"apikey1",
"apikey2",
},
},
LogLevel: "info",
},
wantErr: assert.NoError,
},
{
name: "Valid json file",
fileLocation: "../testdata/config/valid-file.json",
Expand Down Expand Up @@ -323,6 +403,7 @@ func TestConfig_IsValid(t *testing.T) {
Retriever *config.RetrieverConf
Retrievers *[]config.RetrieverConf
Exporter *config.ExporterConf
Exporters *[]config.ExporterConf
Notifiers []config.NotifierConf
LogLevel string
Debug bool
Expand Down Expand Up @@ -454,6 +535,26 @@ func TestConfig_IsValid(t *testing.T) {
},
wantErr: assert.Error,
},
{
name: "invalid exporter in the list of exporters",
fields: fields{
ListenPort: 8080,
Retriever: &config.RetrieverConf{
Kind: "file",
Path: "../testdata/config/valid-file.yaml",
},
Exporters: &[]config.ExporterConf{
{
Kind: "webhook",
EndpointURL: "https://example.com/webhook",
},
{
Kind: "file",
},
},
},
wantErr: assert.Error,
},
{
name: "invalid notifier",
fields: fields{
Expand Down Expand Up @@ -508,6 +609,7 @@ func TestConfig_IsValid(t *testing.T) {
StartWithRetrieverError: tt.fields.StartWithRetrieverError,
Retriever: tt.fields.Retriever,
Exporter: tt.fields.Exporter,
Exporters: tt.fields.Exporters,
Notifiers: tt.fields.Notifiers,
Retrievers: tt.fields.Retrievers,
LogLevel: tt.fields.LogLevel,
Expand Down
99 changes: 70 additions & 29 deletions cmd/relayproxy/service/gofeatureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,24 @@ func NewGoFeatureFlagClient(
logger *zap.Logger,
notifiers []notifier.Notifier,
) (*ffclient.GoFeatureFlag, error) {
var mainRetriever retriever.Retriever
var err error

if proxyConf == nil {
return nil, fmt.Errorf("proxy config is empty")
}

if proxyConf.Retriever != nil {
mainRetriever, err = initRetriever(proxyConf.Retriever)
if err != nil {
return nil, err
}
}

// Manage if we have more than 1 retriever
retrievers := make([]retriever.Retriever, 0)
if proxyConf.Retrievers != nil {
for _, r := range *proxyConf.Retrievers {
currentRetriever, err := initRetriever(&r)
if err != nil {
return nil, err
}
retrievers = append(retrievers, currentRetriever)
}
mainRetriever, retrievers, err := initRetrievers(proxyConf)
if err != nil {
return nil, err
}

var exp ffclient.DataExporter
if proxyConf.Exporter != nil {
exp, err = initDataExporter(proxyConf.Exporter)
if err != nil {
return nil, err
}
mainDataExporter, dataExporters, err := initExporters(proxyConf)
if err != nil {
return nil, err
}

notif, err := initNotifier(proxyConf.Notifiers)
notif, err := initNotifiers(proxyConf.Notifiers, notifiers)
if err != nil {
return nil, err
}
notif = append(notif, notifiers...)

f := ffclient.Config{
PollingInterval: time.Duration(proxyConf.PollingInterval) * time.Millisecond,
Expand All @@ -89,7 +68,8 @@ func NewGoFeatureFlagClient(
Retrievers: retrievers,
Notifiers: notif,
FileFormat: proxyConf.FileFormat,
DataExporter: exp,
DataExporter: mainDataExporter,
DataExporters: dataExporters,
StartWithRetrieverError: proxyConf.StartWithRetrieverError,
EnablePollingJitter: proxyConf.EnablePollingJitter,
DisableNotifierOnInit: proxyConf.DisableNotifierOnInit,
Expand All @@ -100,6 +80,67 @@ func NewGoFeatureFlagClient(
return ffclient.New(f)
}

func initRetrievers(proxyConf *config.Config) (retriever.Retriever, []retriever.Retriever, error) {
var mainRetriever retriever.Retriever
var err error

if proxyConf.Retriever != nil {
mainRetriever, err = initRetriever(proxyConf.Retriever)
if err != nil {
return nil, nil, err
}
}

retrievers := make([]retriever.Retriever, 0)
if proxyConf.Retrievers != nil {
for _, r := range *proxyConf.Retrievers {
currentRetriever, err := initRetriever(&r)
if err != nil {
return nil, nil, err
}
retrievers = append(retrievers, currentRetriever)
}
}

return mainRetriever, retrievers, nil
}

func initExporters(proxyConf *config.Config) (ffclient.DataExporter, []ffclient.DataExporter, error) {
var mainDataExporter ffclient.DataExporter
var err error

if proxyConf.Exporter != nil {
mainDataExporter, err = initDataExporter(proxyConf.Exporter)
if err != nil {
return ffclient.DataExporter{}, nil, err
}
}

dataExporters := make([]ffclient.DataExporter, 0)
if proxyConf.Exporters != nil {
for _, e := range *proxyConf.Exporters {
currentExporter, err := initDataExporter(&e)
if err != nil {
return ffclient.DataExporter{}, nil, err
}
dataExporters = append(dataExporters, currentExporter)
}
}

return mainDataExporter, dataExporters, nil
}

func initNotifiers(
configNotifiers []config.NotifierConf,
additionalNotifiers []notifier.Notifier,
) ([]notifier.Notifier, error) {
notif, err := initNotifier(configNotifiers)
if err != nil {
return nil, err
}
return append(notif, additionalNotifiers...), nil
}

// initRetriever initialize the retriever based on the configuration
func initRetriever(c *config.RetrieverConf) (retriever.Retriever, error) {
retrieverTimeout := config.DefaultRetriever.Timeout
Expand Down
Loading
Loading