diff --git a/pkg/remotewrite/config.go b/pkg/remotewrite/config.go index c609913..b404a43 100644 --- a/pkg/remotewrite/config.go +++ b/pkg/remotewrite/config.go @@ -180,7 +180,9 @@ func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, _ } func parseEnvs(env map[string]string) (Config, error) { - var c Config + c := Config{ + Headers: make(map[string]string), + } getEnvBool := func(env map[string]string, name string) (null.Bool, error) { if v, vDefined := env[name]; vDefined { @@ -231,12 +233,19 @@ func parseEnvs(env map[string]string) (Config, error) { envHeaders := getEnvMap(env, "K6_PROMETHEUS_RW_HEADERS_") for k, v := range envHeaders { - if c.Headers == nil { - c.Headers = make(map[string]string) - } c.Headers[k] = v } + if headers, headersDefined := env["K6_PROMETHEUS_RW_HTTP_HEADERS"]; headersDefined { + for _, kvPair := range strings.Split(headers, ",") { + header := strings.Split(kvPair, ":") + if len(header) != 2 { + return c, fmt.Errorf("the provided header (%s) does not respect the expected format
:", kvPair) + } + c.Headers[header[0]] = header[1] + } + } + if b, err := getEnvBool(env, "K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM"); err != nil { return c, err } else if b.Valid { diff --git a/pkg/remotewrite/config_test.go b/pkg/remotewrite/config_test.go index ae499ad..f5c67ae 100644 --- a/pkg/remotewrite/config_test.go +++ b/pkg/remotewrite/config_test.go @@ -255,8 +255,15 @@ func TestOptionHeaders(t *testing.T) { env map[string]string jsonRaw json.RawMessage }{ - "JSON": {jsonRaw: json.RawMessage(`{"headers":{"X-MY-HEADER1":"hval1","X-MY-HEADER2":"hval2"}}`)}, - "Env": {env: map[string]string{"K6_PROMETHEUS_RW_HEADERS_X-MY-HEADER1": "hval1", "K6_PROMETHEUS_RW_HEADERS_X-MY-HEADER2": "hval2"}}, + "JSON": {jsonRaw: json.RawMessage( + `{"headers":{"X-MY-HEADER1":"hval1","X-MY-HEADER2":"hval2","X-Scope-OrgID":"my-org-id","another-header":"true","empty":""}}`)}, + "Env": {env: map[string]string{ + "K6_PROMETHEUS_RW_HEADERS_X-MY-HEADER1": "hval1", + "K6_PROMETHEUS_RW_HEADERS_X-MY-HEADER2": "hval2", + // it assert that the new method using HTTP_HEADERS overwrites it + "K6_PROMETHEUS_RW_HEADERS_X-Scope-OrgID": "my-org-id-old-method", + "K6_PROMETHEUS_RW_HTTP_HEADERS": "X-Scope-OrgID:my-org-id,another-header:true,empty:", + }}, //nolint:gocritic //"Arg": {arg: "headers.X-MY-HEADER1=hval1,headers.X-MY-HEADER2=hval2"}, } @@ -266,8 +273,11 @@ func TestOptionHeaders(t *testing.T) { InsecureSkipTLSVerify: null.BoolFrom(false), PushInterval: types.NullDurationFrom(5 * time.Second), Headers: map[string]string{ - "X-MY-HEADER1": "hval1", - "X-MY-HEADER2": "hval2", + "X-MY-HEADER1": "hval1", + "X-MY-HEADER2": "hval2", + "X-Scope-OrgID": "my-org-id", + "another-header": "true", + "empty": "", }, TrendStats: []string{"p(99)"}, StaleMarkers: null.BoolFrom(false),