Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Custom threshold] Fix adding ECS groups multiple times to recovered alert context #188629

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,7 @@ describe('The custom threshold alert type', () => {
});

describe('querying recovered alert with a count aggregator', () => {
beforeEach(() => jest.clearAllMocks());
afterAll(() => clearInstances());
const execute = (comparator: COMPARATORS, threshold: number[], sourceId: string = 'default') =>
executor({
Expand All @@ -1509,20 +1510,26 @@ describe('The custom threshold alert type', () => {
threshold,
},
],
groupBy: ['host.name'],
},
});
test('alerts based on the doc_count value instead of the aggregatedValue', async () => {
test('generates viewInAppUrl for active alerts correctly', async () => {
setEvaluationResults([
{
'*': {
a: {
...customThresholdCountCriterion,
comparator: COMPARATORS.GREATER_THAN,
threshold: [0.9],
currentValue: 1,
timestamp: new Date().toISOString(),
shouldFire: true,
isNoData: false,
bucketKey: { groupBy0: 'a' },
bucketKey: { 'host.name': 'a' },
context: {
host: {
name: 'a',
},
},
},
},
]);
Expand All @@ -1539,8 +1546,110 @@ describe('The custom threshold alert type', () => {
});
await execute(COMPARATORS.GREATER_THAN, [0.9]);
const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
expect(services.alertsClient.setAlertData).toBeCalledTimes(1);
expect(services.alertsClient.setAlertData).toBeCalledWith({
context: {
alertDetailsUrl: 'http://localhost:5601/app/observability/alerts/uuid-a',
viewInAppUrl: 'mockedViewInApp',
group: [
{
field: 'host.name',
value: 'a',
},
],
host: {
name: 'a',
},
reason:
'Document count is 1, above the threshold of 0.9. (duration: 1 min, data view: mockedDataViewName, group: a)',
tags: [],
value: ['1'],
timestamp: expect.stringMatching(ISO_DATE_REGEX),
},
id: 'a',
});
expect(getViewInAppUrl).lastCalledWith({
dataViewId: 'c34a7c79-a88b-4b4a-ad19-72f6d24104e4',
groups: [
{
field: 'host.name',
value: 'a',
},
],
logsExplorerLocator: undefined,
metrics: customThresholdCountCriterion.metrics,
startedAt: expect.stringMatching(ISO_DATE_REGEX),
searchConfiguration: {
index: {},
query: {
query: mockQuery,
language: 'kuery',
},
},
});
});

test('includes group by information in the recovered alert document', async () => {
setEvaluationResults([{}]);
const mockedSetContext = jest.fn();
services.alertsClient.getRecoveredAlerts.mockImplementation((params: any) => {
return [
{
alert: {
meta: [],
state: [],
context: {},
id: 'host-0',
getId: jest.fn().mockReturnValue('host-0'),
getUuid: jest.fn().mockReturnValue('mockedUuid'),
getStart: jest.fn().mockReturnValue('2024-07-18T08:09:05.697Z'),
},
hit: {
'host.name': 'host-0',
},
},
];
});
services.alertFactory.done.mockImplementation(() => {
return {
getRecoveredAlerts: jest.fn().mockReturnValue([
{
setContext: mockedSetContext,
getId: jest.fn().mockReturnValue('mockedId'),
},
]),
};
});
await execute(COMPARATORS.GREATER_THAN, [0.9]);
const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
expect(services.alertsClient.setAlertData).toBeCalledTimes(1);
expect(services.alertsClient.setAlertData).toBeCalledWith({
context: {
alertDetailsUrl: 'http://localhost:5601/app/observability/alerts/mockedUuid',
viewInAppUrl: 'mockedViewInApp',
group: [
{
field: 'host.name',
value: 'host-0',
},
],
host: {
name: 'host-0',
},
timestamp: expect.stringMatching(ISO_DATE_REGEX),
},
id: 'host-0',
'host.name': 'host-0',
});
expect(getViewInAppUrl).toBeCalledTimes(1);
expect(getViewInAppUrl).toBeCalledWith({
dataViewId: 'c34a7c79-a88b-4b4a-ad19-72f6d24104e4',
groups: [
{
field: 'host.name',
value: 'host-0',
},
],
logsExplorerLocator: undefined,
metrics: customThresholdCountCriterion.metrics,
startedAt: expect.stringMatching(ISO_DATE_REGEX),
Expand Down Expand Up @@ -1638,7 +1747,7 @@ describe('The custom threshold alert type', () => {
});
});

describe('alerts with NO_DATA where one condtion is an aggregation and the other is a document count', () => {
describe('alerts with NO_DATA where one condition is an aggregation and the other is a document count', () => {
afterAll(() => clearInstances());
const instanceID = '*';
const execute = (alertOnNoData: boolean, sourceId: string = 'default') =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ export const createCustomThresholdExecutor = ({
startedAt: indexedStartedAt,
}),
...additionalContext,
...getEcsGroups(group),
};

alertsClient.setAlertData({
id: recoveredAlertId,
context,
...getEcsGroups(group),
});
}

Expand Down
Loading