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

[Alerts] Added API to find all alerts instances by the filters like consumers, status(active, ...), etc. #88224

Closed

Conversation

YulNaumenko
Copy link
Contributor

@YulNaumenko YulNaumenko commented Jan 13, 2021

Current PR includes two alertsClient methods:

  1. /api/alerts/_find_alerts_with_instances_summary designed to return alerts list with summary for its instances by the filter on alert object props and dateStart, dateEnd, instancesStatus and instancesMuted. Example:

Request:
https://localhost:5601/ihp/api/alerts/_find_alerts_with_instances_summary?page=1&per_page=10&search_fields=%5B%22name%22%2C%22consumer%22%5D&search=alerts&default_search_operator=AND&sort_field=name.keyword&date_start=2021-01-15T00:59:40.935Z&instances_status=Active&instances_muted=false

filter options (paging, search, sort, etc) is applied on the alert object and in addition there are an options for instances filtering:
date_start=2021-01-14T00:59:40.935Z
date_end=2021-01-15T00:59:40.935Z
instances_status=Active
instances_muted=false

Response:

{
    "page": 1,
    "perPage": 20,
    "total": 2,
    "data": [
        {
            "id": "ab883630-5aa0-11eb-9dcb-5f8dbbce55fc",
            "name": "test ",
            "tags": [],
            "alertTypeId": ".index-threshold",
            "consumer": "alerts",
            "statusStartDate": "2021-01-15T00:59:40.935Z",
            "statusEndDate": "2021-01-19T22:01:25.484Z",
            "status": "Active",
            "muteAll": false,
            "throttle": null,
            "enabled": true,
            "lastRun": "2021-01-19T22:01:13.244Z",
            "errorMessages": [],
            "instances": {
                "host-14": {
                    "status": "Active",
                    "muted": false,
                    "actionGroupId": "threshold met",
                    "activeStartDate": "2021-01-19T22:00:00.393Z"
                },
                "host-8": {
                    "status": "Active",
                    "muted": false,
                    "actionGroupId": "threshold met",
                    "activeStartDate": "2021-01-19T22:01:13.266Z"
                }
            }
        },
        {
            "id": "d5352830-5aa0-11eb-9dcb-5f8dbbce55fc",
            "name": "test 2",
            "tags": [],
            "alertTypeId": ".index-threshold",
            "consumer": "alerts",
            "statusStartDate": "2021-01-15T00:59:40.935Z",
            "statusEndDate": "2021-01-19T22:01:25.484Z",
            "status": "Active",
            "muteAll": false,
            "throttle": null,
            "enabled": true,
            "lastRun": "2021-01-19T22:01:16.294Z",
            "errorMessages": [],
            "instances": {
                "host-2": {
                    "status": "Active",
                    "muted": false,
                    "actionGroupId": "threshold met",
                    "activeStartDate": "2021-01-19T21:54:00.306Z"
                }
            }
        }
    ]
}

  1. /api/alerts/_find_alerts_with_instances_timeline designed to return alerts list with its instances activity log by the selected filter on alerts props and dateStart, dateEnd, instancesStatus. Example:
    Request:
    https://localhost:5601/pbl/api/alerts/_find_alerts_with_instances_timeline?page=1&per_page=10&search_fields=%5B%22name%22%2C%22consumer%22%5D&search=alerts&default_search_operator=AND&sort_field=name.keyword&date_start=2021-01-19T21:58:54.356Z&date_end=2021-01-19T22:01:13.661Z

filter options (paging, search, sort, etc) is applied on the alert object and in addition there are an options for instances filtering:
date_start=2021-01-14T00:59:40.935Z
date_end=2021-01-15T00:59:40.935Z
instances_status=Active

Response:

{
    "page": 1,
    "perPage": 20,
    "total": 2,
    "data": [
        {
            "id": "ab883630-5aa0-11eb-9dcb-5f8dbbce55fc",
            "notifyWhen": "onActionGroupChange",
            "params": {
                "aggType": "avg",
                "termSize": 3,
                "thresholdComparator": ">",
                "timeWindowSize": 30,
                "timeWindowUnit": "s",
                "groupBy": "top",
                "threshold": [
                    0.6
                ],
                "index": [
                    "es-apm-sys-sim"
                ],
                "timeField": "@timestamp",
                "aggField": "system.cpu.total.norm.pct",
                "termField": "host.name.keyword"
            },
            "consumer": "alerts",
            "alertTypeId": ".index-threshold",
            "schedule": {
                "interval": "30s"
            },
            "actions": [],
            "tags": [],
            "name": "test ",
            "enabled": true,
            "throttle": null,
            "apiKeyOwner": "elastic",
            "createdBy": "elastic",
            "updatedBy": "elastic",
            "muteAll": false,
            "mutedInstanceIds": [
                "host-2"
            ],
            "updatedAt": "2021-01-19T21:55:35.254Z",
            "createdAt": "2021-01-19T21:52:48.975Z",
            "scheduledTaskId": "ac19eee0-5aa0-11eb-9dcb-5f8dbbce55fc",
            "executionStatus": {
                "lastExecutionDate": "2021-01-19T22:46:49.522Z",
                "status": "active"
            },
            "instances": {
                "host-12": [
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:27.348Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:27.351Z"
                    },
                    {
                        "status": "OK",
                        "timeStamp": "2021-01-19T21:59:28.231Z"
                    },
                    {
                        "status": "OK",
                        "timeStamp": "2021-01-19T22:00:00.393Z"
                    }
                ],
                "host-8": [
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:58:54.356Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:58:54.357Z"
                    },
                    {
                        "status": "OK",
                        "timeStamp": "2021-01-19T21:58:55.135Z"
                    },
                    {
                        "status": "OK",
                        "timeStamp": "2021-01-19T21:59:27.348Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:01:13.266Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:01:13.267Z"
                    },
                    {
                        "status": "OK",
                        "timeStamp": "2021-01-19T22:01:13.661Z"
                    }
                ]
            }
        },
        {
            "id": "d5352830-5aa0-11eb-9dcb-5f8dbbce55fc",
            "notifyWhen": "onActionGroupChange",
            "params": {
                "aggType": "avg",
                "termSize": 2,
                "thresholdComparator": ">",
                "timeWindowSize": 5,
                "timeWindowUnit": "m",
                "groupBy": "top",
                "threshold": [
                    0.4
                ],
                "index": [
                    "es-apm-sys-sim"
                ],
                "timeField": "@timestamp",
                "aggField": "system.cpu.total.norm.pct",
                "termField": "host.name.keyword"
            },
            "consumer": "alerts",
            "alertTypeId": ".index-threshold",
            "schedule": {
                "interval": "10s"
            },
            "actions": [
                {
                    "actionTypeId": ".server-log",
                    "group": "threshold met",
                    "params": {
                        "level": "info",
                        "message": "alert '{{alertName}}' is active for group '{{context.group}}':\n\n- Value: {{context.value}}\n- Conditions Met: {{context.conditions}} over {{params.timeWindowSize}}{{params.timeWindowUnit}}\n- Timestamp: {{context.date}}"
                    },
                    "id": "a970a0d0-5aa0-11eb-9dcb-5f8dbbce55fc"
                }
            ],
            "tags": [],
            "name": "test 2",
            "enabled": true,
            "throttle": null,
            "apiKeyOwner": "elastic",
            "createdBy": "elastic",
            "updatedBy": "elastic",
            "muteAll": false,
            "mutedInstanceIds": [
                "host-3"
            ],
            "updatedAt": "2021-01-19T21:55:48.368Z",
            "createdAt": "2021-01-19T21:53:59.128Z",
            "scheduledTaskId": "d6034da0-5aa0-11eb-9dcb-5f8dbbce55fc",
            "executionStatus": {
                "lastExecutionDate": "2021-01-19T22:47:13.562Z",
                "status": "active"
            },
            "instances": {
                "host-2": [
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:00.364Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:12.352Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:24.527Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:36.363Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:48.384Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:00:00.397Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:00:12.362Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:00:40.244Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:00:53.192Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:01:04.210Z"
                    }
                ],
                "host-3": [
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:00.365Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:12.352Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:24.527Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:36.365Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T21:59:48.384Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:00:00.397Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:00:12.362Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:00:40.245Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:00:53.192Z"
                    },
                    {
                        "status": "Active",
                        "timeStamp": "2021-01-19T22:01:04.211Z"
                    }
                ]
            }
        }
    ]
}


Resolve #70169

@YulNaumenko YulNaumenko self-assigned this Jan 13, 2021
@YulNaumenko YulNaumenko added Feature:Alerting Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v7.12.0 v8.0.0 release_note:skip Skip the PR/issue when compiling release notes labels Jan 18, 2021
@YulNaumenko YulNaumenko marked this pull request as ready for review January 18, 2021 20:58
@YulNaumenko YulNaumenko requested a review from a team as a code owner January 18, 2021 20:58
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-alerting-services (Team:Alerting Services)

@kibanamachine
Copy link
Contributor

kibanamachine commented Jan 20, 2021

💔 Build Failed

Failed CI Steps


Test Failures

Jest Tests.x-pack/plugins/alerts/server/alerts_client/tests.findAlertsWithInstancesSummary() runs as expected with some event log data

Link to Jenkins

Standard Out

Failed Tests Reporter:
  - Test has not failed recently on tracked branches


Stack Trace

Error: expect(received).toMatchInlineSnapshot(snapshot)

Snapshot name: `findAlertsWithInstancesSummary() runs as expected with some event log data 1`

- Snapshot  - 16
+ Received  +  2

@@ -5,36 +5,22 @@
        "consumer": "alert-consumer",
        "enabled": true,
        "errorMessages": Array [],
        "id": "1",
        "instances": Object {
-         "instance-currently-active": Object {
-           "actionGroupId": "action group A",
-           "actionSubgroup": undefined,
-           "activeStartDate": "2019-02-12T21:01:22.479Z",
-           "muted": false,
-           "status": "Active",
-         },
          "instance-muted-no-activity": Object {
            "actionGroupId": undefined,
            "actionSubgroup": undefined,
            "activeStartDate": undefined,
            "muted": true,
-           "status": "OK",
-         },
-         "instance-previously-active": Object {
-           "actionGroupId": undefined,
-           "actionSubgroup": undefined,
-           "activeStartDate": undefined,
-           "muted": false,
            "status": "OK",
          },
        },
-       "lastRun": "2019-02-12T21:01:32.479Z",
+       "lastRun": undefined,
        "muteAll": false,
        "name": "alert-name",
-       "status": "Active",
+       "status": "OK",
        "statusEndDate": "2019-02-12T21:01:22.479Z",
        "statusStartDate": "2019-02-12T21:00:22.479Z",
        "tags": Array [
          "tag-1",
          "tag-2",
    at Object.<anonymous> (/dev/shm/workspace/parallel/7/kibana/x-pack/plugins/alerts/server/alerts_client/tests/find_alerts_with_instances_summary.test.ts:193:20)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at _callCircusTest (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:212:5)
    at _runTest (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:149:3)
    at _runTestsForDescribeBlock (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:63:9)
    at _runTestsForDescribeBlock (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:57:9)
    at run (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:25:3)
    at runAndTransformResultsToJestFormat (/dev/shm/workspace/kibana/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:176:21)
    at jestAdapter (/dev/shm/workspace/kibana/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:109:19)
    at runTestInternal (/dev/shm/workspace/kibana/node_modules/jest-runner/build/runTest.js:380:16)
    at runTest (/dev/shm/workspace/kibana/node_modules/jest-runner/build/runTest.js:472:34)
    at Object.worker (/dev/shm/workspace/kibana/node_modules/jest-runner/build/testWorker.js:133:12)

Jest Tests.x-pack/plugins/alerts/server/alerts_client/tests.findAlertsWithInstancesTimeline() runs as expected with some event log data

Link to Jenkins

Standard Out

Failed Tests Reporter:
  - Test has not failed recently on tracked branches


Stack Trace

TypeError: Cannot destructure property 'page' of '(intermediate value)' as it is undefined.
    at AlertsClient.find (/dev/shm/workspace/parallel/7/kibana/x-pack/plugins/alerts/server/alerts_client/alerts_client.ts:632:7)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at AlertsClient.findAlertsWithInstancesTimeline (/dev/shm/workspace/parallel/7/kibana/x-pack/plugins/alerts/server/alerts_client/alerts_client.ts:545:62)
    at Object.<anonymous> (/dev/shm/workspace/parallel/7/kibana/x-pack/plugins/alerts/server/alerts_client/tests/find_alerts_with_instances_timeline.test.ts:148:20)
    at _callCircusTest (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:212:5)
    at _runTest (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:149:3)
    at _runTestsForDescribeBlock (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:63:9)
    at _runTestsForDescribeBlock (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:57:9)
    at run (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:25:3)
    at runAndTransformResultsToJestFormat (/dev/shm/workspace/kibana/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:176:21)
    at jestAdapter (/dev/shm/workspace/kibana/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:109:19)
    at runTestInternal (/dev/shm/workspace/kibana/node_modules/jest-runner/build/runTest.js:380:16)
    at runTest (/dev/shm/workspace/kibana/node_modules/jest-runner/build/runTest.js:472:34)
    at Object.worker (/dev/shm/workspace/kibana/node_modules/jest-runner/build/testWorker.js:133:12)

Jest Tests.x-pack/plugins/alerts/server/alerts_client/tests.findAlertsWithInstancesTimeline() calls saved objects and event log client with default params

Link to Jenkins

Standard Out

Failed Tests Reporter:
  - Test has not failed recently on tracked branches


Stack Trace

TypeError: Cannot destructure property 'page' of '(intermediate value)' as it is undefined.
    at AlertsClient.find (/dev/shm/workspace/parallel/7/kibana/x-pack/plugins/alerts/server/alerts_client/alerts_client.ts:632:7)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at AlertsClient.findAlertsWithInstancesTimeline (/dev/shm/workspace/parallel/7/kibana/x-pack/plugins/alerts/server/alerts_client/alerts_client.ts:545:62)
    at Object.<anonymous> (/dev/shm/workspace/parallel/7/kibana/x-pack/plugins/alerts/server/alerts_client/tests/find_alerts_with_instances_timeline.test.ts:204:5)
    at _callCircusTest (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:212:5)
    at _runTest (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:149:3)
    at _runTestsForDescribeBlock (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:63:9)
    at _runTestsForDescribeBlock (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:57:9)
    at run (/dev/shm/workspace/kibana/node_modules/jest-circus/build/run.js:25:3)
    at runAndTransformResultsToJestFormat (/dev/shm/workspace/kibana/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:176:21)
    at jestAdapter (/dev/shm/workspace/kibana/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:109:19)
    at runTestInternal (/dev/shm/workspace/kibana/node_modules/jest-runner/build/runTest.js:380:16)
    at runTest (/dev/shm/workspace/kibana/node_modules/jest-runner/build/runTest.js:472:34)
    at Object.worker (/dev/shm/workspace/kibana/node_modules/jest-runner/build/testWorker.js:133:12)

and 5 more failures, only showing the first 3.

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@YulNaumenko
Copy link
Contributor Author

Closing this PR with the purpose to provide a different better approach to get alert instances summary statuses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Alerting needs_docs release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v7.13.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API to get all active instances from Observability consumers
5 participants