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

New format for custom HTTP headers #147

Merged
merged 2 commits into from
Sep 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions pkg/remotewrite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 <header key>:<value>", 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 {
Expand Down
18 changes: 14 additions & 4 deletions pkg/remotewrite/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
Expand All @@ -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),
Expand Down
Loading