From 62910f6a44ca12f72c76538c0a0db0a428f37787 Mon Sep 17 00:00:00 2001 From: IvanovOleg Date: Thu, 26 Oct 2023 10:19:38 -0400 Subject: [PATCH] Adds the K6_INFLUXDB_PROXY environment variable support, closes #3418 --- output/influxdb/config.go | 4 ++++ output/influxdb/config_test.go | 11 +++++++++++ output/influxdb/util.go | 14 ++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/output/influxdb/config.go b/output/influxdb/config.go index b18a91313f3d..35a5dc83bd7d 100644 --- a/output/influxdb/config.go +++ b/output/influxdb/config.go @@ -17,6 +17,7 @@ import ( type Config struct { // Connection. Addr null.String `json:"addr" envconfig:"K6_INFLUXDB_ADDR"` + Proxy null.String `json:"proxy,omitempty" envconfig:"K6_INFLUXDB_PROXY"` Username null.String `json:"username,omitempty" envconfig:"K6_INFLUXDB_USERNAME"` Password null.String `json:"password,omitempty" envconfig:"K6_INFLUXDB_PASSWORD"` Insecure null.Bool `json:"insecure,omitempty" envconfig:"K6_INFLUXDB_INSECURE"` @@ -57,6 +58,9 @@ func (c Config) Apply(cfg Config) Config { if cfg.Addr.Valid { c.Addr = cfg.Addr } + if cfg.Proxy.Valid { + c.Proxy = cfg.Proxy + } if cfg.Username.Valid { c.Username = cfg.Username } diff --git a/output/influxdb/config_test.go b/output/influxdb/config_test.go index 90a682e41235..c4ec148af86c 100644 --- a/output/influxdb/config_test.go +++ b/output/influxdb/config_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "gopkg.in/guregu/null.v3" ) @@ -51,3 +52,13 @@ func TestParseURL(t *testing.T) { }) } } + +func TestGetConsolidatedConfig(t *testing.T) { + t.Parallel() + testdata := map[string]string{ + "K6_INFLUXDB_PROXY": "http://localhost:3128", + } + config, err := GetConsolidatedConfig(nil, testdata, "") + require.NoError(t, err) + require.Equal(t, config.Proxy, null.StringFrom("http://localhost:3128")) +} diff --git a/output/influxdb/util.go b/output/influxdb/util.go index 1c4088b5fb3b..5db6d2a39c7a 100644 --- a/output/influxdb/util.go +++ b/output/influxdb/util.go @@ -2,6 +2,8 @@ package influxdb import ( "fmt" + "net/http" + "net/url" "strings" client "github.com/influxdata/influxdb1-client/v2" @@ -18,13 +20,21 @@ func MakeClient(conf Config) (client.Client, error) { if conf.Addr.String == "" { conf.Addr = null.StringFrom("http://localhost:8086") } - return client.NewHTTPClient(client.HTTPConfig{ + clientHTTPConfig := client.HTTPConfig{ Addr: conf.Addr.String, Username: conf.Username.String, Password: conf.Password.String, UserAgent: "k6", InsecureSkipVerify: conf.Insecure.Bool, - }) + } + if conf.Proxy.Valid && conf.Proxy.String != "" { + parsedProxyURL, err := url.Parse(conf.Proxy.String) + if err != nil { + return nil, fmt.Errorf("failed to parse the http proxy URL: %w", err) + } + clientHTTPConfig.Proxy = http.ProxyURL(parsedProxyURL) + } + return client.NewHTTPClient(clientHTTPConfig) } func MakeBatchConfig(conf Config) client.BatchPointsConfig {