CloudWatch: Fix - make sure dimensions are propagated to alert query editor (#58281)

* fix(unified-alerting/cloudwatch): propagate dimensions to alert

Signed-off-by: Conor Evans <coevans@tcd.ie>

* add unit test

Signed-off-by: Conor Evans <coevans@tcd.ie>
Co-authored-by: Erik Sundell <erik.sundell87@gmail.com>
This commit is contained in:
Conor Evans 2022-12-07 08:42:04 +00:00 committed by GitHub
parent 0801fce23c
commit dce7f50d59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 8 deletions

View File

@ -55,6 +55,24 @@ describe('Dimensions', () => {
});
});
describe('when rendered with two existing dimensions and values are represented as arrays', () => {
it('should render two filter items', async () => {
props.query.dimensions = {
InstanceId: ['*'],
InstanceGroup: ['Group1'],
};
render(<Dimensions {...props} metricStat={props.query} dimensionKeys={[]} />);
const filterItems = screen.getAllByTestId('cloudwatch-dimensions-filter-item');
expect(filterItems.length).toBe(2);
expect(within(filterItems[0]).getByText('InstanceId')).toBeInTheDocument();
expect(within(filterItems[0]).getByText('*')).toBeInTheDocument();
expect(within(filterItems[1]).getByText('InstanceGroup')).toBeInTheDocument();
expect(within(filterItems[1]).getByText('Group1')).toBeInTheDocument();
});
});
describe('when adding a new filter item', () => {
it('it should add the new item but not call onChange', async () => {
props.query.dimensions = {};

View File

@ -25,15 +25,32 @@ export interface DimensionFilterCondition {
const dimensionsToFilterConditions = (dimensions: DimensionsType | undefined) =>
Object.entries(dimensions ?? {}).reduce<DimensionFilterCondition[]>((acc, [key, value]) => {
if (value && typeof value === 'string') {
const filter = {
key,
value,
operator: '=',
};
return [...acc, filter];
if (!value) {
return acc;
}
return acc;
// Previously, we only appended to the `acc`umulated dimensions if the value was a string.
// However, Cloudwatch can present dimensions with single-value arrays, e.g.
// k: FunctionName
// v: ['MyLambdaFunction']
// in which case we grab the single-value from the Array and use that as the value.
let v = '';
if (typeof value === 'string') {
v = value;
} else if (Array.isArray(value) && typeof value[0] === 'string') {
v = value[0];
}
if (!v) {
return acc;
}
const filter = {
key: key,
value: v,
operator: '=',
};
return [...acc, filter];
}, []);
const filterConditionsToDimensions = (filters: DimensionFilterCondition[]) => {