CloudWatch: Return with error message for empty logs query string (#59584)

This commit is contained in:
Shirley 2022-12-02 10:01:01 +01:00 committed by GitHub
parent 8152b0e1ef
commit ada0c771ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 11 deletions

View File

@ -6,8 +6,8 @@ import { dateTime, Field } from '@grafana/data';
import {
fieldsVariable,
logGroupNamesVariable,
setupMockedDataSource,
regionVariable,
setupMockedDataSource,
} from './__mocks__/CloudWatchDataSource';
import { setupForLogs } from './__mocks__/logsTestContext';
import { validLogsQuery, validMetricSearchBuilderQuery } from './__mocks__/queries';
@ -165,6 +165,7 @@ describe('datasource', () => {
region: '',
queryMode: 'Logs',
logGroupNames: ['test'],
expression: 'some query',
refId: 'a',
},
],
@ -195,7 +196,7 @@ describe('datasource', () => {
expect(emits[0].data[0].fields.find((f: Field) => f.name === '@message').config.links).toMatchObject([
{
title: 'View in CloudWatch console',
url: "https://us-west-1.console.aws.amazon.com/cloudwatch/home?region=us-west-1#logs-insights:queryDetail=~(end~'2020-12-31T19*3a00*3a00.000Z~start~'2020-12-31T19*3a00*3a00.000Z~timeType~'ABSOLUTE~tz~'UTC~editorString~'~isLiveTail~false~source~(~'test))",
url: "https://us-west-1.console.aws.amazon.com/cloudwatch/home?region=us-west-1#logs-insights:queryDetail=~(end~'2020-12-31T19*3a00*3a00.000Z~start~'2020-12-31T19*3a00*3a00.000Z~timeType~'ABSOLUTE~tz~'UTC~editorString~'some*20query~isLiveTail~false~source~(~'test))",
},
]);
});

View File

@ -1,10 +1,18 @@
import { interval, lastValueFrom, of } from 'rxjs';
import { LogRowModel, MutableDataFrame, FieldType, LogLevel, dataFrameToJSON, DataQueryErrorType } from '@grafana/data';
import {
dataFrameToJSON,
DataQueryErrorType,
DataQueryRequest,
FieldType,
LogLevel,
LogRowModel,
MutableDataFrame,
} from '@grafana/data';
import { genMockFrames, setupMockedLogsQueryRunner } from '../__mocks__/LogsQueryRunner';
import { validLogsQuery } from '../__mocks__/queries';
import { LogAction } from '../types';
import { CloudWatchQuery, LogAction } from '../types';
import * as rxjsUtils from '../utils/rxjs/increasingInterval';
import { LOG_IDENTIFIER_INTERNAL, LOGSTREAM_IDENTIFIER_INTERNAL } from './CloudWatchLogsQueryRunner';
@ -213,4 +221,32 @@ describe('CloudWatchLogsQueryRunner', () => {
expect(i).toBe(3);
});
});
describe('handleLogQueries', () => {
it('should return error message when missing query string', async () => {
const { runner } = setupMockedLogsQueryRunner();
const response = await lastValueFrom(
runner.handleLogQueries(
[
{
datasource: { type: 'cloudwatch', uid: 'Zne6OZIVk' },
id: '',
logGroups: [{ label: '', text: '', value: '' }],
queryMode: 'Logs',
refId: 'A',
region: 'default',
},
],
{ scopedVars: {} } as DataQueryRequest<CloudWatchQuery>
)
);
expect(response).toEqual({
data: [],
error: {
message: 'Query is required',
},
});
});
});
});

View File

@ -1,20 +1,20 @@
import { set } from 'lodash';
import {
Observable,
of,
mergeMap,
map,
from,
catchError,
concatMap,
finalize,
from,
lastValueFrom,
map,
mergeMap,
Observable,
of,
repeat,
scan,
share,
takeWhile,
tap,
zip,
catchError,
lastValueFrom,
} from 'rxjs';
import {
@ -108,6 +108,14 @@ export class CloudWatchLogsQueryRunner extends CloudWatchRequest {
return of({ data: [], error: { message: 'Log group is required' } });
}
const hasQueryWithMissingQueryString = queryParams.some((qp) => {
return qp.queryString.length === 0;
});
if (hasQueryWithMissingQueryString) {
return of({ data: [], error: { message: 'Query is required' } });
}
const startTime = new Date();
const timeoutFunc = () => {
return Date.now() >= startTime.valueOf() + rangeUtil.intervalToMs(this.logsTimeout);