-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient][MonologBridge] Add HttpClientProcessor to debug HttpClient errors #35659
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
[HttpClient][MonologBridge] Add HttpClientProcessor to debug HttpClient errors #35659
Conversation
de54fe9
to
6ef1c2e
Compare
At the moment the processor is not able to log http client content for some reasons. I try to dig a bit and observed that this code is able to get the response content while the processor cannot: $response = $this->httpClient->request('GET', 'https://127.0.0.1:8000/sleep/1/throw');
try {
$response->getContent();
} catch(HttpExceptionInterface $e) {
dump($e->getResponse()->getContent(false));
} My guess is that the |
If you don't read the content, neither will the client. The destructor will ensure headers arrived and close the stream just after. Why would it care about the body if the app doesn't ? |
@nicolas-grekas Thanks for the quick answer. For example, I was calling an API to update a user profile but that request ends in a 400 because one of the field was wrongly formatted. |
Then this should be done on the app's side, not in the processor (or you're taking the risk of logging an ISO/AVI file content...) |
That's quite cumbersome to do sadly but I see your point. What do you think if we add safe guards like:
At the moment the processor truncates the content in log after 10.000 characters. |
How are people going to enable this processor? Maybe you can update the description of the PR to hint about this as that'd help write the doc afterward? |
Updated.
Yeap!
Yeap it's on my todo but I wanted to talk about it before going too far for 2 reasons:
|
I don't get this issue as the response is not destructed yet when the exception is processed, so you should be able to get the body
that's not possible, but if this is opt-in (eg by default the body is ever logged), then this is fine I think. |
I will set up a reproducer tomorrow hopefully.
Thanks! So what did you have in mind with this comment #34289 (comment)? |
My previous comment is not very relevant in the context of errors :) |
@nicolas-grekas Hello! Here is the reproducer https://github.com/B-Galati/symfony_reproducer_34289 Let me know for anything. |
while ($exception instanceof \Throwable) { | ||
if ($exception instanceof HttpExceptionInterface) { | ||
// It needs to be the 1st statement in order to fulfil the response info | ||
$responseContent = $exception->getResponse()->getContent(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this must be wrapped in a try/catch(TransportExceptionInterface)
|
||
$record['context']['http_client'][] = | ||
$exception->getResponse()->getInfo() | ||
+ ['response_content' => mb_strimwidth($responseContent, 0, 10000)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mb_strimwidth is random to me (all mb_* without encoding arguments are, relying on global state).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it should be removed? What would be the alternative otherwise, specify the $encoding
argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it can be removed if the response size is checked before trying to get the content. WDYT?
…ctor throwed an HttpExceptionInterface (nicolas-grekas) This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] fix getting response content after its destructor throwed an HttpExceptionInterface | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Spotted by @B-Galati in #35659 (comment) Commits ------- 6d1657b [HttpClient] fix getting response content after its destructor throwed an HttpExceptionInterface
OK
…------------------ 原始邮件 ------------------
发件人: "Benoit GALATI"<notifications@github.com>;
发送时间: 2020年2月12日(星期三) 凌晨1:00
收件人: "symfony/symfony"<symfony@noreply.github.com>;
抄送: "Subscribed"<subscribed@noreply.github.com>;
主题: Re: [symfony/symfony] [HttpClient][MonologBridge] Add HttpClientProcessor to debug HttpClient errors (#35659)
@B-Galati commented on this pull request.
In src/Symfony/Bridge/Monolog/Processor/HttpClientProcessor.php:
> + public function __invoke(array $record): array + { + $exception = $record['context']['exception'] ?? null; + + if ($exception === null) { + return $record; + } + + while ($exception instanceof \Throwable) { + if ($exception instanceof HttpExceptionInterface) { + // It needs to be the 1st statement in order to fulfil the response info + $responseContent = $exception->getResponse()->getContent(false); + + $record['context']['http_client'][] = + $exception->getResponse()->getInfo() + + ['response_content' => mb_strimwidth($responseContent, 0, 10000)]
I guess it can be removed if the response size is checked before trying to get the content no. WDYT?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
I'm not sure this is appropriate, especially if you consider prod: |
@nicolas-grekas Agree! I think |
In fact, cookies are initially set by response headers so that's not what we want :D |
@nicolas-grekas Sounds like a decorator using AsyncDecoratorTrait #36779 with a logger would do a better job than that no? |
@B-Galati |
Indeed! The LoggerDecorator would log a lot more information that may be useless. The regexp looks good to me 👍, very nice! |
@B-Galati What's the status of this PR? |
@fabpot still WIP. I am going to change the status. |
@B-Galati My question was more about the mergeability status estimated time. If it won't be ready in the next few weeks, I'd recommend to close it and reopen when ready. |
Sounds good to me 👍 |
Add HttpClient monolog processor that will write http request / response info in log context.
It can help debugging 4XX / 5XX in a prod context.
Processor can be enabled like this: