Skip to content

Commit acad41c

Browse files
shahargltalboren
andauthored
fix: gcp monitoring (keephq#1340)
Signed-off-by: Tal <[email protected]> Co-authored-by: Tal <[email protected]>
1 parent fefd2ae commit acad41c

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
ALERTS = {
2+
"test-incident": {
3+
"payload": {
4+
"version": "test",
5+
"incident": {
6+
"incident_id": "12345",
7+
"scoping_project_id": "12345",
8+
"scoping_project_number": 12345,
9+
"url": "http://www.example.com",
10+
"started_at": 0,
11+
"ended_at": 0,
12+
"state": "OPEN",
13+
"summary": "Test Incident",
14+
"apigee_url": "http://www.example.com",
15+
"observed_value": "1.0",
16+
"resource": {
17+
"type": "example_resource",
18+
"labels": {"example": "label"},
19+
},
20+
"resource_type_display_name": "Example Resource Type",
21+
"resource_id": "12345",
22+
"resource_display_name": "Example Resource",
23+
"resource_name": "projects/12345/example_resources/12345",
24+
"metric": {
25+
"type": "test.googleapis.com/metric",
26+
"displayName": "Test Metric",
27+
"labels": {"example": "label"},
28+
},
29+
"metadata": {
30+
"system_labels": {"example": "label"},
31+
"user_labels": {"example": "label"},
32+
},
33+
"policy_name": "projects/12345/alertPolicies/12345",
34+
"policy_user_labels": {"example": "label"},
35+
"documentation": "Test documentation",
36+
"condition": {
37+
"name": "projects/12345/alertPolicies/12345/conditions/12345",
38+
"displayName": "Example condition",
39+
"conditionThreshold": {
40+
"filter": 'metric.type="test.googleapis.com/metric" resource.type="example_resource"',
41+
"comparison": "COMPARISON_GT",
42+
"thresholdValue": 0.5,
43+
"duration": "0s",
44+
"trigger": {"count": 1},
45+
},
46+
},
47+
"condition_name": "Example condition",
48+
"threshold_value": "0.5",
49+
},
50+
},
51+
"parameters": {},
52+
},
53+
}

keep/providers/gcpmonitoring_provider/gcpmonitoring_provider.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ def _format_alert(
7373
incident.pop("state", "").upper(), AlertStatus.FIRING
7474
)
7575
url = incident.pop("url", "")
76-
name = incident.pop("documentation", {}).get("subject")
77-
incident_id = incident.pop("incident_id", "")
76+
documentation = incident.pop("documentation", {})
77+
if isinstance(documentation, dict):
78+
name = documentation.get("subject")
79+
else:
80+
name = "Test notification"
81+
incident_id = incident.get("incident_id", "")
7882
# Get the severity
7983
if "severity" in incident:
8084
severity = GcpmonitoringProvider.SEVERITIES_MAP.get(
@@ -89,18 +93,23 @@ def _format_alert(
8993
# Parse and format the timestamp
9094
event_time = incident.get("started_at")
9195
if event_time:
92-
event_time = datetime.datetime.fromtimestamp(event_time)
93-
else:
94-
event_time = datetime.datetime.utcnow()
96+
event_time = datetime.datetime.fromtimestamp(
97+
event_time, tz=datetime.timezone.utc
98+
)
9599
# replace timezone to utc
96-
event_time = event_time.replace(tzinfo=datetime.timezone.utc)
97100

101+
else:
102+
event_time = datetime.datetime.now(tz=datetime.timezone.utc)
103+
104+
event_time = event_time.isoformat(timespec="milliseconds").replace(
105+
"+00:00", "Z"
106+
)
98107
# Construct the alert object
99108
alert = AlertDto(
100109
id=incident_id,
101110
name=name,
102111
status=status,
103-
lastReceived=str(event_time),
112+
lastReceived=event_time,
104113
source=["gcpmonitoring"],
105114
description=description,
106115
severity=severity,

0 commit comments

Comments
 (0)