Skip to content

Commit

Permalink
k6: Pass auth to write path via headers not URL (#6915)
Browse files Browse the repository at this point in the history
This changes the k6 load testing script to pass authentication for remote
writes via headers (same as we do for reads) instead of as part of the URL.
  • Loading branch information
56quarters authored Dec 12, 2023
1 parent b9d91bd commit 1f903e1
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions operations/k6/load-testing-with-k6.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ const TENANT_ID = __ENV.K6_TENANT_ID || '';
const remote_write_url = get_remote_write_url();
console.debug("Remote write URL:", remote_write_url)

const write_client = new remote.Client({ url: remote_write_url, timeout: '32s', tenant_name: TENANT_ID });
const write_client_headers = get_write_authentication_headers()

const write_client = new remote.Client({ url: remote_write_url, timeout: '32s', tenant_name: TENANT_ID, headers: write_client_headers });

const query_client_headers = {
'User-Agent': 'k6-load-test',
Expand Down Expand Up @@ -483,10 +485,6 @@ function align_timestamp_to_step(ts, step) {
* @returns {string}
*/
function get_remote_write_url() {
if (USERNAME !== '' || WRITE_TOKEN !== '') {
return `${SCHEME}://${USERNAME}:${WRITE_TOKEN}@${WRITE_HOSTNAME}/api/v1/push`;
}

return `${SCHEME}://${WRITE_HOSTNAME}/api/v1/push`;
}

Expand All @@ -509,6 +507,25 @@ function get_read_authentication_headers() {
return auth_headers;
}

/**
* Returns the HTTP Authentication header to use on the write path.
* @returns {map}
*/
function get_write_authentication_headers() {
let auth_headers = new Map();

if (USERNAME !== '' || WRITE_TOKEN !== '') {
auth_headers.set('Authorization', `Basic ${encoding.b64encode(`${USERNAME}:${WRITE_TOKEN}`)}`)
}

if (TENANT_ID !== '') {
auth_headers.set('X-Scope-OrgID', TENANT_ID)

}

return auth_headers;
}

/**
* Runs a range query randomly generated based on the configured distribution defined in range_query_distribution.
* It validate that a successful response is received and tags requests with { type: "read" } so that requests can be distinguished from writes.
Expand Down

0 comments on commit 1f903e1

Please sign in to comment.