From 41826ed318706b879dfe9ee8938f8af0d6ff0425 Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Fri, 17 Feb 2023 20:00:21 +0100 Subject: [PATCH] [resotocore][fix] Fix pagerduty alert. (#1453) * [resotocore][fix] Fix pagerduty alert. * fix test --- resotocore/resotocore/core_config.py | 20 ++++++------ .../tests/resotocore/cli/command_test.py | 32 ++++++++++--------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/resotocore/resotocore/core_config.py b/resotocore/resotocore/core_config.py index 7f91278863..a515664333 100644 --- a/resotocore/resotocore/core_config.py +++ b/resotocore/resotocore/core_config.py @@ -351,8 +351,7 @@ def alias_templates() -> List[AliasTemplateConfig]: info="Create an alert in pagerduty from a search.", description=( "Perform a search and send the result to pagerduty.\n\n" - "No resource specific data will be sent to pagerduty. " - "The resources are aggregated by account and region with the count of matching resources.\n" + "A call to this command will only send the first 100 occurrences to the incident, the rest is dropped. " "The `summary` should explain why this alert is triggered, so that the user can take action.\n" "The `dedup_key` is used to identify an alert uniquely. " "You can fire the same alert multiple times by using the same dedup_key.\n\n" @@ -361,24 +360,27 @@ def alias_templates() -> List[AliasTemplateConfig]: ), template=( # aggregate the result by account and region - flat the results, since pagerduty only allows flat data - 'aggregate "account_{/ancestors.account.reported.id}__region_{/ancestors.region.reported.id}" as name :' - "sum(1) as count | chunk 100 | head 1 | " - "jq --no-rewrite 'map({(.group.name // \"none\"): .count}) | add | {details: .}'" + "head 100 | jq --no-rewrite {" + "cloud: .ancestors.cloud.reported.name, " + "account: .ancestors.account.reported.name, " + "region: .ancestors.region.reported.name, " + "name: .reported.name, " + "kind: .reported.kind} | chunk 100 " "| jq --no-rewrite '{payload: " '{summary: "{{summary}}", ' 'timestamp: "@utc@", ' 'source:"{{source}}", ' 'severity: "{{severity}}", ' 'component: "{{component}}", ' - "custom_details: .details, " + "custom_details: .}, " 'routing_key: "{{routing_key}}", ' 'dedup_key: "{{dedup_key}}", ' - 'images:[{src: "https://cdn.some.engineering/assets/resoto-logos/resoto-logo.svg", href: ' - '"https://resoto.com/", alt: "Resoto Home Page"}], ' + 'images:[{src: "https://cdn.some.engineering/assets/resoto-illustrations/small/resoto-alert.png", href:' + ' "https://resoto.com/", alt: "Resoto Home Page"}], ' "links:[], " 'event_action: "{{event_action}}", ' 'client: "Resoto Service", ' - 'client_url: "https://resoto.com"}}\'' + 'client_url: "https://resoto.com"}\'' # send the event to pagerduty ' | http {{webhook_url}} "Content-Type:application/json"' ), diff --git a/resotocore/tests/resotocore/cli/command_test.py b/resotocore/tests/resotocore/cli/command_test.py index 5e54fdcfcf..d13ba2635c 100644 --- a/resotocore/tests/resotocore/cli/command_test.py +++ b/resotocore/tests/resotocore/cli/command_test.py @@ -883,7 +883,7 @@ async def test_jira_alias(cli: CLI, echo_http_server: Tuple[int, List[Tuple[Requ async def test_pagerduty_alias(cli: CLI, echo_http_server: Tuple[int, List[Tuple[Request, Json]]]) -> None: port, requests = echo_http_server result = await cli.execute_cli_command( - f'search is(bla) | pagerduty --webhook-url "http://localhost:{port}/success" --summary test --routing-key 123 --dedup-key 234', + f'search is(bla) | head 1 | pagerduty --webhook-url "http://localhost:{port}/success" --summary test --routing-key 123 --dedup-key 234', stream.list, ) assert result == [["1 requests with status 200 sent."]] @@ -899,21 +899,23 @@ async def test_pagerduty_alias(cli: CLI, echo_http_server: Tuple[int, List[Tuple "source": "Resoto", "severity": "warning", "component": "Resoto", - "custom_details": {"account___region_": 100}, - "routing_key": "123", - "dedup_key": "234", - "images": [ - { - "src": "https://cdn.some.engineering/assets/resoto-logos/resoto-logo.svg", - "href": "https://resoto.com/", - "alt": "Resoto Home Page", - } + "custom_details": [ + {"cloud": None, "account": None, "region": None, "name": "yes or no", "kind": "bla"}, ], - "links": [], - "event_action": "trigger", - "client": "Resoto Service", - "client_url": "https://resoto.com", - } + }, + "routing_key": "123", + "dedup_key": "234", + "images": [ + { + "src": "https://cdn.some.engineering/assets/resoto-illustrations/small/resoto-alert.png", + "href": "https://resoto.com/", + "alt": "Resoto Home Page", + } + ], + "links": [], + "event_action": "trigger", + "client": "Resoto Service", + "client_url": "https://resoto.com", }