Symfony 5.4.20 HTTP Client面板为空,如何开启请求监控?
问题:Symfony 5.4.20升级后HTTP Client面板无请求记录
我使用Symfony的HTTP Client组件调用Elasticsearch API,相关配置与代码如下:
# config/packages/framework.yaml http_client: scoped_clients: elasticsearch.client: base_uri: 'http://%elasticsearch.host%:%elasticsearch.port%/%elasticsearch.index%/%elasticsearch.document%' headers: 'Accept': 'application/json' 'Content-Type': 'application/json' retry_failed: max_retries: 3
<?php use Symfony\Contracts\HttpClient\HttpClientInterface; class ElasticsearchEngine { private $elasticsearchClient; public function __construct(HttpClientInterface $elasticsearchClient) { $this->elasticsearchClient = $elasticsearchClient; } public function search($query, ?int $limit = null, array $options = []) { $response = $this->elasticsearchClient->request('GET', '/_search', [ 'json' => $query ]); }
class myController extends AbstractController { /** * @Route("/httpcall", name="http_call") */ public function httpCallAction(RequestStack $requestStack, ElasticsearchEngine $engine) { // retrieve query //$query = .... $engine->search($query); return new Response( '<html><body>Action '.__METHOD__.'</body></html>' ); }
接口可正常返回正确结果,但升级到Symfony 5.4.20后,HTTP Client面板显示“未发起任何HTTP请求”,请问该情况原因是什么?如何开启监控?
原因分析
- Symfony 5.4.20对HTTP Client的监控逻辑做了调整,延迟执行的请求默认不会被自动收集。HTTP Client采用惰性执行机制,仅在调用
$response->getContent()、$response->toArray()等方法时才会实际发送请求;当前代码仅调用request()方法,未触发实际请求执行,因此面板无记录。 - 部分版本更新后,Scoped Client的请求追踪需要显式配置开启。
解决方法
1. 确保请求被实际执行
修改ElasticsearchEngine的search方法,调用响应内容获取方法触发请求发送:
public function search($query, ?int $limit = null, array $options = []) { $response = $this->elasticsearchClient->request('GET', '/_search', [ 'json' => $query ]); // 触发请求执行并返回结果 return $response->toArray(); }
2. 显式开启Scoped Client的追踪
在framework.yaml中为目标Scoped Client添加追踪配置:
# config/packages/framework.yaml http_client: scoped_clients: elasticsearch.client: base_uri: 'http://%elasticsearch.host%:%elasticsearch.port%/%elasticsearch.index%/%elasticsearch.document%' headers: 'Accept': 'application/json' 'Content-Type': 'application/json' retry_failed: max_retries: 3 # 开启该客户端的请求追踪 trace: true # 可选:追踪请求内容(生产环境需注意敏感数据风险) trace_content: true
3. 确认Web Profiler的HTTP Client监控已启用
检查config/packages/web_profiler.yaml,确保HTTP Client面板配置开启:
web_profiler: toolbar: true intercept_redirects: false excluded_ajax_paths: '^/(_(profiler|wdt)|css|images|js)/' profiles: http_client: true
内容的提问来源于stack exchange,提问作者akio
相关产品推荐
相关产品推荐

