Skip to content

Deduplicate request logs, couple request<>response by UID #19

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

Closed
wants to merge 4 commits into from
Closed

Deduplicate request logs, couple request<>response by UID #19

wants to merge 4 commits into from

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Feb 4, 2022

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets fixes #18
Documentation if this is a new feature, link to pull request in https://github.com/php-http/documentation that adds relevant documentation
License MIT

Given

httplug:
    clients:
        test:
            service: 'psr18.http_client'
            plugins:
                - httplug.plugin.logger
        test_with_error_handler:
            service: 'psr18.http_client'
            plugins:
                - httplug.plugin.logger
                - httplug.plugin.error
$request = $this->requestFactory->createRequest('GET', 'https://httpbin.org/status/200');
$requestError = $this->requestFactory->createRequest('GET', 'https://httpbin.org/status/500');

$this->test->sendRequest($request);
$this->test->sendRequest($requestError);
$this->testWithErrorHandler->sendRequest($requestError);
18:54:46 INFO      [httplug] Sending request:
GET https://httpbin.org/status/200 1.1 ["request" => Nyholm\Psr7\Request { …},"uid" => "61fd6866755f45.27019150"]
18:54:46 INFO      [httplug] Received response:
200 OK 1.1 ["request" => Nyholm\Psr7\Request { …},"response" => Nyholm\Psr7\Response { …},"milliseconds" => 490,"uid" => "61fd6866755f45.27019150"]

18:54:46 INFO      [httplug] Sending request:
GET https://httpbin.org/status/500 1.1 ["request" => Nyholm\Psr7\Request { …},"uid" => "61fd6866ed29b5.48079388"]
18:54:47 INFO      [httplug] Received response:
500 Internal Server Error 1.1 ["request" => Nyholm\Psr7\Request { …},"response" => Nyholm\Psr7\Response { …},"milliseconds" => 129,"uid" => "61fd6866ed29b5.48079388"]

18:54:47 INFO      [httplug] Sending request:
GET https://httpbin.org/status/500 1.1 ["request" => Nyholm\Psr7\Request { …},"uid" => "61fd686718a270.30706898"]
18:54:47 ERROR     [httplug] Error:
Internal Server Error
with response:
500 Internal Server Error 1.1 ["request" => Nyholm\Psr7\Request { …},"response" => Nyholm\Psr7\Response { …},"exception" => Http\Client\Common\Exception\ServerErrorException { …},"milliseconds" => 107,"uid" => "61fd686718a270.30706898"]

For me this works OK, and could go as a patch release. But any next version works for us 👍

ro0NL added 2 commits February 4, 2022 17:08

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

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

thanks, looks good to me. can you please look at the failing tests? i think the tests have assertions on how the logger is called (which makes sense, as this is the whole purpose of this plugin).

i think i see this as a new minor version, as it adds something significant.

please add a note to the changelog about the change. i think it would be correct to mention there to configure a logger with the fingerscrossed behaviour (don't need detailed instructions, just mention it so people can find the doc if they are not familiar). because if you only log something more severe than info, this new behaviour will no longer let you see the request that led to an error.

@dbu
Copy link
Contributor

dbu commented Feb 7, 2022

we should also update https://github.com/php-http/documentation/blob/master/plugins/logger.rst to have the example use a fingers crossed logger and a note why we use that instead of a straight logger.

ro0NL added 2 commits February 7, 2022 09:25

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@ro0NL
Copy link
Contributor Author

ro0NL commented Feb 7, 2022

@dbu done. I'll let you update docs, i dont see how "fingerscrossed" is relevant. We always log certain channels, that are used dedicated for http traffic.

$milliseconds = (int) round((hrtime(true) / 1E6 - $start));
if ($exception instanceof Exception\HttpException) {
$this->logger->error(
sprintf("Error:\n%s\nwith response:\n%s\n\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)),
sprintf("Error:\n%s\nwith response:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse())),
[
'request' => $request,
Copy link
Contributor

Choose a reason for hiding this comment

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

you still specify the request in the context. is that on purpose?

Copy link
Contributor Author

@ro0NL ro0NL Feb 8, 2022

Choose a reason for hiding this comment

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

yes, i left other context entries untouched.

this is for #16 to solve, but in CLI it works well:

16:07:07 INFO      [app] Received response:
HTTP/1.1 200 OK

[
  "request" => GuzzleHttp\Psr7\Request {
    -method: "GET"

i wouldnt mind if these are dropped though :) if these objects become stringable/serializable one day, it starts leaking into prod logs.

Arguably, even in dev env it's just noise.

@dbu
Copy link
Contributor

dbu commented Feb 9, 2022

thanks a lot!

i addjusted the changelog and squashed commits, then merged locally.

@dbu dbu closed this Feb 9, 2022
@dbu
Copy link
Contributor

dbu commented Feb 9, 2022

i wait with tagging until #16 is clarified and maybe php-http/message#146 as well

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.

Couple request with response using context
2 participants