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: Azure Blob Storage Exporter #2637

Merged
merged 8 commits into from
Nov 10, 2024
Merged
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
13 changes: 12 additions & 1 deletion cmd/relayproxy/config/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type ExporterConf struct {
Topic string `mapstructure:"topic" koanf:"topic"`
StreamArn string `mapstructure:"streamArn" koanf:"streamarn"`
StreamName string `mapstructure:"streamName" koanf:"streamname"`
AccountName string `mapstructure:"accountName" koanf:"accountname"`
AccountKey string `mapstructure:"accountKey" koanf:"accountkey"`
Container string `mapstructure:"container" koanf:"container"`
}

func (c *ExporterConf) IsValid() error {
Expand Down Expand Up @@ -65,6 +68,13 @@ func (c *ExporterConf) IsValid() error {
return fmt.Errorf("invalid exporter: \"projectID\" and \"topic\" are required for kind \"%s\"", c.Kind)
}

if c.Kind == AzureExporter && c.Container == "" {
return fmt.Errorf("invalid exporter: no \"container\" property found for kind \"%s\"", c.Kind)
}
if c.Kind == AzureExporter && c.AccountName == "" {
return fmt.Errorf("invalid exporter: no \"accountName\" property found for kind \"%s\"", c.Kind)
}

return nil
}

Expand All @@ -80,13 +90,14 @@ const (
SQSExporter ExporterKind = "sqs"
KafkaExporter ExporterKind = "kafka"
PubSubExporter ExporterKind = "pubsub"
AzureExporter ExporterKind = "azureBlobStorage"
)

// IsValid is checking if the value is part of the enum
func (r ExporterKind) IsValid() error {
switch r {
case FileExporter, WebhookExporter, LogExporter, S3Exporter, GoogleStorageExporter, SQSExporter, KafkaExporter,
PubSubExporter, KinesisExporter:
PubSubExporter, KinesisExporter, AzureExporter:
return nil
}
return fmt.Errorf("invalid exporter: kind \"%s\" is not supported", r)
Expand Down
23 changes: 23 additions & 0 deletions cmd/relayproxy/config/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func TestExporterConf_IsValid(t *testing.T) {
ProjectID string
Topic string
StreamName string
Container string
AccountName string
}
tests := []struct {
name string
Expand Down Expand Up @@ -77,6 +79,25 @@ func TestExporterConf_IsValid(t *testing.T) {
wantErr: true,
errValue: "invalid exporter: no \"bucket\" property found for kind \"googleStorage\"",
},
{
name: "kind azureBlobStorage without container",
fields: fields{
Kind: "azureBlobStorage",
Container: "",
AccountName: "MyAccount",
},
wantErr: true,
errValue: "invalid exporter: no \"container\" property found for kind \"azureBlobStorage\"",
}, {
name: "kind azureBlobStorage without accountName",
fields: fields{
Kind: "azureBlobStorage",
Container: "MyContainer",
AccountName: "",
},
wantErr: true,
errValue: "invalid exporter: no \"accountName\" property found for kind \"azureBlobStorage\"",
},
{
name: "kind webhook without bucket",
fields: fields{
Expand Down Expand Up @@ -222,6 +243,8 @@ func TestExporterConf_IsValid(t *testing.T) {
ProjectID: tt.fields.ProjectID,
Topic: tt.fields.Topic,
StreamName: tt.fields.StreamName,
AccountName: tt.fields.AccountName,
Container: tt.fields.Container,
}
err := c.IsValid()
assert.Equal(t, tt.wantErr, err != nil)
Expand Down
12 changes: 12 additions & 0 deletions cmd/relayproxy/service/gofeatureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
"github.com/thomaspoignant/go-feature-flag/exporter"
"github.com/thomaspoignant/go-feature-flag/exporter/azureexporter"
"github.com/thomaspoignant/go-feature-flag/exporter/fileexporter"
"github.com/thomaspoignant/go-feature-flag/exporter/gcstorageexporter"
"github.com/thomaspoignant/go-feature-flag/exporter/kafkaexporter"
Expand Down Expand Up @@ -319,6 +320,17 @@ func createExporter(c *config.ExporterConf) (exporter.CommonExporter, error) {
ProjectID: c.ProjectID,
Topic: c.Topic,
}, nil
case config.AzureExporter:
return &azureexporter.Exporter{
Container: c.Container,
Format: format,
Path: c.Path,
Filename: filename,
CsvTemplate: csvTemplate,
ParquetCompressionCodec: parquetCompressionCodec,
AccountKey: c.AccountKey,
AccountName: c.AccountName,
}, nil
default:
return nil, fmt.Errorf("invalid exporter: kind \"%s\" is not supported", c.Kind)
}
Expand Down
24 changes: 24 additions & 0 deletions cmd/relayproxy/service/gofeatureflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
"github.com/thomaspoignant/go-feature-flag/exporter"
"github.com/thomaspoignant/go-feature-flag/exporter/azureexporter"
"github.com/thomaspoignant/go-feature-flag/exporter/fileexporter"
"github.com/thomaspoignant/go-feature-flag/exporter/gcstorageexporter"
"github.com/thomaspoignant/go-feature-flag/exporter/kafkaexporter"
Expand Down Expand Up @@ -437,6 +438,29 @@ func Test_initExporter(t *testing.T) {
wantType: &kinesisexporter.Exporter{},
skipCompleteValidation: true,
},
{
name: "Azure Blob Storage Exporter",
wantErr: assert.NoError,
conf: &config.ExporterConf{
Kind: "azureBlobStorage",
Container: "my-container",
Path: "/my-path/",
MaxEventInMemory: 1990,
},
want: ffclient.DataExporter{
FlushInterval: config.DefaultExporter.FlushInterval,
MaxEventInMemory: 1990,
Exporter: &azureexporter.Exporter{
Container: "my-container",
Format: config.DefaultExporter.Format,
Path: "/my-path/",
Filename: config.DefaultExporter.FileName,
CsvTemplate: config.DefaultExporter.CsvFormat,
ParquetCompressionCodec: config.DefaultExporter.ParquetCompressionCodec,
},
},
wantType: &azureexporter.Exporter{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
119 changes: 119 additions & 0 deletions exporter/azureexporter/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package azureexporter

import (
"context"
"fmt"
"log/slog"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/thomaspoignant/go-feature-flag/exporter"
"github.com/thomaspoignant/go-feature-flag/exporter/fileexporter"
"github.com/thomaspoignant/go-feature-flag/utils/fflog"
)

type Exporter struct {
// Container is the name of your Azure Blob Storage Container similar to Buckets in S3.
Container string

// Storage Account Name and Key
AccountName string
AccountKey string

Format string
Path string
Filename string
CsvTemplate string
ParquetCompressionCodec string

// ServiceURL is the URL of the storage account e.g. https://<account>.blob.core.windows.net/
// It can be overridden by the user to use a custom URL.
// Default: https://<account>.blob.core.windows.net/
ServiceURL string
}

func (f *Exporter) initializeAzureClient() (*azblob.Client, error) {
url := fmt.Sprintf("https://%s.blob.core.windows.net/", f.AccountName)
if f.ServiceURL != "" {
url = f.ServiceURL
}
if f.AccountKey == "" {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, err
}
return azblob.NewClient(url, cred, nil)

Check warning on line 46 in exporter/azureexporter/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporter/azureexporter/exporter.go#L42-L46

Added lines #L42 - L46 were not covered by tests
}
cred, err := azblob.NewSharedKeyCredential(f.AccountName, f.AccountKey)
if err != nil {
return nil, err
}

Check warning on line 51 in exporter/azureexporter/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporter/azureexporter/exporter.go#L50-L51

Added lines #L50 - L51 were not covered by tests
return azblob.NewClientWithSharedKeyCredential(url, cred, nil)
}

func (f *Exporter) Export(ctx context.Context, logger *fflog.FFLogger, featureEvents []exporter.FeatureEvent) error {
if f.AccountName == "" {
return fmt.Errorf("you should specify an AccountName. %v is invalid", f.AccountName)
}

client, err := f.initializeAzureClient()
if err != nil {
return err
}

Check warning on line 63 in exporter/azureexporter/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporter/azureexporter/exporter.go#L62-L63

Added lines #L62 - L63 were not covered by tests

if f.Container == "" {
return fmt.Errorf("you should specify a container. %v is invalid", f.Container)
}

outputDir, err := os.MkdirTemp("", "go_feature_flag_AzureBlobStorage_export")
if err != nil {
return err
}

Check warning on line 72 in exporter/azureexporter/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporter/azureexporter/exporter.go#L71-L72

Added lines #L71 - L72 were not covered by tests

fileExporter := fileexporter.Exporter{
Format: f.Format,
OutputDir: outputDir,
Filename: f.Filename,
CsvTemplate: f.CsvTemplate,
ParquetCompressionCodec: f.ParquetCompressionCodec,
}
err = fileExporter.Export(ctx, logger, featureEvents)
if err != nil {
return err
}

files, err := os.ReadDir(outputDir)
if err != nil {
return err
}

Check warning on line 89 in exporter/azureexporter/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporter/azureexporter/exporter.go#L88-L89

Added lines #L88 - L89 were not covered by tests

for _, file := range files {
fileName := file.Name()
of, err := os.Open(outputDir + "/" + fileName)
if err != nil {
logger.Error("[Azure Exporter] impossible to open file", slog.String("path", outputDir+"/"+fileName))
continue

Check warning on line 96 in exporter/azureexporter/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporter/azureexporter/exporter.go#L95-L96

Added lines #L95 - L96 were not covered by tests
}
defer func() { _ = of.Close() }()

// prepend the path
source := fileName
if f.Path != "" {
source = f.Path + "/" + fileName
}

_, err = client.UploadFile(context.Background(), f.Container, source, of, nil)
if err != nil {
logger.Error("[Azure Exporter] failed to upload file", slog.String("path", outputDir+"/"+fileName))
return err
}

logger.Info("[Azure Exporter] file uploaded.", slog.String("location", f.Container+"/"+fileName))
}
return nil
}

func (f *Exporter) IsBulk() bool {
return true
}
Loading
Loading