-
Notifications
You must be signed in to change notification settings - Fork 290
feat: Enable per-alert Incident Severity, metadata improvements #1970
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
Open
igorhrcek
wants to merge
1
commit into
robusta-dev:master
Choose a base branch
from
igorhrcek:feat/no-ref/incident-io-improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,18 @@ | ||
| """ | ||
| Incident.io sink for Robusta. | ||
| """ | ||
|
|
||
| import logging | ||
| from typing import Optional, Dict, List, Any | ||
| from robusta.core.sinks.incidentio.incidentio_client import IncidentIoClient | ||
| from robusta.core.sinks.incidentio.incidentio_sink_params import IncidentioSinkParams, IncidentioSinkConfigWrapper | ||
| from robusta.core.sinks.incidentio.incidentio_api import AlertEventsApi | ||
| from robusta.core.sinks.sink_base import SinkBase | ||
|
|
||
| from robusta.core.reporting.base import BaseBlock, Finding, FindingSeverity, Enrichment, Link, LinkType | ||
| from robusta.core.reporting.base import BaseBlock, Finding | ||
| from robusta.core.reporting.blocks import ( | ||
| HeaderBlock, | ||
| JsonBlock, | ||
| LinksBlock, | ||
| ListBlock, | ||
| MarkdownBlock, | ||
| TableBlock, | ||
|
|
@@ -18,14 +21,17 @@ | |
|
|
||
|
|
||
| class IncidentioSink(SinkBase): | ||
| """ | ||
| Incident.io sink for Robusta. | ||
| """ | ||
|
|
||
| params: IncidentioSinkParams | ||
|
|
||
| def __init__(self, sink_config: IncidentioSinkConfigWrapper, registry): | ||
| super().__init__(sink_config.incidentio_sink, registry) | ||
| self.source_config_id = sink_config.incidentio_sink.source_config_id | ||
| self.client = IncidentIoClient( | ||
| base_url=sink_config.incidentio_sink.base_url, | ||
| token=sink_config.incidentio_sink.token | ||
| base_url=sink_config.incidentio_sink.base_url, token=sink_config.incidentio_sink.token | ||
| ) | ||
|
|
||
| @staticmethod | ||
|
|
@@ -34,7 +40,6 @@ def __to_incidentio_status_type(title: str) -> str: | |
| if title.startswith("[RESOLVED]"): | ||
| return "resolved" | ||
| return "firing" | ||
|
|
||
|
|
||
| def __send_event_to_incidentio(self, finding: Finding, platform_enabled: bool) -> dict: | ||
| metadata: Dict[str, Any] = {} | ||
|
|
@@ -58,36 +63,49 @@ def __send_event_to_incidentio(self, finding: Finding, platform_enabled: bool) - | |
| metadata["source"] = finding.source.name | ||
| metadata["fingerprint_id"] = finding.fingerprint | ||
|
|
||
| # Convert blocks to metadata | ||
| # Convert blocks to metadata as structured array | ||
| additional_info_list = [] | ||
| for enrichment in finding.enrichments: | ||
| for block in enrichment.blocks: | ||
| text = self.__to_unformatted_text(block) | ||
| if text: | ||
| metadata["additional_info"] = metadata.get("additional_info", "") + text + "\n" | ||
| block_type = self.__get_block_type_name(block) | ||
| additional_info_list.append({"type": block_type, "content": text}) | ||
|
|
||
| if additional_info_list: | ||
| metadata["additional_info"] = additional_info_list | ||
|
|
||
| return { | ||
| payload = { | ||
| "deduplication_key": finding.fingerprint, | ||
| "title": finding.title, | ||
| "description": finding.description or "No description provided.", | ||
| "status": self.__to_incidentio_status_type(finding.title), | ||
| "metadata": metadata, | ||
| "source_url": finding.get_investigate_uri(self.account_id, self.cluster_name), | ||
| "links": links, | ||
| } | ||
|
|
||
| if platform_enabled: | ||
| payload["source_url"] = finding.get_investigate_uri(self.account_id, self.cluster_name) | ||
|
|
||
| return payload | ||
|
|
||
| def write_finding(self, finding: Finding, platform_enabled: bool) -> None: | ||
| payload = self.__send_event_to_incidentio(finding, platform_enabled) | ||
|
|
||
| response = self.client.request( | ||
| "POST", | ||
| AlertEventsApi(self.client.base_url, self.source_config_id).build_url(), | ||
| payload | ||
| "POST", AlertEventsApi(self.client.base_url, self.source_config_id).build_url(), payload | ||
| ) | ||
|
|
||
| if not response.ok: | ||
| logging.error( | ||
| f"Error sending alert to Incident.io: {response.status_code}, {response.text}" | ||
| ) | ||
| logging.error("Error sending alert to Incident.io: %s, %s", {response.status_code}, {response.text}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like code rabbit is right, and this is indeed a bug |
||
|
|
||
| @staticmethod | ||
| def __get_block_type_name(block: BaseBlock) -> str: | ||
| """Extract the block type name, removing 'Block' suffix if present.""" | ||
| class_name = block.__class__.__name__ | ||
| if class_name.endswith("Block"): | ||
| return class_name[:-5].lower() # Remove 'Block' suffix and convert to lowercase | ||
| return class_name.lower() | ||
|
|
||
| @staticmethod | ||
| def __to_unformatted_text(block: BaseBlock) -> Optional[str]: | ||
|
|
@@ -103,4 +121,40 @@ def __to_unformatted_text(block: BaseBlock) -> Optional[str]: | |
| return block.json_str | ||
| elif isinstance(block, KubernetesDiffBlock): | ||
| return "\n".join(diff.formatted_path for diff in block.diffs) | ||
| else: | ||
| # Handle additional block types dynamically | ||
| block_class = block.__class__.__name__ | ||
|
|
||
| # FileBlock: has file_content attribute | ||
| if hasattr(block, "file_content") and block.file_content: | ||
| return block.file_content | ||
|
|
||
| # EmptyFileBlock: just return a placeholder | ||
| elif block_class == "EmptyFileBlock": | ||
| return "[Empty File]" | ||
|
|
||
| # PrometheusBlock: has query results | ||
| elif hasattr(block, "query") and hasattr(block, "series_data"): | ||
| return f"Query: {block.query}\nResults: {len(block.series_data)} series" | ||
|
|
||
| # ScanReportBlock: has scan results | ||
| elif hasattr(block, "title") and hasattr(block, "score"): | ||
| return f"Scan: {block.title}, Score: {block.score}" | ||
|
|
||
| # CallbackBlock: has callback info | ||
| elif hasattr(block, "action_name"): | ||
| return f"Action: {block.action_name}" | ||
|
|
||
| # DividerBlock: just a visual separator | ||
| elif block_class == "DividerBlock": | ||
| return "[Divider]" | ||
|
|
||
| # Generic fallback: try to get text content from common attributes | ||
| elif hasattr(block, "text"): | ||
| return block.text | ||
| elif hasattr(block, "content"): | ||
| return str(block.content) | ||
| elif hasattr(block, "message"): | ||
| return block.message | ||
|
|
||
| return None | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the logging statement - critical bug.
The logging arguments are wrapped in curly braces, which creates set literals in Python instead of passing the actual values. This will log useless output like
{<HTTPStatus.NOT_FOUND: 404>}instead of the actual status code and response text.Apply this diff to fix the logging:
📝 Committable suggestion
🤖 Prompt for AI Agents