Commit f33158dc by Zoltán Bedi Committed by GitHub

Prometheus: parse value as number when label is le (#26375)

* Prometheus: parse value as number when label is le

* Convert prometheus table value back to string for modify query
parent 7896836d
......@@ -24,6 +24,12 @@ describe('addLabelToQuery()', () => {
expect(addLabelToQuery('sum by (xx) (foo)', 'bar', 'baz')).toBe('sum by (xx) (foo{bar="baz"})');
});
it('should convert number Infinity to +Inf', () => {
expect(
addLabelToQuery('sum(rate(prometheus_tsdb_compaction_chunk_size_bytes_bucket[5m])) by (le)', 'le', Infinity)
).toBe('sum(rate(prometheus_tsdb_compaction_chunk_size_bytes_bucket{le="+Inf"}[5m])) by (le)');
});
it('should handle selectors with punctuation', () => {
expect(addLabelToQuery('foo{instance="my-host.com:9100"}', 'bar', 'baz')).toBe(
'foo{bar="baz",instance="my-host.com:9100"}'
......
......@@ -21,7 +21,7 @@ const selectorRegexp = /{([^{]*)}/g;
export function addLabelToQuery(
query: string,
key: string,
value: string,
value: string | number,
operator?: string,
hasNoMetrics?: boolean
): string {
......@@ -29,6 +29,9 @@ export function addLabelToQuery(
throw new Error('Need label to add to query.');
}
// We need to make sure that we convert the value back to string because it may be a number
const transformedValue = value === Infinity ? '+Inf' : value.toString();
// Add empty selectors to bare metric names
let previousWord: string;
query = query.replace(metricNameRegexp, (match, word, offset) => {
......@@ -65,7 +68,7 @@ export function addLabelToQuery(
while (match) {
const prefix = query.slice(lastIndex, match.index);
const selector = match[1];
const selectorWithLabel = addLabelToSelector(selector, key, value, operator);
const selectorWithLabel = addLabelToSelector(selector, key, transformedValue, operator);
lastIndex = match.index + match[1].length + 2;
suffix = query.slice(match.index + match[0].length);
parts.push(prefix, selectorWithLabel);
......
......@@ -114,6 +114,17 @@ describe('Prometheus Result Transformer', () => {
{ text: 'Value' },
]);
});
it('should return table model with le label values parsed as numbers', () => {
const table = ctx.resultTransformer.transformMetricDataToTable([
{
metric: { le: '102' },
value: [1594908838, '0'],
},
]);
expect(table.type).toBe('table');
expect(table.rows).toEqual([[1594908838000, 102, 0]]);
});
});
describe('When resultFormat is time series and instant = true', () => {
......
......@@ -132,7 +132,11 @@ export class ResultTransformer {
for (j = 0; j < sortedLabels.length; j++) {
const label = sortedLabels[j];
if (series.metric.hasOwnProperty(label)) {
if (label === 'le') {
reordered.push(parseHistogramLabel(series.metric[label]));
} else {
reordered.push(series.metric[label]);
}
} else {
reordered.push('');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment