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

Skip to content

Commit 3af7d1d

Browse files
committed
bug #48898 [HttpClient] Move Http clients data collecting at a late level (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
2 parents 05582da + 692a631 commit 3af7d1d

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function registerClient(string $name, TraceableHttpClient $client)
3737
* {@inheritdoc}
3838
*/
3939
public function collect(Request $request, Response $response, \Throwable $exception = null)
40+
{
41+
}
42+
43+
public function lateCollect()
4044
{
4145
$this->reset();
4246

@@ -50,12 +54,7 @@ public function collect(Request $request, Response $response, \Throwable $except
5054

5155
$this->data['request_count'] += \count($traces);
5256
$this->data['error_count'] += $errorCount;
53-
}
54-
}
5557

56-
public function lateCollect()
57-
{
58-
foreach ($this->clients as $client) {
5958
$client->reset();
6059
}
6160
}

src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Symfony\Component\HttpClient\DataCollector\HttpClientDataCollector;
1616
use Symfony\Component\HttpClient\NativeHttpClient;
1717
use Symfony\Component\HttpClient\TraceableHttpClient;
18-
use Symfony\Component\HttpFoundation\Request;
19-
use Symfony\Component\HttpFoundation\Response;
2018
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
2119

2220
class HttpClientDataCollectorTest extends TestCase
@@ -50,7 +48,7 @@ public function testItCollectsRequestCount()
5048
$sut->registerClient('http_client2', $httpClient2);
5149
$sut->registerClient('http_client3', $httpClient3);
5250
$this->assertEquals(0, $sut->getRequestCount());
53-
$sut->collect(new Request(), new Response());
51+
$sut->lateCollect();
5452
$this->assertEquals(3, $sut->getRequestCount());
5553
}
5654

@@ -79,7 +77,7 @@ public function testItCollectsErrorCount()
7977
$sut->registerClient('http_client2', $httpClient2);
8078
$sut->registerClient('http_client3', $httpClient3);
8179
$this->assertEquals(0, $sut->getErrorCount());
82-
$sut->collect(new Request(), new Response());
80+
$sut->lateCollect();
8381
$this->assertEquals(1, $sut->getErrorCount());
8482
}
8583

@@ -108,7 +106,7 @@ public function testItCollectsErrorCountByClient()
108106
$sut->registerClient('http_client2', $httpClient2);
109107
$sut->registerClient('http_client3', $httpClient3);
110108
$this->assertEquals([], $sut->getClients());
111-
$sut->collect(new Request(), new Response());
109+
$sut->lateCollect();
112110
$collectedData = $sut->getClients();
113111
$this->assertEquals(0, $collectedData['http_client1']['error_count']);
114112
$this->assertEquals(1, $collectedData['http_client2']['error_count']);
@@ -140,7 +138,7 @@ public function testItCollectsTracesByClient()
140138
$sut->registerClient('http_client2', $httpClient2);
141139
$sut->registerClient('http_client3', $httpClient3);
142140
$this->assertEquals([], $sut->getClients());
143-
$sut->collect(new Request(), new Response());
141+
$sut->lateCollect();
144142
$collectedData = $sut->getClients();
145143
$this->assertCount(2, $collectedData['http_client1']['traces']);
146144
$this->assertCount(1, $collectedData['http_client2']['traces']);
@@ -157,7 +155,7 @@ public function testItIsEmptyAfterReset()
157155
]);
158156
$sut = new HttpClientDataCollector();
159157
$sut->registerClient('http_client1', $httpClient1);
160-
$sut->collect(new Request(), new Response());
158+
$sut->lateCollect();
161159
$collectedData = $sut->getClients();
162160
$this->assertCount(1, $collectedData['http_client1']['traces']);
163161
$sut->reset();

0 commit comments

Comments
 (0)