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

Adds the K6_INFLUXDB_PROXY environment variable support, closes #3418 #3423

Merged
merged 1 commit into from
Oct 30, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions output/influxdb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Copy link
Collaborator

@codebien codebien Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Proxy null.String `json:"proxy,omitempty" envconfig:"K6_INFLUXDB_PROXY"`
Proxy null.String `json:"proxy,omitempty" envconfig:"K6_INFLUXDB_HTTP_PROXY"`

@IvanovOleg @mstoykov WDYT? Should we do it? I like the consistency with the original HTTP_PROXY variable, but on the other side, I'm not sure because it could introduce confusion around the expected HTTP/S value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change the name as you suggested, no problem. The reason I named it like that because it will work with http and https at the same time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a config test for the K6_INFLUXDB_PROXY variable.

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"`
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions output/influxdb/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,26 @@ func TestParseURL(t *testing.T) {
})
}
}

func TestGetConsolidatedConfigHTTPProxy(t *testing.T) {
t.Parallel()
t.Run("Valid Proxy URL", func(t *testing.T) {
t.Parallel()
testdata := map[string]string{
"K6_INFLUXDB_PROXY": "http://localhost:3128",
}
config, err := GetConsolidatedConfig(nil, testdata, "")
assert.NoError(t, err)
assert.Equal(t, "http://localhost:3128", config.Proxy.String)
})
t.Run("Invalid Proxy URL", func(t *testing.T) {
t.Parallel()
testdata := map[string]string{
"K6_INFLUXDB_PROXY": "http://foo\x7f.com/",
}
config, err := GetConsolidatedConfig(nil, testdata, "")
assert.NoError(t, err)
_, err = MakeClient(config)
assert.Error(t, err)
})
}
14 changes: 12 additions & 2 deletions output/influxdb/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package influxdb

import (
"fmt"
"net/http"
"net/url"
"strings"

client "github.com/influxdata/influxdb1-client/v2"
Expand All @@ -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 {
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 {
Expand Down