Skip to content

Commit

Permalink
feat: add mocking alert for cloudwatch provider (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
ztzxt authored Apr 16, 2024
1 parent fd4fff4 commit eca2229
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
17 changes: 17 additions & 0 deletions keep/providers/cloudwatch_provider/alerts_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ALERTS = {
"high_cpu_usage": {
"payload": {
"Message": {
"AlarmName": "HighCPUUsage",
"MetricName": "CPUUtilization",
"Namespace": "AWS/EC2",
"Threshold": 90,
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Priority": "P3"
}
},
"parameters": {
"Message.AlarmName": ["HighCPUUsage-1", "HighCPUUsage-2", "HighCPUUsage-3"],
},
},
}
34 changes: 34 additions & 0 deletions keep/providers/cloudwatch_provider/cloudwatch_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,40 @@ def _format_alert(
**alert,
)

@classmethod
def simulate_alert(cls) -> dict:
# Choose a random alert type
import hashlib
import random

from keep.providers.cloudwatch_provider.alerts_mock import ALERTS

alert_type = random.choice(list(ALERTS.keys()))
alert_data = ALERTS[alert_type]

# Start with the base payload
simulated_alert = alert_data["payload"].copy()
# Apply variability based on parameters
for param, choices in alert_data.get("parameters", {}).items():
# Split param on '.' for nested parameters (if any)
param_parts = param.split(".")
target = simulated_alert
for part in param_parts[:-1]:
target = target.setdefault(part, {})

# Choose a random value for the parameter
target[param_parts[-1]] = random.choice(choices)

# Set StateChangeTime to current time
simulated_alert["Message"]["StateChangeTime"] = datetime.datetime.now().isoformat()

# Provider expects all keys as string
for key in simulated_alert:
value = simulated_alert[key]
simulated_alert[key] = json.dumps(value)

return simulated_alert


if __name__ == "__main__":
config = ProviderConfig(
Expand Down
2 changes: 1 addition & 1 deletion keep/providers/prometheus_provider/alerts_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"labels": {
"instance": "example1",
"job": "example2",
"workfload": "somecoolworkload",
"workload": "somecoolworkload",
"severity": "critical",
},
},
Expand Down

0 comments on commit eca2229

Please sign in to comment.