Skip to content

Commit

Permalink
Add Cloud Foundry event metadata to DataDog event text as serialized …
Browse files Browse the repository at this point in the history
…JSON (DataDog#7407)
  • Loading branch information
Slavek Kabrda authored Aug 20, 2020
1 parent 73bd64b commit 0f7b8b2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import copy
import json
import time
from typing import Any, Dict, Generator, Tuple

Expand Down Expand Up @@ -366,6 +367,7 @@ def parse_event(self, cf_event):
event_entity["actee"],
event_entity.get("space_guid"), # Some events might not have a space associated
event_entity.get("organization_guid"), # Some events might not have an org associated
event_entity.get("metadata", {}),
)
elif self._api_version == "v3":
# Parse a v3 event
Expand All @@ -387,6 +389,7 @@ def parse_event(self, cf_event):
target["guid"],
cf_event.get("space", {}).get("guid", ""), # Some events might not have a space associated
cf_event.get("organization", {}).get("guid", ""), # Some events might not have an org associated
cf_event.get("data", {}),
)
return dd_event, event_guid, event_ts

Expand All @@ -403,8 +406,9 @@ def build_dd_event(
target_guid,
space_guid,
org_guid,
metadata,
):
# type: (str, str, int, str, str, str, str, str, str, str, str) -> Event
# type: (str, str, int, str, str, str, str, str, str, str, str, dict) -> Event
space_id = space_guid if space_guid else "none"
org_id = org_guid if org_guid else "none"
# we include both space_guid+space_id and org_guid+org_id; the *_guid are kept for
Expand All @@ -423,12 +427,15 @@ def build_dd_event(
"org_id:{}".format(org_id),
"org_name:{}".format(self.get_org_name(org_guid) if org_guid else "none"),
] + self._tags
metadata_json = "```\n{}\n```".format(json.dumps(metadata, sort_keys=True, indent=2))
dd_event = {
"source_type_name": SOURCE_TYPE_NAME,
"event_type": event_type,
"timestamp": event_ts,
"msg_title": "Event {} happened for {} {}".format(event_type, target_type, target_name),
"msg_text": "Triggered by {} {}".format(actor_type, actor_name),
"msg_text": "%%% \n Triggered by {} {}\n\nMetadata:\n{} \n %%%".format(
actor_type, actor_name, metadata_json
),
"priority": "normal",
"tags": tags,
"aggregation_key": event_guid,
Expand Down
6 changes: 4 additions & 2 deletions cloud_foundry_api/tests/fixtures/event_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"actee_type": "target_type",
"actee_name": "target_name",
"timestamp": "2020-06-08T00:00:02Z",
"metadata": {},
"metadata": {
"some": "metadata"
},
"space_guid": "space_guid",
"organization_guid": "org_guid"
}
}
}
6 changes: 4 additions & 2 deletions cloud_foundry_api/tests/fixtures/event_v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"type": "target_type",
"name": "target_name"
},
"data": {},
"data": {
"some": "metadata"
},
"space": {
"guid": "space_guid"
},
Expand All @@ -25,4 +27,4 @@
"href": ""
}
}
}
}
11 changes: 8 additions & 3 deletions cloud_foundry_api/tests/test_cloud_foundry_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def test_parse_event(build_dd_event_mock, _, __, ___, ____, event_v2, event_v3,
"target_guid",
"space_guid",
"org_guid",
{"some": "metadata"},
)

# v3
Expand All @@ -412,6 +413,7 @@ def test_parse_event(build_dd_event_mock, _, __, ___, ____, event_v2, event_v3,
"target_guid",
"space_guid",
"org_guid",
{"some": "metadata"},
)


Expand All @@ -432,6 +434,7 @@ def test_build_dd_event(_, __, ___, instance):
"target_guid",
"space_guid",
"org_guid",
{"some": "metadata"},
)
tags = [
"event_type:event_type",
Expand All @@ -452,14 +455,15 @@ def test_build_dd_event(_, __, ___, instance):
"event_type": "event_type",
"timestamp": 1234,
"msg_title": "Event event_type happened for target_type target_name",
"msg_text": "Triggered by actor_type actor_name",
"msg_text": "%%% \n Triggered by actor_type actor_name\n\n"
+ "Metadata:\n```\n{\n \"some\": \"metadata\"\n}\n``` \n %%%",
"priority": "normal",
"tags": tags,
"aggregation_key": "event_guid",
}
assert event == expected_event

# With no space and org
# With no space and org and metadata
event = check.build_dd_event(
"event_type",
"event_guid",
Expand All @@ -472,6 +476,7 @@ def test_build_dd_event(_, __, ___, instance):
"target_guid",
"",
"",
{},
)
tags = [
"event_type:event_type",
Expand All @@ -492,7 +497,7 @@ def test_build_dd_event(_, __, ___, instance):
"event_type": "event_type",
"timestamp": 1234,
"msg_title": "Event event_type happened for target_type target_name",
"msg_text": "Triggered by actor_type actor_name",
"msg_text": "%%% \n Triggered by actor_type actor_name\n\nMetadata:\n```\n{}\n``` \n %%%",
"priority": "normal",
"tags": tags,
"aggregation_key": "event_guid",
Expand Down

0 comments on commit 0f7b8b2

Please sign in to comment.