Skip to content

Commit

Permalink
[php-sdk] fix BlackfiredHttpClient sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-jean committed Nov 3, 2023
1 parent 54c2d79 commit 2750a71
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Blackfire/Bridge/Symfony/BlackfiredHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Blackfire\Client as BlackfireClient;
use Blackfire\Profile\Configuration as ProfileConfiguration;
use Blackfire\Profile\Request;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\HttpClientTrait;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -42,7 +41,11 @@ public function request(string $method, string $url, array $options = array()):
// this normalizes HTTP headers and allows direct access to $options['headers']['x-blackfire-query']
// without checking the header name case sensitivity
[, $options] = self::prepareRequest($method, $url, $options, static::OPTIONS_DEFAULTS);
$profileRequest = null;
if (!isset($options['extra'])) {
$options['extra'] = array();
}

$profileRequest = $options['extra']['profile_request'] ?? null;

if ($this->shouldAutoEnable() && !isset($options['extra']['blackfire'])) {
$options['extra']['blackfire'] = new ProfileConfiguration();
Expand All @@ -64,30 +67,33 @@ public function request(string $method, string $url, array $options = array()):
throw new \InvalidArgumentException('blackfire must be true or an instance of \Blackfire\Profile\Configuration.');
}

$profileRequest = $this->blackfire->createRequest($options['extra']['blackfire']);
if (!$profileRequest) {
$options['extra']['profile_request'] = $this->blackfire->createRequest($options['extra']['blackfire']);
}

if (isset($probe)) {
$probe->enable();
}

$options['headers']['X-Blackfire-Query'] = $profileRequest->getToken();
$options['headers']['X-Blackfire-Profile-Url'] = $profileRequest->getProfileUrl();
$options['headers']['X-Blackfire-Profile-Uuid'] = $profileRequest->getUuid();
$options['headers']['X-Blackfire-Query'] = $options['extra']['profile_request']->getToken();
$options['headers']['X-Blackfire-Profile-Url'] = $options['extra']['profile_request']->getProfileUrl();
$options['headers']['X-Blackfire-Profile-Uuid'] = $options['extra']['profile_request']->getUuid();
}

$response = $this->client->request($method, $url, $options);

return $this->processResponse($method, $url, $options, $response, $profileRequest);
return $this->processResponse($method, $url, $options, $response);
}

public function stream($responses, float $timeout = null): ResponseStreamInterface
{
return $this->client->stream($responses, $timeout);
}

private function processResponse($method, $url, array $options, ResponseInterface $response, Request $request = null)
private function processResponse($method, $url, array $options, ResponseInterface $response)
{
$headers = $response->getHeaders(false);
$request = $options['extra']['profile_request'] ?? null;

if (!isset($headers['x-blackfire-response'])) {
if (null !== $this->logger) {
Expand Down Expand Up @@ -117,6 +123,8 @@ private function processResponse($method, $url, array $options, ResponseInterfac
return new BlackfiredHttpResponse($response, $request);
}

$options['extra']['profile_request'] = $request;

return $this->request($method, $url, $options);
}

Expand Down

0 comments on commit 2750a71

Please sign in to comment.