Skip to content

Commit df98222

Browse files
authoredFeb 9, 2025··
chore(coralogix): better handling of fields (keephq#3350)
1 parent 610c331 commit df98222

File tree

1 file changed

+23
-45
lines changed

1 file changed

+23
-45
lines changed
 

‎keep/providers/coralogix_provider/coralogix_provider.py

+23-45
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Coralogix is a modern observability platform delivers comprehensive visibility into all your logs, metrics, traces and security events with end-to-end monitoring.
33
"""
44

5+
import json
6+
57
from keep.api.models.alert import AlertDto, AlertSeverity, AlertStatus
68
from keep.contextmanager.contextmanager import ContextManager
79
from keep.providers.base.base_provider import BaseProvider
@@ -59,67 +61,43 @@ def validate_config(self):
5961
# no config
6062
pass
6163

62-
def get_value_by_key(fields: dict, key: str):
63-
for item in fields:
64-
if item["key"] == key:
65-
return item["value"]
66-
return None
67-
6864
@staticmethod
6965
def _format_alert(
7066
event: dict, provider_instance: "BaseProvider" = None
7167
) -> AlertDto:
68+
fields_list = event["fields"] if "fields" in event else []
69+
fields = {item["key"]: item["value"] for item in fields_list}
70+
71+
labels = fields.get("text", fields.get("labels"))
72+
if isinstance(labels, str):
73+
try:
74+
labels = json.loads(labels)
75+
except Exception:
76+
# Do nothing, keep labels as str
77+
pass
78+
7279
alert = AlertDto(
73-
id=(
74-
CoralogixProvider.get_value_by_key(
75-
event["fields"], "alertUniqueIdentifier"
76-
)
77-
if "fields" in event
78-
else None
79-
),
80+
id=fields.get("alertUniqueIdentifier"),
8081
alert_id=event["alert_id"] if "alert_id" in event else None,
8182
name=event["name"] if "name" in event else None,
8283
description=event["description"] if "description" in event else None,
8384
status=CoralogixProvider.STATUS_MAP.get(event["alert_action"]),
8485
severity=CoralogixProvider.SEVERITIES_MAP.get(
85-
CoralogixProvider.get_value_by_key(event["fields"], "severityLowercase")
86-
),
87-
lastReceived=(
88-
CoralogixProvider.get_value_by_key(event["fields"], "timestampISO")
89-
if "fields" in event
90-
else None
91-
),
92-
alertUniqueIdentifier=(
93-
CoralogixProvider.get_value_by_key(
94-
event["fields"], "alertUniqueIdentifier"
95-
)
96-
if "fields" in event
97-
else None
86+
fields.get("severityLowercase", "info")
9887
),
88+
lastReceived=fields.get("timestampISO"),
89+
alertUniqueIdentifier=fields.get("alertUniqueIdentifier"),
9990
uuid=event["uuid"] if "uuid" in event else None,
10091
threshold=event["threshold"] if "threshold" in event else None,
10192
timewindow=event["timewindow"] if "timewindow" in event else None,
102-
group_by_labels=(
103-
event["group_by_labels"] if "group_by_labels" in event else None
104-
),
93+
group_by_labels=fields.get("group_by_labels"),
10594
alert_url=event["alert_url"] if "alert_url" in event else None,
10695
log_url=event["log_url"] if "log_url" in event else None,
107-
team=(
108-
CoralogixProvider.get_value_by_key(event["fields"], "team")
109-
if "fields" in event
110-
else None
111-
),
112-
priority=(
113-
CoralogixProvider.get_value_by_key(event["fields"], "priority")
114-
if "fields" in event
115-
else None
116-
),
117-
computer=(
118-
CoralogixProvider.get_value_by_key(event["fields"], "computer")
119-
if "fields" in event
120-
else None
121-
),
122-
fields=event["fields"] if "fields" in event else None,
96+
team=fields.get("team"),
97+
priority=fields.get("priority"),
98+
computer=fields.get("computer"),
99+
fields=fields,
100+
labels=labels,
123101
source=["coralogix"],
124102
)
125103

0 commit comments

Comments
 (0)
Please sign in to comment.