github.com/grafana/pyroscope@v1.18.0/tools/k6/lib/request.js (about) 1 import { check } from 'k6'; 2 import http from 'k6/http'; 3 import encoding from 'k6/encoding'; 4 5 import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'; 6 7 import { READ_TOKEN, TENANT_ID, BASE_URL } from './env.js'; 8 9 export function doSelectMergeProfileRequest(body, headers) { 10 const res = http.post(`${BASE_URL}/querier.v1.QuerierService/SelectMergeProfile`, JSON.stringify(body), { 11 headers: withHeaders(headers), 12 tags: { name: "querier.v1.QuerierService/SelectMergeProfile" } 13 }); 14 15 check(res, { 16 'status is 200': (r) => r.status === 200, 17 }); 18 } 19 20 export function doRenderRequest(body, headers) { 21 const params = new URL(`${BASE_URL}/pyroscope/render`); 22 for (const [k, v] of Object.entries(body)) { 23 params.searchParams.set(k, v); 24 } 25 26 const res = http.get(params.toString(), { 27 headers: withHeaders(headers), 28 tags: { name: '/render' }, 29 }); 30 31 check(res, { 32 'status is 200': (r) => r.status === 200, 33 }); 34 } 35 36 export function doSelectMergeStacktracesRequest(body, headers) { 37 const res = http.post(`${BASE_URL}/querier.v1.QuerierService/SelectMergeStacktraces`, JSON.stringify(body), { 38 headers: withHeaders(headers), 39 tags: { name: "querier.v1.QuerierService/SelectMergeStacktraces" } 40 }); 41 42 check(res, { 43 'status is 200': (r) => r.status === 200, 44 }); 45 } 46 47 export function doLabelNamesRequest(body, headers) { 48 const res = http.post(`${BASE_URL}/querier.v1.QuerierService/LabelNames`, JSON.stringify(body), { 49 headers: withHeaders(headers), 50 tags: { name: "querier.v1.QuerierService/LabelNames" } 51 }); 52 53 check(res, { 54 'status is 200': (r) => r.status === 200, 55 }); 56 } 57 58 export function doSeriesRequest(body, headers) { 59 const res = http.post(`${BASE_URL}/querier.v1.QuerierService/Series`, JSON.stringify(body), { 60 headers: withHeaders(headers), 61 tags: { name: "querier.v1.QuerierService/Series" } 62 }); 63 64 check(res, { 65 'status is 200': (r) => r.status === 200, 66 }); 67 } 68 69 export function doRenderDiffRequest(body, headers) { 70 const params = new URL(`${BASE_URL}/pyroscope/render-diff`); 71 for (const [k, v] of Object.entries(body)) { 72 params.searchParams.set(k, v); 73 } 74 75 const res = http.get(params.toString(), { 76 headers: withHeaders(headers), 77 tags: { name: '/render-diff' }, 78 }); 79 80 check(res, { 81 'status is 200': (r) => r.status === 200, 82 }); 83 } 84 85 function withHeaders(headers) { 86 const baseHeaders = { 87 'User-Agent': 'k6-load-test', 88 'Content-Type': 'application/json', 89 'Authorization': `Basic ${encoding.b64encode(`${TENANT_ID}:${READ_TOKEN}`)}` 90 }; 91 92 for (const [k, v] of Object.entries(headers || {})) { 93 baseHeaders[k] = v; 94 } 95 return baseHeaders; 96 }