Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] HttpClientDataCollector does not collect data of requests sent within StreamedResponse callback function #48894

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
pforesi opened this issue Jan 6, 2023 · 2 comments

Comments

@pforesi
Copy link
Contributor

pforesi commented Jan 6, 2023

Symfony version(s) affected

4.4.1 (https://github.com/symfony/symfony/blob/0f51da6ec7c3e905fcfebd49e99e0b4a12a3a733/src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php)

Description

When Controller's action method is returning a StreamedResponse like following sample code:

`

public function event(HttpClientInterface $client): StreamedResponse
{
   $response = new StreamedResponse();
    $response->setCallback(function () use ($client) {
        $url = 'https://symfony.com';
        $response = $client->request('GET', $url);
    });
    return $response;
}

`

And the callback method is sending an HTTP request thru the symfony HTTP Client.

The bug: The profiler's Http Client panel is empty.

According our investigation, this is due to the fact that kernel.response event is dispatched (and thus the call to HttpClientDataCollector::collect() method) before HttpClient send the request, so no data are yet collected.

HttpClientDataCollector is already implementing LateDataCollectorInterface but the lateCollect() is only resetting the client object.

How to reproduce

  1. Fork this github repo: https://github.com/pforesi/HttpClientDataCollector_issue
  2. Launch the app (symfony server:start)
  3. Browse https://127.0.0.1:8000/bug/reproducer
  4. Follow the " Steps to check the bug" on the served page.

Possible Solution

Do data collecting into the lateCollect() instead of the collect() method:

`

public function collect(Request $request, Response $response, \Throwable $exception = null)
{
    $this->reset();
}

public function lateCollect()
{
    foreach ($this->clients as $name => $client) {
        [$errorCount, $traces] = $this->collectOnClient($client);

        $this->data['clients'][$name] = [
            'traces' => $traces,
            'error_count' => $errorCount,
        ];

        $this->data['request_count'] += \count($traces);
        $this->data['error_count'] += $errorCount;
    }
    foreach ($this->clients as $client) {
        $client->reset();
    }
}

`

Additional Context

No response

@OskarStark
Copy link
Contributor

Hi, we do not support 4.4 anymore, except for security issues. You should consider upgrading and check if the bug is already fixed.

Thank you

@OskarStark
Copy link
Contributor

Oh thanks for the PR against 5.4 👌🏻

@fabpot fabpot closed this as completed Jan 9, 2023
fabpot added a commit that referenced this issue Jan 9, 2023
…evel (pforesi)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[HttpClient] Move Http clients data collecting at a late level

This allows to collect http clients data when sending them in a StreamedResponse callback method

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #48894
| License       | MIT
| Doc PR        |
<!--
Replace this notice by a short README for your feature/bugfix.
This will help reviewers and should be a good start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the latest branch.
 - For new features, provide some code snippets to help understand usage.
 - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
 - Never break backward compatibility (see https://symfony.com/bc).
-->
When a controller action is returning a StreamedResponse doing some HttpClient requests, http clients data are collected to early, so the Web profiler HttpClient panel is wrongly empty.
To fix this we moved data collecting to the HttpClientDataCollector::lateCollect() method which is called on ther kernel.terminate event.

Commits
-------

692a631 [HttpClient] Move Http clients data collecting at a late level
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants