Skip to content
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

profiler redesign #146

Merged
merged 1 commit into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- The real request method and target url are now displayed in the profiler.

### Changed

- The profiler design has been updated.

## 1.4.0 - 2017-02-21

### Changed
Expand Down
79 changes: 71 additions & 8 deletions Collector/ProfileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Http\HttplugBundle\Collector;

use Http\Client\Common\FlexibleHttpClient;
use Http\Client\Exception\HttpException;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target
Expand All @@ -27,12 +29,18 @@ class ProfileClient implements HttpClient, HttpAsyncClient
*/
private $collector;

/**
* @var Formatter
*/
private $formatter;

/**
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and
* HttpAsyncClient interfaces.
* @param Collector $collector
* @param Formatter $formatter
*/
public function __construct($client, Collector $collector)
public function __construct($client, Collector $collector, Formatter $formatter)
{
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
throw new \RuntimeException(sprintf(
Expand All @@ -45,39 +53,94 @@ public function __construct($client, Collector $collector)
}
$this->client = $client;
$this->collector = $collector;
$this->formatter = $formatter;
}

/**
* {@inheritdoc}
*/
public function sendAsyncRequest(RequestInterface $request)
{
$this->collectRequestInformations($request);
$stack = $this->collector->getCurrentStack();
$this->collectRequestInformations($request, $stack);

return $this->client->sendAsyncRequest($request);
return $this->client->sendAsyncRequest($request)->then(function (ResponseInterface $response) use ($stack) {
$this->collectResponseInformations($response, $stack);

return $response;
}, function (\Exception $exception) use ($stack) {
$this->collectExceptionInformations($exception, $stack);

throw $exception;
});
}

/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request)
{
$this->collectRequestInformations($request);
$stack = $this->collector->getCurrentStack();
$this->collectRequestInformations($request, $stack);

try {
$response = $this->client->sendRequest($request);

return $this->client->sendRequest($request);
$this->collectResponseInformations($response, $stack);

return $response;
} catch (\Exception $e) {
$this->collectExceptionInformations($e, $stack);

throw $e;
}
}

/**
* @param RequestInterface $request
* @param Stack|null $stack
*/
private function collectRequestInformations(RequestInterface $request)
private function collectRequestInformations(RequestInterface $request, Stack $stack = null)
{
if (!$stack = $this->collector->getCurrentStack()) {
if (!$stack) {
return;
}

$stack = $this->collector->getCurrentStack();
$stack->setRequestTarget($request->getRequestTarget());
$stack->setRequestMethod($request->getMethod());
$stack->setRequestScheme($request->getUri()->getScheme());
$stack->setRequestHost($request->getUri()->getHost());
$stack->setClientRequest($this->formatter->formatRequest($request));
}

/**
* @param ResponseInterface $response
* @param Stack|null $stack
*/
private function collectResponseInformations(ResponseInterface $response, Stack $stack = null)
{
if (!$stack) {
return;
}

$stack->setResponseCode($response->getStatusCode());
$stack->setClientResponse($this->formatter->formatResponse($response));
}

/**
* @param \Exception $exception
* @param Stack|null $stack
*/
private function collectExceptionInformations(\Exception $exception, Stack $stack = null)
{
if ($exception instanceof HttpException) {
$this->collectResponseInformations($exception->getResponse(), $stack);
}

if (!$stack) {
return;
}

$stack->setClientException($this->formatter->formatException($exception));
}
}
11 changes: 9 additions & 2 deletions Collector/ProfileClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,24 @@ class ProfileClientFactory implements ClientFactory
*/
private $collector;

/**
* @var Formatter
*/
private $formatter;

/**
* @param ClientFactory|callable $factory
* @param Collector $collector
* @param Formatter $formatter
*/
public function __construct($factory, Collector $collector)
public function __construct($factory, Collector $collector, Formatter $formatter)
{
if (!$factory instanceof ClientFactory && !is_callable($factory)) {
throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class));
}
$this->factory = $factory;
$this->collector = $collector;
$this->formatter = $formatter;
}

/**
Expand All @@ -50,6 +57,6 @@ public function createClient(array $config = [])
$client = new FlexibleHttpClient($client);
}

return new ProfileClient($client, $this->collector);
return new ProfileClient($client, $this->collector, $this->formatter);
}
}
126 changes: 126 additions & 0 deletions Collector/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ final class Stack
*/
private $requestMethod;

/**
* @var string
*/
private $requestHost;

/**
* @var string
*/
private $requestScheme;

/**
* @var string
*/
private $clientRequest;

/**
* @var string
*/
private $clientResponse;

/**
* @var string
*/
private $clientException;

/**
* @var int
*/
private $responseCode;

/**
* @param string $client
* @param string $request
Expand Down Expand Up @@ -151,4 +181,100 @@ public function setRequestMethod($requestMethod)
{
$this->requestMethod = $requestMethod;
}

/**
* @return string
*/
public function getClientRequest()
{
return $this->clientRequest;
}

/**
* @param string $clientRequest
*/
public function setClientRequest($clientRequest)
{
$this->clientRequest = $clientRequest;
}

/**
* @return mixed
*/
public function getClientResponse()
{
return $this->clientResponse;
}

/**
* @param mixed $clientResponse
*/
public function setClientResponse($clientResponse)
{
$this->clientResponse = $clientResponse;
}

/**
* @return string
*/
public function getClientException()
{
return $this->clientException;
}

/**
* @param string $clientException
*/
public function setClientException($clientException)
{
$this->clientException = $clientException;
}

/**
* @return int
*/
public function getResponseCode()
{
return $this->responseCode;
}

/**
* @param int $responseCode
*/
public function setResponseCode($responseCode)
{
$this->responseCode = $responseCode;
}

/**
* @return string
*/
public function getRequestHost()
{
return $this->requestHost;
}

/**
* @param string $requestHost
*/
public function setRequestHost($requestHost)
{
$this->requestHost = $requestHost;
}

/**
* @return string
*/
public function getRequestScheme()
{
return $this->requestScheme;
}

/**
* @param string $requestScheme
*/
public function setRequestScheme($requestScheme)
{
$this->requestScheme = $requestScheme;
}
}
2 changes: 1 addition & 1 deletion Collector/Twig/HttpMessageMarkupExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function markup($message)
// make header names bold
$headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $parts[0]);

return sprintf("%s\n\n<div class='httplug-http-body'>%s</div>", $headers, $parts[1]);
return sprintf("%s\n\n<div class='httplug-http-body httplug-hidden'>%s</div>", $headers, $parts[1]);
}

public function getName()
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/data-collector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,32 @@
<service id="httplug.collector.factory.buzz" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.buzz" public="false">
<argument type="service" id="httplug.collector.factory.buzz.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
</service>
<service id="httplug.collector.factory.curl" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.curl" public="false">
<argument type="service" id="httplug.collector.factory.curl.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
</service>
<service id="httplug.collector.factory.guzzle5" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.guzzle5" public="false">
<argument type="service" id="httplug.collector.factory.guzzle5.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
</service>
<service id="httplug.collector.factory.guzzle6" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.guzzle6" public="false">
<argument type="service" id="httplug.collector.factory.guzzle6.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
</service>
<service id="httplug.collector.factory.react" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.react" public="false">
<argument type="service" id="httplug.collector.factory.react.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
</service>
<service id="httplug.collector.factory.socket" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.socket" public="false">
<argument type="service" id="httplug.collector.factory.socket.inner"/>
<argument type="service" id="httplug.collector.collector"/>
<argument type="service" id="httplug.collector.formatter"/>
</service>
</services>
</container>
Loading