Commit c0c06e4d by David Kaltschmidt

Prometheus: Make result transformer more robust for empty responses

- some prometheus-compatible backends may return `null` instead of empty lists
- test for lists before iterating
- added tests for empty responses
parent dbb34020
...@@ -16,7 +16,7 @@ export class ResultTransformer { ...@@ -16,7 +16,7 @@ export class ResultTransformer {
options.valueWithRefId options.valueWithRefId
), ),
]; ];
} else if (options.format === 'heatmap') { } else if (prometheusResult && options.format === 'heatmap') {
let seriesList = []; let seriesList = [];
prometheusResult.sort(sortSeriesByLabel); prometheusResult.sort(sortSeriesByLabel);
for (const metricData of prometheusResult) { for (const metricData of prometheusResult) {
...@@ -24,7 +24,7 @@ export class ResultTransformer { ...@@ -24,7 +24,7 @@ export class ResultTransformer {
} }
seriesList = this.transformToHistogramOverTime(seriesList); seriesList = this.transformToHistogramOverTime(seriesList);
return seriesList; return seriesList;
} else { } else if (prometheusResult) {
const seriesList = []; const seriesList = [];
for (const metricData of prometheusResult) { for (const metricData of prometheusResult) {
if (response.data.data.resultType === 'matrix') { if (response.data.data.resultType === 'matrix') {
...@@ -82,7 +82,7 @@ export class ResultTransformer { ...@@ -82,7 +82,7 @@ export class ResultTransformer {
let i, j; let i, j;
const metricLabels = {}; const metricLabels = {};
if (md.length === 0) { if (!md || md.length === 0) {
return table; return table;
} }
......
...@@ -10,6 +10,31 @@ describe('Prometheus Result Transformer', () => { ...@@ -10,6 +10,31 @@ describe('Prometheus Result Transformer', () => {
ctx.resultTransformer = new ResultTransformer(ctx.templateSrv); ctx.resultTransformer = new ResultTransformer(ctx.templateSrv);
}); });
describe('When nothing is returned', () => {
test('should return empty series', () => {
const response = {
status: 'success',
data: {
resultType: '',
result: null,
},
};
const series = ctx.resultTransformer.transform({ data: response }, {});
expect(series).toEqual([]);
});
test('should return empty table', () => {
const response = {
status: 'success',
data: {
resultType: '',
result: null,
},
};
const table = ctx.resultTransformer.transform({ data: response }, { format: 'table' });
expect(table).toMatchObject([{ type: 'table', rows: [] }]);
});
});
describe('When resultFormat is table', () => { describe('When resultFormat is table', () => {
const response = { const response = {
status: 'success', status: 'success',
......
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