Skip to content

Commit 685a6c2

Browse files
RD-14044-OTEL-FastAPI-inbound-request-body-not-parsed-correctly-1 (#954)
* fastapi request.body fix
1 parent 7a686f5 commit 685a6c2

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/lumigo_opentelemetry/instrumentations/fastapi/parsers/__init__.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ async def new_otel_receive(): # type: ignore
4242
return_value = await original_otel_receive()
4343
with lumigo_safe_execute("FastAPIParser: new_otel_receive"):
4444
with instance.tracer.start_as_current_span("receive_body") as send_span:
45-
send_span.set_attribute(
46-
"http.request.body",
47-
dump_with_context("requestBody", return_value),
48-
)
45+
if isinstance(return_value, dict) and "body" in return_value:
46+
send_span.set_attribute(
47+
"http.request.body",
48+
dump_with_context("requestBody", return_value.get("body")),
49+
)
50+
else:
51+
send_span.set_attribute(
52+
"http.request.body",
53+
dump_with_context("requestBody", return_value),
54+
)
4955
return return_value
5056

5157
return new_otel_receive

src/test/integration/fastapi/tests/test_fastapi.py

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ def test_requests_instrumentation(self):
120120
internals, "http.request.body"
121121
)
122122
)
123+
self.assertEqual(
124+
spans_container.get_attribute_from_list_of_spans(
125+
internals, "http.request.body"
126+
),
127+
'{"a": "b"}',
128+
)
123129
self.assertIsNotNone(
124130
spans_container.get_attribute_from_list_of_spans(
125131
internals, "http.response.headers"

0 commit comments

Comments
 (0)