Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,7 @@ class PrometheusQueryHelper {
* @returns {String} the string query to use
*/
static buildStringRangeQuery(query, startTime, endTime, step) {
// Anything that is within `{ }` must be URI encoded (including braces)

const myRegexp = /({.*})/;
let match = myRegexp.exec(query);
while (match !== null) {
query = query.replace(myRegexp, encodeURIComponent(match[0]));
match = myRegexp.exec(query);
}

const builtQuery = 'query_range?query=' + query + '&start=' + startTime + '&end=' + endTime + '&step=' + step;
const builtQuery = 'query_range?query=' + encodeURIComponent(query) + '&start=' + startTime + '&end=' + endTime + '&step=' + step;
return builtQuery;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ describe('PrometheusQueryHelper implementation', () => {
const endTime = 200;
const step = 45;

it('should preserve arithmetic operators and quoted URL characters', () => {
const query = 'sum(up{job="a&b#50%",name=~".+"}) + 1';
const output = PrometheusQueryHelper.buildStringRangeQuery(query, startTime, endTime, step);
const params = new URL(output, 'http://localhost/api/v1/').searchParams;

params.get('query').should.equal(query);
params.get('start').should.equal('100');
params.get('end').should.equal('200');
params.get('step').should.equal('45');
Array.from(params.keys()).should.deep.equal(['query', 'start', 'end', 'step']);
});

it('should add start, end and step information', () => {
const demoString = 'test_the_helper';

Expand Down