Skip to content

Commit

Permalink
Fingerprint randomization
Browse files Browse the repository at this point in the history
  • Loading branch information
Matvey-Kuk committed Nov 18, 2024
1 parent 5bc2ed9 commit 294a792
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
29 changes: 25 additions & 4 deletions keep/api/core/demo_mode_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time
import datetime
from datetime import timezone
from requests.models import PreparedRequest

from keep.api.core.db import get_session_sync
from keep.api.core.dependencies import SINGLE_TENANT_UUID
Expand Down Expand Up @@ -316,7 +317,17 @@ async def simulate_alerts(
):
GENERATE_DEDUPLICATIONS = True

providers = ["prometheus", "grafana"]
providers = [
"prometheus",
"grafana",
"cloudwatch",
"datadog",
]

providers_to_randomize_fingerprint_for = [
"cloudwatch",
"datadog",
]

provider_classes = {
provider: ProvidersFactory.get_provider_class(provider)
Expand All @@ -333,8 +344,6 @@ async def simulate_alerts(
get_or_create_topology(keep_api_key, keep_api_url)

while True:
await asyncio.sleep(sleep_interval)

remove_old_incidents(keep_api_key, keep_api_url)

# choose provider
Expand All @@ -344,6 +353,12 @@ async def simulate_alerts(
provider = provider_classes[provider_type]
alert = provider.simulate_alert()

send_alert_url_params = {}

if provider_type in providers_to_randomize_fingerprint_for:
send_alert_url_params['fingerprint'] = \
''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=10))

# Determine number of times to send the same alert
num_iterations = 1
if GENERATE_DEDUPLICATIONS:
Expand All @@ -353,8 +368,11 @@ async def simulate_alerts(
logger.info("Sending alert: {}".format(alert))
try:
env = random.choice(["production", "staging", "development"])
send_alert_url_params['provider_id'] = f"{provider_type}-{env}"
prepared_request = PreparedRequest()
prepared_request.prepare_url(send_alert_url, send_alert_url_params)
response = requests.post(
send_alert_url + f"?provider_id={provider_type}-{env}",
prepared_request.url,
headers={"x-api-key": keep_api_key},
json=alert,
)
Expand All @@ -369,6 +387,9 @@ async def simulate_alerts(
else:
logger.info("Alert sent successfully")

await asyncio.sleep(sleep_interval)


def launch_demo_mode():
"""
Running async demo in the backgound.
Expand Down
2 changes: 1 addition & 1 deletion keep/providers/cloudwatch_provider/alerts_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
},
"parameters": {
"Message.AlarmName": ["HighCPUUsage-1", "HighCPUUsage-2", "HighCPUUsage-3"],
"Message.AlarmName": ["HighCPUUsage", "HighCPUUsageOnAPod", "PodRecycled"],
},
},
}
8 changes: 4 additions & 4 deletions keep/providers/datadog_provider/alerts_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
},
"parameters": {
"tags": [
"environment:production,team:backend,monitor",
"environment:staging,team:backend,monitor",
"environment:production,team:backend,monitor,service:api",
"environment:staging,team:backend,monitor,service:api",
],
"priority": ["P2", "P3", "P4"],
},
Expand All @@ -29,8 +29,8 @@
},
"parameters": {
"tags": [
"environment:production,team:analytics,monitor",
"environment:staging,team:database,monitor",
"environment:production,team:analytics,monitor,service:api",
"environment:staging,team:database,monitor,service:api",
],
"priority": ["P1", "P3", "P4"],
},
Expand Down

0 comments on commit 294a792

Please sign in to comment.