From edac25a18b57d031131ba7062ab6d173088f9566 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 | 6 ++++++ output/influxdb/util.go | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/output/influxdb/config.go b/output/influxdb/config.go index b18a91313f3d..a5f95a589fc4 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 } @@ -159,6 +163,8 @@ func ParseURL(text string) (Config, error) { c.ConcurrentWrites = null.IntFrom(int64(writes)) case "tagsAsFields": c.TagsAsFields = vs + case "proxy": + c.Proxy = null.StringFrom(vs[0]) default: return c, fmt.Errorf("unknown query parameter: %s", k) } diff --git a/output/influxdb/util.go b/output/influxdb/util.go index 1c4088b5fb3b..c609cc96306c 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, err + } + clientHTTPConfig.Proxy = http.ProxyURL(parsedProxyURL) + } + return client.NewHTTPClient(clientHTTPConfig) } func MakeBatchConfig(conf Config) client.BatchPointsConfig {