-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow HTTPX instrumentation to capture more request info, and improve API docs #655
Comments
@Kludex is currently working on headers in #671 which is close to finishing. After that comes capturing JSON bodies which @Kludex will also work on, I've provided him with a PoC implementation in slack. Then #668 can go through. We discussed eventually capturing other types of bodies:
I briefly toyed with the last case, leaving the code here in case we want it: from dataclasses import dataclass
from typing import Iterable
import httpx
from httpx._content import IteratorByteStream
import logfire
from logfire.propagate import attach_context, get_context, ContextCarrier
from opentelemetry.trace import get_current_span, Span
def content():
yield b"Hello, "
yield b"world!"
logfire.configure()
@dataclass
class LoggingStream:
stream: Iterable[bytes]
ctx: ContextCarrier
current_span: Span
def __iter__(self):
result = []
for chunk in self.stream:
result.append(chunk)
yield chunk
body = b"".join(result)
if self.current_span.is_recording():
# TODO this requires the bytes to be decodable as text
self.current_span.set_attribute("http.body", body)
else:
with attach_context(self.ctx):
logfire.info("Streamed body", body=body)
def request_hook(span, info):
record_body(info)
def record_body(info):
if isinstance(info.stream, IteratorByteStream):
info.stream._stream = LoggingStream(
info.stream._stream, get_context(), get_current_span()
)
logfire.instrument_httpx(request_hook=request_hook)
r = httpx.post("https://httpbin.org/post", content=content())
print(r.text) |
What else are we missing to close this? I can work on it today. |
|
@Kludex can you do 2-4? |
You working on 3? If so, I can work on 2-4 after. |
I am not. |
I don't understand if you are going to, if you think it shouldn't be added, or if you are implying that I can and should work on it. |
Waiting review: I'll document tomorrow. |
Description
We should add the following kwargs to
instrument_httpx
:capture_request_headers: bool | None = None
capture_request_body: bool | None = None
capture_response_headers: bool | None = None
capture_response_body: bool | None = None
capture_all: bool = False
- just sets all the above toTrue
We can raise an error if you use these together with
async_request_hook
etc.We can add a note saying that capturing the body in production might increase the amount of data collected significantly.
Also
HTTPXInstrumentKwargs
is not included in docs which makes it basically undocumented.We should probably do the same for requests.
The text was updated successfully, but these errors were encountered: