Skip to content

Capture httpx response JSON bodies - #700

Merged
alexmojaki merged 3 commits into
mainfrom
alex/httpx-response-json
Dec 20, 2024
Merged

alexmojaki merged 3 commits into
mainfrom
alex/httpx-response-json

Conversation

@alexmojaki

Copy link
Copy Markdown
Contributor

No description provided.

@codecov

codecov Bot commented Dec 19, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (cac58a9) to head (c873e48).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #700   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          137       137           
  Lines        10929     11008   +79     
  Branches      1524      1537   +13     
=========================================
+ Hits         10929     11008   +79     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Dec 19, 2024 •

Copy link
Copy Markdown

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: c873e48
Status: ✅  Deploy successful!
Preview URL: https://827f3346.logfire-docs.pages.dev
Branch Preview URL: https://alex-httpx-response-json.logfire-docs.pages.dev

View logs

@alexmojaki
alexmojaki requested a review from Kludex December 19, 2024 14:17
Comment on lines +386 to +406
'http.request.header.host': ('example.org',),
'http.request.header.accept': ('*/*',),
'http.request.header.accept-encoding': ('gzip, deflate',),
'http.request.header.connection': ('keep-alive',),
'http.request.header.user-agent': (IsStr(),),
'http.request.header.content-length': (IsStr(),),
'http.request.header.content-type': ('application/json',),
'logfire.json_schema': '{"type":"object","properties":{"http.request.body.json":{"type":"object"}}}',
'http.request.body.json': '{"hello":"world"}',
'http.status_code': 200,
'http.response.status_code': 200,
'http.flavor': '1.1',
'network.protocol.version': '1.1',
'http.response.header.host': ('example.org',),
'http.response.header.accept': ('*/*',),
'http.response.header.accept-encoding': ('gzip, deflate',),
'http.response.header.connection': ('keep-alive',),
'http.response.header.user-agent': (IsStr(),),
'http.response.header.content-length': (IsStr(),),
'http.response.header.content-type': ('application/json',),
'http.response.header.traceparent': ('00-00000000000000000000000000000001-0000000000000003-01',),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it a bit weird that you have both the information about the request headers, body and response headers in a single span, but the response body on another one? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the time the body is read, the main span is already closed, which is why a new span is needed.

The best we can do for now is to copy the attributes from the main span into the new span so that the new span has everything. But we can't put the body in the main span.


def capture_response_json(logfire_instance: Logfire, response_info: ResponseInfo, is_async: bool) -> None:
headers = cast('httpx.Headers', response_info.headers)
if not headers.get('content-type', '').lower().startswith('application/json'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the lower is redundant.

@alexmojaki
alexmojaki merged commit 930fa87 into main Dec 20, 2024
@alexmojaki
alexmojaki deleted the alex/httpx-response-json branch December 20, 2024 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants