Skip to content

Commit

Permalink
Don't capture empty request bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki committed Feb 5, 2025
1 parent f872739 commit 3bafcc6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
12 changes: 7 additions & 5 deletions logfire/_internal/integrations/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ def capture_body(self):

def capture_body_if_text(self, attr_name: str = 'http.request.body.text'):
if not self.body_is_streaming:
try:
text = self.content.decode(self.content_type_charset)
except (UnicodeDecodeError, LookupError):
return
self.capture_text_as_json(attr_name=attr_name, text=text)
content = self.content
if content:
try:
text = self.content.decode(self.content_type_charset)
except (UnicodeDecodeError, LookupError):
return
self.capture_text_as_json(attr_name=attr_name, text=text)

def capture_body_if_form(self, attr_name: str = 'http.request.body.form') -> bool:
if not self.content_type_header_string == 'application/x-www-form-urlencoded':
Expand Down
34 changes: 34 additions & 0 deletions tests/otel_integrations/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,40 @@ async def test_httpx_client_capture_all(exporter: TestExporter):
)


async def test_httpx_client_no_capture_empty_body(exporter: TestExporter):
async with httpx.AsyncClient(transport=create_transport()) as client:
logfire.instrument_httpx(client, capture_request_body=True)
await client.get('https://example.org/')

assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'GET',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 2000000000,
'attributes': {
'http.method': 'GET',
'http.request.method': 'GET',
'http.url': 'https://example.org/',
'url.full': 'https://example.org/',
'http.host': 'example.org',
'server.address': 'example.org',
'network.peer.address': 'example.org',
'logfire.span_type': 'span',
'logfire.msg': 'GET /',
'http.status_code': 200,
'http.response.status_code': 200,
'http.flavor': '1.1',
'network.protocol.version': '1.1',
'http.target': '/',
},
}
]
)


def test_httpx_capture_all_and_other_flags_should_warn(exporter: TestExporter):
with httpx.Client(transport=create_transport()) as client:
with pytest.warns(
Expand Down

0 comments on commit 3bafcc6

Please sign in to comment.