Skip to content

Commit 6a02cc6

Browse files
committed
fix(asm): work with non dictionary responses
1 parent 040a122 commit 6a02cc6

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

datadog_lambda/asm.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from copy import deepcopy
22
import logging
3-
from typing import Any, Dict, List, Optional
3+
from typing import Any, Dict, List, Optional, Union
44

55
from ddtrace.contrib.internal.trace_utils import _get_request_header_client_ip
66
from ddtrace.internal import core
@@ -134,19 +134,26 @@ def asm_start_response(
134134
span: Span,
135135
status_code: str,
136136
event_source: _EventSource,
137-
response: Dict[str, Any],
137+
response: Union[Dict[str, Any], str, None],
138138
):
139139
if event_source.event_type not in _http_event_types:
140140
return
141141

142-
headers = response.get("headers", {})
143-
multi_value_request_headers = response.get("multiValueHeaders")
144-
if multi_value_request_headers:
145-
response_headers = _merge_single_and_multi_value_headers(
146-
headers, multi_value_request_headers
147-
)
142+
if isinstance(response, dict) and (
143+
"headers" in response or "multiValueHeaders" in response
144+
):
145+
headers = response.get("headers", {})
146+
multi_value_request_headers = response.get("multiValueHeaders")
147+
if multi_value_request_headers:
148+
response_headers = _merge_single_and_multi_value_headers(
149+
headers, multi_value_request_headers
150+
)
151+
else:
152+
response_headers = headers
148153
else:
149-
response_headers = headers
154+
response_headers = {
155+
"content-type": "application/json",
156+
}
150157

151158
core.dispatch(
152159
"aws_lambda.start_response",

tests/test_asm.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@
181181
{},
182182
False, # Should not dispatch for non-HTTP events
183183
),
184+
(
185+
"api_gateway_v2_string_response",
186+
"api-gateway-v2-parametrized.json",
187+
"Hello, World!",
188+
"200",
189+
{"content-type": "application/json"},
190+
True,
191+
),
192+
(
193+
"api_gateway_v2_dict_response",
194+
"api-gateway-v2-parametrized.json",
195+
{"message": "Hello, World!"},
196+
"200",
197+
{"content-type": "application/json"},
198+
True,
199+
),
184200
]
185201

186202

0 commit comments

Comments
 (0)