[v9.4.x] Prometheus: Handle annotation query with empty fields (#63574)

Prometheus: Handle annotation query with empty fields (#63560)

Handle annotation query with empty fields

(cherry picked from commit 3d75dbc31e)

Co-authored-by: ismail simsek <ismailsimsek09@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2023-02-22 14:57:47 +01:00
committed by GitHub
parent 045aff4009
commit 9eefd2e42a
2 changed files with 42 additions and 0 deletions
@@ -185,6 +185,7 @@ describe('PrometheusDatasource', () => {
describe('customQueryParams', () => {
const target = { expr: 'test{job="testjob"}', format: 'time_series', refId: '' };
function makeQuery(target: PromQuery) {
return {
range: { from: time({ seconds: 63 }), to: time({ seconds: 183 }) },
@@ -977,6 +978,19 @@ describe('PrometheusDatasource2', () => {
};
const response = createAnnotationResponse();
const emptyResponse = createEmptyAnnotationResponse();
describe('handle result with empty fields', () => {
it('should return empty results', async () => {
fetchMock.mockImplementation(() => of(emptyResponse));
await ds.annotationQuery(options).then((data) => {
results = data;
});
expect(results.length).toBe(0);
});
});
describe('when time series query is cancelled', () => {
it('should return empty results', async () => {
@@ -2401,3 +2415,28 @@ function createAnnotationResponse() {
return { ...response };
}
function createEmptyAnnotationResponse() {
const response = {
data: {
results: {
X: {
frames: [
{
schema: {
name: 'bar',
refId: 'X',
fields: [],
},
data: {
values: [],
},
},
],
},
},
},
};
return { ...response };
}
@@ -830,6 +830,9 @@ export class PrometheusDatasource
const eventList: AnnotationEvent[] = [];
for (const frame of frames) {
if (frame.fields.length === 0) {
continue;
}
const timeField = frame.fields[0];
const valueField = frame.fields[1];
const labels = valueField?.labels || {};