diff --git a/public/app/plugins/datasource/prometheus/add_label_to_query.test.ts b/public/app/plugins/datasource/prometheus/add_label_to_query.test.ts index a33160f331d..7ef4b7effd7 100644 --- a/public/app/plugins/datasource/prometheus/add_label_to_query.test.ts +++ b/public/app/plugins/datasource/prometheus/add_label_to_query.test.ts @@ -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"}' diff --git a/public/app/plugins/datasource/prometheus/add_label_to_query.ts b/public/app/plugins/datasource/prometheus/add_label_to_query.ts index 147ed81fbdd..bad3e696c38 100644 --- a/public/app/plugins/datasource/prometheus/add_label_to_query.ts +++ b/public/app/plugins/datasource/prometheus/add_label_to_query.ts @@ -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); diff --git a/public/app/plugins/datasource/prometheus/result_transformer.test.ts b/public/app/plugins/datasource/prometheus/result_transformer.test.ts index 373de506556..206ae55c4b4 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.test.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.test.ts @@ -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', () => { diff --git a/public/app/plugins/datasource/prometheus/result_transformer.ts b/public/app/plugins/datasource/prometheus/result_transformer.ts index 5bd5c435d07..5b45bff527a 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.ts @@ -132,7 +132,11 @@ export class ResultTransformer { for (j = 0; j < sortedLabels.length; j++) { const label = sortedLabels[j]; if (series.metric.hasOwnProperty(label)) { - reordered.push(series.metric[label]); + if (label === 'le') { + reordered.push(parseHistogramLabel(series.metric[label])); + } else { + reordered.push(series.metric[label]); + } } else { reordered.push(''); }