Skip to content

Commit

Permalink
[MAIN-2448] - Slack labels to ai (#1640)
Browse files Browse the repository at this point in the history
* added slack labels

* updated holmes convo string

* rebase fix

* fixing slack labels

* log fix

* checking label size before all the params
  • Loading branch information
Avi-Robusta authored Dec 1, 2024
1 parent f8c4008 commit edb93f6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/robusta/core/model/base_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ class HolmesIssueChatParamsContext(BaseModel):
:var investigation_result: HolmesInvestigationResult object that contains investigation saved to Evidence table by frontend for the issue.
:var issue_type: aggregation key of the issue
:var robusta_issue_id: id of the issue
:var labels: labels from the issue
"""

investigation_result: HolmesInvestigationResult
issue_type: str
robusta_issue_id: Optional[str] = None
labels: Optional[Dict[str, str]] = None


# will be deprecated later alongside with holmes_conversation action
Expand Down
10 changes: 9 additions & 1 deletion src/robusta/core/playbooks/internal/ai_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def ask_holmes(event: ExecutionBaseEvent, params: AIInvestigateParams):
subject = params.resource.dict() if params.resource else {}

try:
params.ask = add_labels_to_ask(params)
holmes_req = HolmesRequest(
source=params.context.get("source", "unknown source") if params.context else "unknown source",
title=investigation__title,
Expand Down Expand Up @@ -162,7 +163,14 @@ def holmes_workload_health(event: ExecutionBaseEvent, params: HolmesWorkloadHeal


def build_conversation_title(params: HolmesConversationParams) -> str:
return f"{params.resource}, {params.ask} for issue {params.context.robusta_issue_id}"
return f"{params.resource}, {params.ask} for issue '{params.context.robusta_issue_id}'"


def add_labels_to_ask(params: HolmesConversationParams) -> str:
label_string = f"the alert has the following labels: {params.context.get('labels')}" if params.context.get("labels") else ""
ask = f"{params.ask}, {label_string}" if label_string else params.ask
logging.debug(f"holmes ask query: {ask}")
return ask


# old version of holmes conversation API
Expand Down
27 changes: 27 additions & 0 deletions src/robusta/integrations/slack/sender.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import logging
import ssl
import tempfile
Expand Down Expand Up @@ -308,6 +309,31 @@ def __send_blocks_to_slack(
f"error sending message to slack\ne={e}\ntext={message}\nchannel={channel}\nblocks={*output_blocks,}\nattachment_blocks={*attachment_blocks,}"
)


def __limit_labels_size(self, labels: dict, max_size: int = 1000) -> dict:
# slack can only send 2k tokens in a callback so the labels are limited in size

low_priority_labels = ["job", "prometheus", "severity", "service"]
current_length = len(str(labels))
if current_length <= max_size:
return labels

limited_labels = copy.deepcopy(labels)

# first remove the low priority labels if needed
for key in low_priority_labels:
if current_length <= max_size:
break
if key in limited_labels:
del limited_labels[key]
current_length = len(str(limited_labels))

while current_length > max_size and limited_labels:
limited_labels.pop(next(iter(limited_labels)))
current_length = len(str(limited_labels))

return limited_labels

def __create_holmes_callback(self, finding: Finding) -> CallbackBlock:
resource = ResourceInfo(
name=finding.subject.name if finding.subject.name else "",
Expand All @@ -321,6 +347,7 @@ def __create_holmes_callback(self, finding: Finding) -> CallbackBlock:
"robusta_issue_id": str(finding.id),
"issue_type": finding.aggregation_key,
"source": finding.source.name,
"labels": self.__limit_labels_size(labels=finding.subject.labels)
}

return CallbackBlock(
Expand Down

0 comments on commit edb93f6

Please # to comment.