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

Handle annotation query with empty fields
This commit is contained in:
ismail simsek 2023-02-22 14:47:50 +01:00 committed by GitHub
parent f925a989d3
commit 3d75dbc31e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -191,6 +191,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 }) },
@ -975,6 +976,19 @@ describe('PrometheusDatasource2', () => {
} as unknown as AnnotationQueryRequest<PromQuery>;
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 () => {
@ -2399,3 +2413,28 @@ function createAnnotationResponse() {
return { ...response };
}
function createEmptyAnnotationResponse() {
const response = {
data: {
results: {
X: {
frames: [
{
schema: {
name: 'bar',
refId: 'X',
fields: [],
},
data: {
values: [],
},
},
],
},
},
},
};
return { ...response };
}

View File

@ -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 || {};