Skip to content

Commit

Permalink
fix: gcp monitoring (keephq#1340)
Browse files Browse the repository at this point in the history
Signed-off-by: Tal <[email protected]>
Co-authored-by: Tal <[email protected]>
  • Loading branch information
shahargl and talboren authored Jul 9, 2024
1 parent fefd2ae commit acad41c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
53 changes: 53 additions & 0 deletions keep/providers/gcpmonitoring_provider/alerts_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
ALERTS = {
"test-incident": {
"payload": {
"version": "test",
"incident": {
"incident_id": "12345",
"scoping_project_id": "12345",
"scoping_project_number": 12345,
"url": "http://www.example.com",
"started_at": 0,
"ended_at": 0,
"state": "OPEN",
"summary": "Test Incident",
"apigee_url": "http://www.example.com",
"observed_value": "1.0",
"resource": {
"type": "example_resource",
"labels": {"example": "label"},
},
"resource_type_display_name": "Example Resource Type",
"resource_id": "12345",
"resource_display_name": "Example Resource",
"resource_name": "projects/12345/example_resources/12345",
"metric": {
"type": "test.googleapis.com/metric",
"displayName": "Test Metric",
"labels": {"example": "label"},
},
"metadata": {
"system_labels": {"example": "label"},
"user_labels": {"example": "label"},
},
"policy_name": "projects/12345/alertPolicies/12345",
"policy_user_labels": {"example": "label"},
"documentation": "Test documentation",
"condition": {
"name": "projects/12345/alertPolicies/12345/conditions/12345",
"displayName": "Example condition",
"conditionThreshold": {
"filter": 'metric.type="test.googleapis.com/metric" resource.type="example_resource"',
"comparison": "COMPARISON_GT",
"thresholdValue": 0.5,
"duration": "0s",
"trigger": {"count": 1},
},
},
"condition_name": "Example condition",
"threshold_value": "0.5",
},
},
"parameters": {},
},
}
23 changes: 16 additions & 7 deletions keep/providers/gcpmonitoring_provider/gcpmonitoring_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ def _format_alert(
incident.pop("state", "").upper(), AlertStatus.FIRING
)
url = incident.pop("url", "")
name = incident.pop("documentation", {}).get("subject")
incident_id = incident.pop("incident_id", "")
documentation = incident.pop("documentation", {})
if isinstance(documentation, dict):
name = documentation.get("subject")
else:
name = "Test notification"
incident_id = incident.get("incident_id", "")
# Get the severity
if "severity" in incident:
severity = GcpmonitoringProvider.SEVERITIES_MAP.get(
Expand All @@ -89,18 +93,23 @@ def _format_alert(
# Parse and format the timestamp
event_time = incident.get("started_at")
if event_time:
event_time = datetime.datetime.fromtimestamp(event_time)
else:
event_time = datetime.datetime.utcnow()
event_time = datetime.datetime.fromtimestamp(
event_time, tz=datetime.timezone.utc
)
# replace timezone to utc
event_time = event_time.replace(tzinfo=datetime.timezone.utc)

else:
event_time = datetime.datetime.now(tz=datetime.timezone.utc)

event_time = event_time.isoformat(timespec="milliseconds").replace(
"+00:00", "Z"
)
# Construct the alert object
alert = AlertDto(
id=incident_id,
name=name,
status=status,
lastReceived=str(event_time),
lastReceived=event_time,
source=["gcpmonitoring"],
description=description,
severity=severity,
Expand Down

0 comments on commit acad41c

Please sign in to comment.