Skip to content

Commit afc5057

Browse files
authored
[Logs forwarder] Fix step function tags fetch (DataDog#855)
* [Logs forwarder] Fix step function tags fetch * update tests
1 parent 67550ba commit afc5057

File tree

6 files changed

+18
-34
lines changed

6 files changed

+18
-34
lines changed

aws/logs_monitoring/caching/step_functions_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _get_state_machine_tags(self, state_machine_arn: str):
132132
except Exception as e:
133133
self.logger.exception(f"Failed to get Step Functions tags due to {e}")
134134

135-
if len(response.get("ResourceTagMappingList", {})) > 0:
135+
if response and len(response.get("ResourceTagMappingList", {})) > 0:
136136
resource_dict = response.get("ResourceTagMappingList")[0]
137137
for a_tag in resource_dict.get("Tags", []):
138138
key = sanitize_aws_tag_string(a_tag["Key"], remove_colons=True)

aws/logs_monitoring/steps/handlers/awslogs_handler.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,13 @@ def handle_rds_source(self):
160160

161161
def handle_step_function_source(self):
162162
if not self.aws_attributes.get_log_stream().startswith("states/"):
163-
pass
164-
state_machine_arn = ""
163+
return
165164

166-
try:
167-
state_machine_arn = self.get_state_machine_arn()
168-
if state_machine_arn: # not empty
169-
self.metadata[DD_HOST] = state_machine_arn
170-
except Exception as e:
171-
logger.debug(
172-
"Unable to set stepfunction host or get state_machine_arn: %s" % e
173-
)
165+
state_machine_arn = self.get_state_machine_arn()
166+
if not state_machine_arn:
167+
return
174168

169+
self.metadata[DD_HOST] = state_machine_arn
175170
formatted_stepfunctions_tags = (
176171
self.cache_layer.get_step_functions_tags_cache().get(state_machine_arn)
177172
)
@@ -214,12 +209,15 @@ def process_eks_logs(self):
214209
# In case the conditions above don't match we maintain eks as the source
215210

216211
def get_state_machine_arn(self):
217-
message = json.loads(self.aws_attributes.get_log_events()[0].get("message"))
218-
if message.get("execution_arn") is not None:
219-
execution_arn = message["execution_arn"]
220-
arn_tokens = re.split(r"[:/\\]", execution_arn)
221-
arn_tokens[5] = "stateMachine"
222-
return ":".join(arn_tokens[:7])
212+
try:
213+
message = json.loads(self.aws_attributes.get_log_events()[0].get("message"))
214+
if message.get("execution_arn") is not None:
215+
execution_arn = message["execution_arn"]
216+
arn_tokens = re.split(r"[:/\\]", execution_arn)
217+
arn_tokens[5] = "stateMachine"
218+
return ":".join(arn_tokens[:7])
219+
except Exception as e:
220+
logger.debug("Unable to get state_machine_arn: %s" % e)
223221
return ""
224222

225223
# Lambda logs can be from either default or customized log group

aws/logs_monitoring/tests/approved_files/TestAWSLogsHandler.test_awslogs_handler_step_functions_tags_added_properly.approved.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
},
1010
"id": "37199773595581154154810589279545129148442535997644275712",
11-
"message": "{\"id\":\"1\",\"type\":\"ExecutionStarted\",\"details\":{\"input\":\"{\"Comment\": \"Insert your JSON here\"}\",\"inputDetails\":{\"truncated\":false},\"roleArn\":\"arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03\"},\",previous_event_id\":\"0\",\"event_timestamp\":\"1668095539607\",\"execution_arn\":\"arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:d0dbefd8-a0f6-b402-da4c-f4863def7456:7fa0cfbe-be28-4a20-9875-73c37f5dc39e\"}",
11+
"message": "{\"id\": \"1\",\"type\": \"ExecutionStarted\",\"details\": {\"input\": \"{}\",\"inputDetails\": {\"truncated\": \"false\"},\"roleArn\": \"arn:aws:iam::12345678910:role/service-role/StepFunctions-test-role-a0iurr4pt\"},\"previous_event_id\": \"0\",\"event_timestamp\": \"1716992192441\",\"execution_arn\": \"arn:aws:states:us-east-1:12345678910:execution:StepFunction1:ccccccc-d1da-4c38-b32c-2b6b07d713fa\",\"redrive_count\": \"0\"}",
1212
"timestamp": 1668095539607
1313
}
1414
]
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ddsource": "stepfunction",
33
"ddtags": "env:dev,test_tag_key:test_tag_value,dd_step_functions_trace_enabled:true",
4-
"host": "/aws/vendedlogs/states/logs-to-traces-sequential-Logs",
4+
"host": "arn:aws:states:us-east-1:12345678910:stateMachine:StepFunction1",
55
"service": "stepfunction"
66
}

aws/logs_monitoring/tests/test_awslogs_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_awslogs_handler_step_functions_tags_added_properly(
9393
{
9494
"id": "37199773595581154154810589279545129148442535997644275712",
9595
"timestamp": 1668095539607,
96-
"message": '{"id":"1","type":"ExecutionStarted","details":{"input":"{"Comment": "Insert your JSON here"}","inputDetails":{"truncated":false},"roleArn":"arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03"},",previous_event_id":"0","event_timestamp":"1668095539607","execution_arn":"arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:d0dbefd8-a0f6-b402-da4c-f4863def7456:7fa0cfbe-be28-4a20-9875-73c37f5dc39e"}',
96+
"message": '{"id": "1","type": "ExecutionStarted","details": {"input": "{}","inputDetails": {"truncated": "false"},"roleArn": "arn:aws:iam::12345678910:role/service-role/StepFunctions-test-role-a0iurr4pt"},"previous_event_id": "0","event_timestamp": "1716992192441","execution_arn": "arn:aws:states:us-east-1:12345678910:execution:StepFunction1:ccccccc-d1da-4c38-b32c-2b6b07d713fa","redrive_count": "0"}',
9797
}
9898
],
9999
}

aws/logs_monitoring/tools/integration_tests/snapshots/step_functions_log.json~snapshot

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,6 @@
8787
],
8888
"type": "distribution"
8989
},
90-
{
91-
"device": null,
92-
"host": null,
93-
"interval": 10,
94-
"metric": "aws.dd_forwarder.local_step_functions_tags_cache_expired",
95-
"points": "<redacted from snapshot>",
96-
"tags": [
97-
"forwardername:test",
98-
"forwarder_memorysize:1536",
99-
"forwarder_version:<redacted from snapshot>",
100-
"event_type:awslogs"
101-
],
102-
"type": "distribution"
103-
},
10490
{
10591
"device": null,
10692
"host": null,

0 commit comments

Comments
 (0)