Skip to content

Commit

Permalink
New format for passing custom HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Sep 22, 2023
1 parent 58a6576 commit e3241b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 13 additions & 0 deletions pkg/remotewrite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ func parseEnvs(env map[string]string) (Config, error) {
c.Headers[k] = v
}

if headers, headersDefined := env["K6_PROMETHEUS_RW_HTTP_HEADERS"]; headersDefined {
if c.Headers == nil {
c.Headers = make(map[string]string)
}
for _, kvPair := range strings.Split(headers, ",") {
header := strings.Split(kvPair, ":")
if len(header) != 2 {
continue
}
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
17 changes: 13 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"}}`)},
"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",
}},
//nolint:gocritic
//"Arg": {arg: "headers.X-MY-HEADER1=hval1,headers.X-MY-HEADER2=hval2"},
}
Expand All @@ -266,8 +273,10 @@ 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",
},
TrendStats: []string{"p(99)"},
StaleMarkers: null.BoolFrom(false),
Expand Down

0 comments on commit e3241b8

Please sign in to comment.