Skip to content

Commit

Permalink
Prefix all envconfig struct tags with K6 directly (grafana#1215)
Browse files Browse the repository at this point in the history
* Prefix all envconfig struct tags with K6 directly

Works on grafana#671 and fixing it in almost all cases.

This was done primarily by running:
```
rg -g "*.go"  'envconfig:"' --files-with-matches  |\
    xargs -n 1 sed -s -i 's/envconfig:"/envconfig:"K6_/g'
```

And than manually fixing all remaing problems by hand.
In the mean time I found out we weren't properly combing the
configuration for statsd/datadog and fixed.

Also uppercases all the envconfig struct tags

using:
```
rg -g "*.go"  'envconfig:"' --files-with-matches   |\
  xargs -n 1 sed -r -i -s  's/envconfig:"([^"]*)"/envconfig:"\U\1\E"/g'
```

Renamed HttpDebug to HTTPDebug in the Options struct.

Unfortunately due to the way the statsd and datadog share the same
struct for a config this doesn't fix the issue as a whole for them.
  • Loading branch information
mstoykov authored Oct 25, 2019
1 parent 4b4d88d commit 82a59aa
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 103 deletions.
2 changes: 1 addition & 1 deletion cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ This will execute the test on the Load Impact cloud service. Use "k6 login cloud

// Cloud config
cloudConfig := cloud.NewConfig().Apply(derivedConf.Collectors.Cloud)
if err := envconfig.Process("k6", &cloudConfig); err != nil {
if err = envconfig.Process("", &cloudConfig); err != nil {
return err
}
if !cloudConfig.Token.Valid {
Expand Down
8 changes: 4 additions & 4 deletions cmd/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func getCollector(collectorName, arg string, src *loader.SourceData, conf Config
return jsonc.New(afero.NewOsFs(), arg)
case collectorInfluxDB:
config := influxdb.NewConfig().Apply(conf.Collectors.InfluxDB)
if err := envconfig.Process("k6", &config); err != nil {
if err := envconfig.Process("", &config); err != nil {
return nil, err
}
urlConfig, err := influxdb.ParseURL(arg)
Expand All @@ -83,7 +83,7 @@ func getCollector(collectorName, arg string, src *loader.SourceData, conf Config
return influxdb.New(config)
case collectorCloud:
config := cloud.NewConfig().Apply(conf.Collectors.Cloud)
if err := envconfig.Process("k6", &config); err != nil {
if err := envconfig.Process("", &config); err != nil {
return nil, err
}
if arg != "" {
Expand All @@ -92,7 +92,7 @@ func getCollector(collectorName, arg string, src *loader.SourceData, conf Config
return cloud.New(config, src, conf.Options, consts.Version)
case collectorKafka:
config := kafka.NewConfig().Apply(conf.Collectors.Kafka)
if err := envconfig.Process("k6", &config); err != nil {
if err := envconfig.Process("", &config); err != nil {
return nil, err
}
if arg != "" {
Expand All @@ -117,7 +117,7 @@ func getCollector(collectorName, arg string, src *loader.SourceData, conf Config
return datadog.New(config)
case collectorCSV:
config := csv.NewConfig().Apply(conf.Collectors.CSV)
if err := envconfig.Process("k6", &config); err != nil {
if err := envconfig.Process("", &config); err != nil {
return nil, err
}
if arg != "" {
Expand Down
22 changes: 13 additions & 9 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func configFlagSet() *pflag.FlagSet {
type Config struct {
lib.Options

Out []string `json:"out" envconfig:"out"`
Linger null.Bool `json:"linger" envconfig:"linger"`
NoUsageReport null.Bool `json:"noUsageReport" envconfig:"no_usage_report"`
NoThresholds null.Bool `json:"noThresholds" envconfig:"no_thresholds"`
NoSummary null.Bool `json:"noSummary" envconfig:"no_summary"`
Out []string `json:"out" envconfig:"K6_OUT"`
Linger null.Bool `json:"linger" envconfig:"K6_LINGER"`
NoUsageReport null.Bool `json:"noUsageReport" envconfig:"K6_NO_USAGE_REPORT"`
NoThresholds null.Bool `json:"noThresholds" envconfig:"K6_NO_THRESHOLDS"`
NoSummary null.Bool `json:"noSummary" envconfig:"K6_NO_SUMMARY"`

Collectors struct {
InfluxDB influxdb.Config `json:"influxdb"`
Expand Down Expand Up @@ -174,10 +174,12 @@ func writeDiskConfig(fs afero.Fs, configPath string, conf Config) error {
func readEnvConfig() (conf Config, err error) {
// TODO: replace envconfig and refactor the whole configuration from the groun up :/
for _, err := range []error{
envconfig.Process("k6", &conf),
envconfig.Process("k6", &conf.Collectors.Cloud),
envconfig.Process("k6", &conf.Collectors.InfluxDB),
envconfig.Process("k6", &conf.Collectors.Kafka),
envconfig.Process("", &conf),
envconfig.Process("", &conf.Collectors.Cloud),
envconfig.Process("", &conf.Collectors.InfluxDB),
envconfig.Process("", &conf.Collectors.Kafka),
envconfig.Process("k6_statsd", &conf.Collectors.StatsD),
envconfig.Process("k6_datadog", &conf.Collectors.Datadog),
} {
return conf, err
}
Expand Down Expand Up @@ -288,6 +290,8 @@ func getConsolidatedConfig(fs afero.Fs, cliConf Config, runner lib.Runner) (conf
cliConf.Collectors.InfluxDB = influxdb.NewConfig().Apply(cliConf.Collectors.InfluxDB)
cliConf.Collectors.Cloud = cloud.NewConfig().Apply(cliConf.Collectors.Cloud)
cliConf.Collectors.Kafka = kafka.NewConfig().Apply(cliConf.Collectors.Kafka)
cliConf.Collectors.StatsD = common.NewConfig().Apply(cliConf.Collectors.StatsD)
cliConf.Collectors.Datadog = datadog.NewConfig().Apply(cliConf.Collectors.Datadog)

fileConf, _, err := readDiskConfig(fs)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestConfigEnv(t *testing.T) {
t.Run(`"`+value+`"`, func(t *testing.T) {
assert.NoError(t, os.Setenv(field.Key, value))
var config Config
assert.NoError(t, envconfig.Process("k6", &config))
assert.NoError(t, envconfig.Process("", &config))
fn(config)
})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
Batch: getNullInt64(flags, "batch"),
RPS: getNullInt64(flags, "rps"),
UserAgent: getNullString(flags, "user-agent"),
HttpDebug: getNullString(flags, "http-debug"),
HTTPDebug: getNullString(flags, "http-debug"),
InsecureSkipTLSVerify: getNullBool(flags, "insecure-skip-tls-verify"),
NoConnectionReuse: getNullBool(flags, "no-connection-reuse"),
NoVUConnectionReuse: getNullBool(flags, "no-vu-connection-reuse"),
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ a commandline interface for interacting with it.`,
updateFreq = 1 * time.Second
}
ticker := time.NewTicker(updateFreq)
if quiet || conf.HttpDebug.Valid && conf.HttpDebug.String != "" {
if quiet || conf.HTTPDebug.Valid && conf.HTTPDebug.String != "" {
ticker.Stop()
}
mainLoop:
Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func newRuntime(
UserAgent: null.StringFrom("TestUserAgent"),
Throw: null.BoolFrom(true),
SystemTags: &stats.DefaultSystemTagSet,
//HttpDebug: null.StringFrom("full"),
//HTTPDebug: null.StringFrom("full"),
}
samples := make(chan stats.SampleContainer, 1000)

Expand Down Expand Up @@ -1936,7 +1936,7 @@ func TestDigestAuthWithBody(t *testing.T) {
defer tb.Cleanup()

state.Options.Throw = null.BoolFrom(true)
state.Options.HttpDebug = null.StringFrom("full")
state.Options.HTTPDebug = null.StringFrom("full")

tb.Mux.HandleFunc("/digest-auth-with-post/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "POST", r.Method)
Expand Down
4 changes: 2 additions & 2 deletions lib/netext/httpext/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error
tracerTransport := newTransport(state, tags)
var transport http.RoundTripper = tracerTransport

if state.Options.HttpDebug.String != "" {
if state.Options.HTTPDebug.String != "" {
transport = httpDebugTransport{
originalTransport: transport,
httpDebugOption: state.Options.HttpDebug.String,
httpDebugOption: state.Options.HTTPDebug.String,
}
}

Expand Down
70 changes: 35 additions & 35 deletions lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,102 +185,102 @@ func ParseCIDR(s string) (*IPNet, error) {

type Options struct {
// Should the test start in a paused state?
Paused null.Bool `json:"paused" envconfig:"paused"`
Paused null.Bool `json:"paused" envconfig:"K6_PAUSED"`

// Initial values for VUs, max VUs, duration cap, iteration cap, and stages.
// See the Runner or Executor interfaces for more information.
VUs null.Int `json:"vus" envconfig:"vus"`
VUs null.Int `json:"vus" envconfig:"K6_VUS"`

//TODO: deprecate this? or reuse it in the manual control "scheduler"?
VUsMax null.Int `json:"vusMax" envconfig:"vus_max"`
Duration types.NullDuration `json:"duration" envconfig:"duration"`
Iterations null.Int `json:"iterations" envconfig:"iterations"`
Stages []Stage `json:"stages" envconfig:"stages"`
VUsMax null.Int `json:"vusMax" envconfig:"K6_VUS_MAX"`
Duration types.NullDuration `json:"duration" envconfig:"K6_DURATION"`
Iterations null.Int `json:"iterations" envconfig:"K6_ITERATIONS"`
Stages []Stage `json:"stages" envconfig:"K6_STAGES"`

Execution scheduler.ConfigMap `json:"execution,omitempty" envconfig:"-"`

// Timeouts for the setup() and teardown() functions
SetupTimeout types.NullDuration `json:"setupTimeout" envconfig:"setup_timeout"`
TeardownTimeout types.NullDuration `json:"teardownTimeout" envconfig:"teardown_timeout"`
SetupTimeout types.NullDuration `json:"setupTimeout" envconfig:"K6_SETUP_TIMEOUT"`
TeardownTimeout types.NullDuration `json:"teardownTimeout" envconfig:"K6_TEARDOWN_TIMEOUT"`

// Limit HTTP requests per second.
RPS null.Int `json:"rps" envconfig:"rps"`
RPS null.Int `json:"rps" envconfig:"K6_RPS"`

// How many HTTP redirects do we follow?
MaxRedirects null.Int `json:"maxRedirects" envconfig:"max_redirects"`
MaxRedirects null.Int `json:"maxRedirects" envconfig:"K6_MAX_REDIRECTS"`

// Default User Agent string for HTTP requests.
UserAgent null.String `json:"userAgent" envconfig:"user_agent"`
UserAgent null.String `json:"userAgent" envconfig:"K6_USER_AGENT"`

// How many batch requests are allowed in parallel, in total and per host?
Batch null.Int `json:"batch" envconfig:"batch"`
BatchPerHost null.Int `json:"batchPerHost" envconfig:"batch_per_host"`
Batch null.Int `json:"batch" envconfig:"K6_BATCH"`
BatchPerHost null.Int `json:"batchPerHost" envconfig:"K6_BATCH_PER_HOST"`

// Should all HTTP requests and responses be logged (excluding body)?
HttpDebug null.String `json:"httpDebug" envconfig:"http_debug"`
HTTPDebug null.String `json:"httpDebug" envconfig:"K6_HTTP_DEBUG"`

// Accept invalid or untrusted TLS certificates.
InsecureSkipTLSVerify null.Bool `json:"insecureSkipTLSVerify" envconfig:"insecure_skip_tls_verify"`
InsecureSkipTLSVerify null.Bool `json:"insecureSkipTLSVerify" envconfig:"K6_INSECURE_SKIP_TLS_VERIFY"`

// Specify TLS versions and cipher suites, and present client certificates.
TLSCipherSuites *TLSCipherSuites `json:"tlsCipherSuites" envconfig:"tls_cipher_suites"`
TLSVersion *TLSVersions `json:"tlsVersion" envconfig:"tls_version"`
TLSAuth []*TLSAuth `json:"tlsAuth" envconfig:"tlsauth"`
TLSCipherSuites *TLSCipherSuites `json:"tlsCipherSuites" envconfig:"K6_TLS_CIPHER_SUITES"`
TLSVersion *TLSVersions `json:"tlsVersion" envconfig:"K6_TLS_VERSION"`
TLSAuth []*TLSAuth `json:"tlsAuth" envconfig:"K6_TLSAUTH"`

// Throw warnings (eg. failed HTTP requests) as errors instead of simply logging them.
Throw null.Bool `json:"throw" envconfig:"throw"`
Throw null.Bool `json:"throw" envconfig:"K6_THROW"`

// Define thresholds; these take the form of 'metric=["snippet1", "snippet2"]'.
// To create a threshold on a derived metric based on tag queries ("submetrics"), create a
// metric on a nonexistent metric named 'real_metric{tagA:valueA,tagB:valueB}'.
Thresholds map[string]stats.Thresholds `json:"thresholds" envconfig:"thresholds"`
Thresholds map[string]stats.Thresholds `json:"thresholds" envconfig:"K6_THRESHOLDS"`

// Blacklist IP ranges that tests may not contact. Mainly useful in hosted setups.
BlacklistIPs []*IPNet `json:"blacklistIPs" envconfig:"blacklist_ips"`
BlacklistIPs []*IPNet `json:"blacklistIPs" envconfig:"K6_BLACKLIST_IPS"`

// Hosts overrides dns entries for given hosts
Hosts map[string]net.IP `json:"hosts" envconfig:"hosts"`
Hosts map[string]net.IP `json:"hosts" envconfig:"K6_HOSTS"`

// Disable keep-alive connections
NoConnectionReuse null.Bool `json:"noConnectionReuse" envconfig:"no_connection_reuse"`
NoConnectionReuse null.Bool `json:"noConnectionReuse" envconfig:"K6_NO_CONNECTION_REUSE"`

// Do not reuse connections between VU iterations. This gives more realistic results (depending
// on what you're looking for), but you need to raise various kernel limits or you'll get
// errors about running out of file handles or sockets, or being unable to bind addresses.
NoVUConnectionReuse null.Bool `json:"noVUConnectionReuse" envconfig:"no_vu_connection_reuse"`
NoVUConnectionReuse null.Bool `json:"noVUConnectionReuse" envconfig:"K6_NO_VU_CONNECTION_REUSE"`

// MinIterationDuration can be used to force VUs to pause between iterations if a specific
// iteration is shorter than the specified value.
MinIterationDuration types.NullDuration `json:"minIterationDuration" envconfig:"min_iteration_duration"`
MinIterationDuration types.NullDuration `json:"minIterationDuration" envconfig:"K6_MIN_ITERATION_DURATION"`

// These values are for third party collectors' benefit.
// Can't be set through env vars.
External map[string]json.RawMessage `json:"ext" ignored:"true"`

// Summary trend stats for trend metrics (response times) in CLI output
SummaryTrendStats []string `json:"summaryTrendStats" envconfig:"summary_trend_stats"`
SummaryTrendStats []string `json:"summaryTrendStats" envconfig:"K6_SUMMARY_TREND_STATS"`

// Summary time unit for summary metrics (response times) in CLI output
SummaryTimeUnit null.String `json:"summaryTimeUnit" envconfig:"summary_time_unit"`
SummaryTimeUnit null.String `json:"summaryTimeUnit" envconfig:"K6_SUMMARY_TIME_UNIT"`

// Which system tags to include with metrics ("method", "vu" etc.)
// Use pointer for identifying whether user provide any tag or not.
SystemTags *stats.SystemTagSet `json:"systemTags" envconfig:"system_tags"`
SystemTags *stats.SystemTagSet `json:"systemTags" envconfig:"K6_SYSTEM_TAGS"`

// Tags to be applied to all samples for this running
RunTags *stats.SampleTags `json:"tags" envconfig:"tags"`
RunTags *stats.SampleTags `json:"tags" envconfig:"K6_TAGS"`

// Buffer size of the channel for metric samples; 0 means unbuffered
MetricSamplesBufferSize null.Int `json:"metricSamplesBufferSize" envconfig:"metric_samples_buffer_size"`
MetricSamplesBufferSize null.Int `json:"metricSamplesBufferSize" envconfig:"K6_METRIC_SAMPLES_BUFFER_SIZE"`

// Do not reset cookies after a VU iteration
NoCookiesReset null.Bool `json:"noCookiesReset" envconfig:"no_cookies_reset"`
NoCookiesReset null.Bool `json:"noCookiesReset" envconfig:"K6_NO_COOKIES_RESET"`

// Discard Http Responses Body
DiscardResponseBodies null.Bool `json:"discardResponseBodies" envconfig:"discard_response_bodies"`
DiscardResponseBodies null.Bool `json:"discardResponseBodies" envconfig:"K6_DISCARD_RESPONSE_BODIES"`

// Redirect console logging to a file
ConsoleOutput null.String `json:"-" envconfig:"console_output"`
ConsoleOutput null.String `json:"-" envconfig:"K6_CONSOLE_OUTPUT"`
}

// Returns the result of overwriting any fields with any that are set on the argument.
Expand Down Expand Up @@ -358,8 +358,8 @@ func (o Options) Apply(opts Options) Options {
if opts.BatchPerHost.Valid {
o.BatchPerHost = opts.BatchPerHost
}
if opts.HttpDebug.Valid {
o.HttpDebug = opts.HttpDebug
if opts.HTTPDebug.Valid {
o.HTTPDebug = opts.HTTPDebug
}
if opts.InsecureSkipTLSVerify.Valid {
o.InsecureSkipTLSVerify = opts.InsecureSkipTLSVerify
Expand Down
8 changes: 4 additions & 4 deletions lib/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ func TestOptions(t *testing.T) {
assert.True(t, opts.BatchPerHost.Valid)
assert.Equal(t, int64(12345), opts.BatchPerHost.Int64)
})
t.Run("HttpDebug", func(t *testing.T) {
opts := Options{}.Apply(Options{HttpDebug: null.StringFrom("foo")})
assert.True(t, opts.HttpDebug.Valid)
assert.Equal(t, "foo", opts.HttpDebug.String)
t.Run("HTTPDebug", func(t *testing.T) {
opts := Options{}.Apply(Options{HTTPDebug: null.StringFrom("foo")})
assert.True(t, opts.HTTPDebug.Valid)
assert.Equal(t, "foo", opts.HTTPDebug.String)
})
t.Run("InsecureSkipTLSVerify", func(t *testing.T) {
opts := Options{}.Apply(Options{InsecureSkipTLSVerify: null.BoolFrom(true)})
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import null "gopkg.in/guregu/null.v3"
// RuntimeOptions are settings passed onto the goja JS runtime
type RuntimeOptions struct {
// Whether to pass the actual system environment variables to the JS runtime
IncludeSystemEnvVars null.Bool `json:"includeSystemEnvVars" envconfig:"include_system_env_vars"`
IncludeSystemEnvVars null.Bool `json:"includeSystemEnvVars" envconfig:"K6_INCLUDE_SYSTEM_ENV_VARS"`

// Environment variables passed onto the runner
Env map[string]string `json:"env" envconfig:"env"`
Env map[string]string `json:"env" envconfig:"K6_ENV"`
}

// Apply overwrites the receiver RuntimeOptions' fields with any that are set
Expand Down
Loading

0 comments on commit 82a59aa

Please sign in to comment.