From 3d75dbc31ef4122e0b7f36688d6bc54a7836e8ff Mon Sep 17 00:00:00 2001 From: ismail simsek Date: Wed, 22 Feb 2023 14:47:50 +0100 Subject: [PATCH] Prometheus: Handle annotation query with empty fields (#63560) Handle annotation query with empty fields --- .../datasource/prometheus/datasource.test.ts | 39 +++++++++++++++++++ .../datasource/prometheus/datasource.tsx | 3 ++ 2 files changed, 42 insertions(+) diff --git a/public/app/plugins/datasource/prometheus/datasource.test.ts b/public/app/plugins/datasource/prometheus/datasource.test.ts index 8e8cce4aed7..402f982407d 100644 --- a/public/app/plugins/datasource/prometheus/datasource.test.ts +++ b/public/app/plugins/datasource/prometheus/datasource.test.ts @@ -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; 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 }; +} diff --git a/public/app/plugins/datasource/prometheus/datasource.tsx b/public/app/plugins/datasource/prometheus/datasource.tsx index ad84f5af598..703d23fb6f8 100644 --- a/public/app/plugins/datasource/prometheus/datasource.tsx +++ b/public/app/plugins/datasource/prometheus/datasource.tsx @@ -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 || {};