Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Closed
@pforesi

Description

@pforesi
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.