Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit e7be89d

Browse files
authored
New format for custom HTTP headers (#147)
A new format for passing custom HTTP headers using the same syntax as used by InfluxDB output in k6 core.
1 parent 8f2caa7 commit e7be89d

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

pkg/remotewrite/config.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, _
180180
}
181181

182182
func parseEnvs(env map[string]string) (Config, error) {
183-
var c Config
183+
c := Config{
184+
Headers: make(map[string]string),
185+
}
184186

185187
getEnvBool := func(env map[string]string, name string) (null.Bool, error) {
186188
if v, vDefined := env[name]; vDefined {
@@ -231,12 +233,19 @@ func parseEnvs(env map[string]string) (Config, error) {
231233

232234
envHeaders := getEnvMap(env, "K6_PROMETHEUS_RW_HEADERS_")
233235
for k, v := range envHeaders {
234-
if c.Headers == nil {
235-
c.Headers = make(map[string]string)
236-
}
237236
c.Headers[k] = v
238237
}
239238

239+
if headers, headersDefined := env["K6_PROMETHEUS_RW_HTTP_HEADERS"]; headersDefined {
240+
for _, kvPair := range strings.Split(headers, ",") {
241+
header := strings.Split(kvPair, ":")
242+
if len(header) != 2 {
243+
return c, fmt.Errorf("the provided header (%s) does not respect the expected format <header key>:<value>", kvPair)
244+
}
245+
c.Headers[header[0]] = header[1]
246+
}
247+
}
248+
240249
if b, err := getEnvBool(env, "K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM"); err != nil {
241250
return c, err
242251
} else if b.Valid {

pkg/remotewrite/config_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,15 @@ func TestOptionHeaders(t *testing.T) {
255255
env map[string]string
256256
jsonRaw json.RawMessage
257257
}{
258-
"JSON": {jsonRaw: json.RawMessage(`{"headers":{"X-MY-HEADER1":"hval1","X-MY-HEADER2":"hval2"}}`)},
259-
"Env": {env: map[string]string{"K6_PROMETHEUS_RW_HEADERS_X-MY-HEADER1": "hval1", "K6_PROMETHEUS_RW_HEADERS_X-MY-HEADER2": "hval2"}},
258+
"JSON": {jsonRaw: json.RawMessage(
259+
`{"headers":{"X-MY-HEADER1":"hval1","X-MY-HEADER2":"hval2","X-Scope-OrgID":"my-org-id","another-header":"true","empty":""}}`)},
260+
"Env": {env: map[string]string{
261+
"K6_PROMETHEUS_RW_HEADERS_X-MY-HEADER1": "hval1",
262+
"K6_PROMETHEUS_RW_HEADERS_X-MY-HEADER2": "hval2",
263+
// it assert that the new method using HTTP_HEADERS overwrites it
264+
"K6_PROMETHEUS_RW_HEADERS_X-Scope-OrgID": "my-org-id-old-method",
265+
"K6_PROMETHEUS_RW_HTTP_HEADERS": "X-Scope-OrgID:my-org-id,another-header:true,empty:",
266+
}},
260267
//nolint:gocritic
261268
//"Arg": {arg: "headers.X-MY-HEADER1=hval1,headers.X-MY-HEADER2=hval2"},
262269
}
@@ -266,8 +273,11 @@ func TestOptionHeaders(t *testing.T) {
266273
InsecureSkipTLSVerify: null.BoolFrom(false),
267274
PushInterval: types.NullDurationFrom(5 * time.Second),
268275
Headers: map[string]string{
269-
"X-MY-HEADER1": "hval1",
270-
"X-MY-HEADER2": "hval2",
276+
"X-MY-HEADER1": "hval1",
277+
"X-MY-HEADER2": "hval2",
278+
"X-Scope-OrgID": "my-org-id",
279+
"another-header": "true",
280+
"empty": "",
271281
},
272282
TrendStats: []string{"p(99)"},
273283
StaleMarkers: null.BoolFrom(false),

0 commit comments

Comments
 (0)