SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
test(integration): alert firing verification fixture (#10131)
Abhishek Kumar Singhc4a6c7e27746
diff --git a/tests/integration/fixtures/alertutils.py b/tests/integration/fixtures/alertutils.pynew file mode 100644index 0000000000..747afff4af--- /dev/null+++ b/tests/integration/fixtures/alertutils.py@@ -0,0 +1,135 @@+import base64+import json+import time+from datetime import datetime, timedelta+from http import HTTPStatus+from typing import List++import requests++from fixtures import types+from fixtures.logger import setup_logger++logger = setup_logger(__name__)+++def collect_webhook_firing_alerts(+ webhook_test_container: types.TestContainerDocker, notification_channel_name: str+) -> List[types.FiringAlert]:+ # Prepare the endpoint path for the channel name, for alerts tests we have+ # used different paths for receiving alerts from each channel so that+ # multiple rules can be tested in isolation.+ rule_webhook_endpoint = f"/alert/{notification_channel_name}"++ url = webhook_test_container.host_configs["8080"].get("__admin/requests/find")+ req = {+ "method": "POST",+ "url": rule_webhook_endpoint,+ }+ res = requests.post(url, json=req, timeout=5)+ assert res.status_code == HTTPStatus.OK, (+ f"Failed to collect firing alerts for notification channel {notification_channel_name}, "+ f"status code: {res.status_code}, response: {res.text}"+ )+ response = res.json()+ alerts = []+ for req in response["requests"]:+ alert_body_base64 = req["bodyAsBase64"]+ alert_body = base64.b64decode(alert_body_base64).decode("utf-8")+ # remove newlines from the alert body+ alert_body = alert_body.replace("\n", "")+ alert_dict = json.loads(alert_body) # parse the alert body into a dictionary+ for a in alert_dict["alerts"]:+ alerts.append(types.FiringAlert(labels=a["labels"]))+ return alerts+++def _verify_alerts_labels(+ firing_alerts: list[dict[str, str]], expected_alerts: list[dict[str, str]]+) -> tuple[int, list[dict[str, str]]]:+ """+ Checks how many of the expected alerts have been fired.+ Returns the count of expected alerts that have been fired.+ """+ fired_count = 0+ missing_alerts = []++ for alert in expected_alerts:+ is_alert_fired = False++ for fired_alert in firing_alerts:+ # Check if current expected alert is present in the fired alerts+ if all(+ key in fired_alert and fired_alert[key] == value+ for key, value in alert.items()+ ):+ is_alert_fired = True+ break++ if is_alert_fired:+ fired_count += 1+ else:+ missing_alerts.append(alert)++ return (fired_count, missing_alerts)+++def verify_webhook_alert_expectation(+ test_alert_container: types.TestContainerDocker,+ notification_channel_name: str,+ alert_expectations: types.AlertExpectation,+) -> bool:++ # time to wait till the expected alerts are fired+ time_to_wait = datetime.now() + timedelta(+ seconds=alert_expectations.wait_time_seconds+ )+ expected_alerts_labels = [+ alert.labels for alert in alert_expectations.expected_alerts+ ]++ while datetime.now() < time_to_wait:+ firing_alerts = collect_webhook_firing_alerts(+ test_alert_container, notification_channel_name+ )+ firing_alert_labels = [alert.labels for alert in firing_alerts]++ if alert_expectations.should_alert:+ # verify the number of alerts fired, currently we're only verifying the labels of the alerts+ # but there could be verification of annotations and other fields in the FiringAlert+ (verified_count, missing_alerts) = _verify_alerts_labels(+ firing_alert_labels, expected_alerts_labels+ )++ if verified_count == len(alert_expectations.expected_alerts):+ logger.info(+ "Got expected number of alerts: %s", {"count": verified_count}+ )+ return True+ else:+ # No alert is supposed to be fired if should_alert is False+ if len(firing_alerts) > 0:+ break++ # wait for some time before checking again+ time.sleep(10)++ # We've waited but we didn't get the expected number of alerts++ # check if alert was expected to be fired or not, if not then we're good+ if not alert_expectations.should_alert:+ assert len(firing_alerts) == 0, (+ "Expected no alerts to be fired, ",+ f"got {len(firing_alerts)} alerts, " f"firing alerts: {firing_alerts}",+ )+ logger.info("No alerts fired, as expected")+ return True++ # we've waited but we didn't get the expected number of alerts, raise an exception+ assert verified_count == len(alert_expectations.expected_alerts), (+ f"Expected {len(alert_expectations.expected_alerts)} alerts to be fired but got {verified_count} alerts, ",+ f"missing alerts: {missing_alerts}, ",+ f"firing alerts: {firing_alerts}",+ )++ return True # should not reach herediff --git a/tests/integration/fixtures/types.py b/tests/integration/fixtures/types.pyindex 6ea711146a..d7d399f836 100644--- a/tests/integration/fixtures/types.py+++ b/tests/integration/fixtures/types.py@@ -1,5 +1,5 @@from dataclasses import dataclass-from typing import Dict, Literal+from typing import Dict, List, Literalfrom urllib.parse import urljoinimport clickhouse_connect@@ -173,3 +173,21 @@ class AlertData:type: Literal["metrics", "logs", "traces"]# path to the data file in testdata directorydata_path: str+++@dataclass(frozen=True)+class FiringAlert:+ # labels of the alert that is firing+ labels: dict[str, str]+ # annotations and other fields can be added later as per need+++@dataclass(frozen=True)+class AlertExpectation:+ # whether we expect any alerts to be fired+ should_alert: bool+ # alerts that we expect to be fired+ expected_alerts: List[FiringAlert]+ # seconds to wait for the alerts to be fired, if no+ # alerts are fired in the expected time, the test will fail+ wait_time_seconds: int