Skip to content

Commit b284f73

Browse files
Patch 1 (#1050)
* Patch service tag extraction extract_ddtags_from_message checks for a service tag override using `if "service:" in` which returns true if there's a tag key that ends with `service`, eg `mail_send_service:production`. This causes the lambda to enter the conditional to extract the tag to a first class datadog tag, but the enclosed logic includes a non-defaulted generator which checks for `startswith("service:")`. Since this second check does _not_ match the example, the generator has no elements, and the call to `next` causes the lambda to crash. This change swaps the check to use the same split/startswith functionality that's used inside the conditional. * Include unit test for this edge case * linter yelled --------- Co-authored-by: eric-teambridge <[email protected]>
1 parent 1691ff4 commit b284f73

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

aws/logs_monitoring/steps/enrichment.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,11 @@ def extract_ddtags_from_message(event):
161161
extracted_ddtags = extracted_ddtags.replace(" ", "")
162162

163163
# Extract service tag from message.ddtags if exists
164-
if "service:" in extracted_ddtags:
165-
event[DD_SERVICE] = next(
166-
tag[8:]
167-
for tag in extracted_ddtags.split(",")
168-
if tag.startswith("service:")
169-
)
164+
service_tags = [
165+
tag for tag in extracted_ddtags.split(",") if tag.startswith("service:")
166+
]
167+
if service_tags:
168+
event[DD_SERVICE] = service_tags[0][8:]
170169
event[DD_CUSTOM_TAGS] = ",".join(
171170
[
172171
tag

aws/logs_monitoring/tests/test_enrichment.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,31 @@ def test_extract_ddtags_handles_empty_spaces(self):
146146
"my_custom_service",
147147
)
148148

149+
def test_extract_ddtags_service_substring_not_prefix(self):
150+
"""Test that service tag extraction handles cases where 'service:' appears
151+
as a substring but not as a tag prefix (e.g., in 'myservice:api').
152+
153+
Before the fix (old code):
154+
- "service:" in extracted_ddtags would be True
155+
- But next() would raise StopIteration since no tag starts with "service:"
156+
157+
After the fix (new code):
158+
- List comprehension returns empty list when no tag starts with "service:"
159+
- No exception is raised, service field is not set
160+
"""
161+
loaded_message_tags = {"ddtags": "env:prod,myservice:api,foo:bar"}
162+
event = {"message": loaded_message_tags, "ddtags": "custom_tag:value"}
163+
164+
extract_ddtags_from_message(event)
165+
166+
# Service should not be set since no tag actually starts with "service:"
167+
self.assertNotIn("service", event)
168+
# All tags should be preserved in the correct order
169+
self.assertEqual(
170+
event["ddtags"],
171+
"custom_tag:value,env:prod,myservice:api,foo:bar",
172+
)
173+
149174

150175
class TestExtractHostFromLogEvents(unittest.TestCase):
151176
def test_parse_source_cloudtrail(self):

0 commit comments

Comments
 (0)