-
Notifications
You must be signed in to change notification settings - Fork 7
Add zenduty integration #67
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2220f03
Add zenduty integration
ayazabbas 1f143f3
comment
ayazabbas 37c5118
comment
ayazabbas 9558154
bump minor version
ayazabbas 57acddf
Merge branch 'main' into AA_zenduty-events
ayazabbas ce3850f
address comments
ayazabbas 3c78d11
Merge branch 'main' into AA_zenduty-events
ayazabbas f6737be
mention new event type in readme
ayazabbas f0c3b12
fix exponent operator
ayazabbas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import asyncio | ||
| import hashlib | ||
| import os | ||
|
|
||
| import aiohttp | ||
| from loguru import logger | ||
|
|
||
| headers = {"Content-Type": "application/json"} | ||
|
|
||
|
|
||
| async def send_zenduty_alert(alert_identifier, message, resolved=False, summary=""): | ||
| url = f"https://www.zenduty.com/api/events/{os.environ['ZENDUTY_INTEGRATION_KEY']}/" | ||
| # Use a hash of the alert_identifier as a unique id for the alert. | ||
| # Take the first 32 characters due to length limit of the api. | ||
| entity_id = hashlib.sha256(alert_identifier.encode("utf-8")).hexdigest()[:32] | ||
|
|
||
| alert_type = "resolved" if resolved else "critical" | ||
|
|
||
| data = { | ||
| "alert_type": alert_type, | ||
| "message": message, | ||
| "summary": summary, | ||
| "entity_id": entity_id, | ||
| } | ||
|
|
||
| async with aiohttp.ClientSession() as session: | ||
| max_retries = 30 | ||
| retries = 0 | ||
| while retries < max_retries: | ||
| async with session.post(url, json=data, headers=headers) as response: | ||
| if 200 <= response.status < 300: | ||
| return response # Success case, return response | ||
| elif response.status == 429: | ||
| retries += 1 | ||
| if retries < max_retries: | ||
| logger.error( | ||
| f"Received 429 Too Many Requests for {alert_identifier}. Retrying in 1 second..." | ||
| ) | ||
| await asyncio.sleep( | ||
| min(30, 2**retries) | ||
| ) # Backoff before retrying, wait upto 30s | ||
| else: | ||
| logger.error( | ||
| f"Failed to send Zenduty event message for {alert_identifier} after {max_retries} retries." | ||
| ) | ||
| return response # Return response after max retries | ||
| else: | ||
| response_text = await response.text() | ||
| logger.error( | ||
| f"{response.status} Failed to send Zenduty event message for {alert_identifier}: {response_text}" | ||
| ) | ||
| return response # Non-retryable failure |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.