CloudWatch Logs: Fix interpolation of scoped variables in queryString (#64267)

This commit is contained in:
Isabella Siu 2023-03-08 08:56:44 -05:00 committed by GitHub
parent ee38bbe030
commit af9a0dbe39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View File

@ -24,7 +24,7 @@ export const LogsRequestMock: DataQueryRequest<CloudWatchLogsQuery> = {
requestId: '',
interval: '',
intervalMs: 0,
scopedVars: {},
scopedVars: { __interval: { value: '20s' } },
timezone: '',
app: '',
startTime: 0,

View File

@ -216,6 +216,16 @@ describe('CloudWatchLogsQueryRunner', () => {
expression: `fields @timestamp, @message | sort @timestamp desc | limit 1`,
};
const logsScopedVarQuery: CloudWatchLogsQuery = {
queryMode: 'Logs',
logGroups: [{ arn: logGroupNamesVariable.name, name: logGroupNamesVariable.name }],
hide: false,
id: '',
region: '$' + regionVariable.name,
refId: 'A',
expression: `stats count(*) by queryType, bin($__interval)`,
};
describe('handleLogQueries', () => {
it('should map log queries to start query requests correctly', async () => {
const { runner } = setupMockedLogsQueryRunner({
@ -229,7 +239,9 @@ describe('CloudWatchLogsQueryRunner', () => {
},
});
const spy = jest.spyOn(runner, 'makeLogActionRequest');
await lastValueFrom(runner.handleLogQueries([legacyLogGroupNamesQuery, logGroupNamesQuery], LogsRequestMock));
await lastValueFrom(
runner.handleLogQueries([legacyLogGroupNamesQuery, logGroupNamesQuery, logsScopedVarQuery], LogsRequestMock)
);
const startQueryRequests: StartQueryRequest[] = [
{
queryString: `fields @timestamp, @message | sort @timestamp desc | limit ${limitVariable.current.value}`,
@ -251,6 +263,13 @@ describe('CloudWatchLogsQueryRunner', () => {
refId: legacyLogGroupNamesQuery.refId,
region: regionVariable.current.value as string,
},
{
queryString: `stats count(*) by queryType, bin(20s)`,
logGroupNames: [],
logGroups: [...(logGroupNamesVariable.current.value as string[]).map((v) => ({ arn: v, name: v }))],
refId: legacyLogGroupNamesQuery.refId,
region: regionVariable.current.value as string,
},
];
expect(spy).toHaveBeenNthCalledWith(1, 'StartQuery', startQueryRequests);
});

View File

@ -103,7 +103,7 @@ export class CloudWatchLogsQueryRunner extends CloudWatchRequest {
return {
refId: target.refId,
region: this.templateSrv.replace(this.getActualRegion(target.region)),
queryString: this.templateSrv.replace(target.expression || ''),
queryString: this.templateSrv.replace(target.expression || '', options.scopedVars),
logGroups,
logGroupNames,
};