Skip to content

Added duration header #3

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 12 additions & 5 deletions spec/StopwatchPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Http\Promise\RejectedPromise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Stopwatch\StopwatchEvent;

class StopwatchPluginSpec extends ObjectBehavior
{
Expand All @@ -29,14 +29,21 @@ function it_is_a_plugin()
$this->shouldImplement('Http\Client\Common\Plugin');
}

function it_records_event(Stopwatch $stopwatch, RequestInterface $request, ResponseInterface $response, UriInterface $uri)
{
function it_records_event(
Stopwatch $stopwatch,
RequestInterface $request,
ResponseInterface $response,
UriInterface $uri,
StopwatchEvent $event
) {
$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
$uri->__toString()->willReturn('http://foo.com/bar');
$event->getDuration()->willReturn(123);

$stopwatch->start('GET http://foo.com/bar', 'php_http.request')->shouldBeCalled();
$stopwatch->stop('GET http://foo.com/bar', 'php_http.request')->shouldBeCalled();
$stopwatch->stop('GET http://foo.com/bar')->shouldBeCalled()->willReturn($event);
$response->withHeader('X-Duration', 123)->shouldBeCalled();

$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
Expand All @@ -52,7 +59,7 @@ function it_records_event_on_error(Stopwatch $stopwatch, RequestInterface $reque
$uri->__toString()->willReturn('http://foo.com/bar');

$stopwatch->start('GET http://foo.com/bar', 'php_http.request')->shouldBeCalled();
$stopwatch->stop('GET http://foo.com/bar', 'php_http.request')->shouldBeCalled();
$stopwatch->stop('GET http://foo.com/bar')->shouldBeCalled();

$next = function (RequestInterface $request) {
return new RejectedPromise(new NetworkException('', $request));
Expand Down
7 changes: 4 additions & 3 deletions src/StopwatchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
final class StopwatchPlugin implements Plugin
{
const CATEGORY = 'php_http.request';
const HEADER = 'X-Duration';

/**
* @var Stopwatch
Expand All @@ -39,11 +40,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
$this->stopwatch->start($eventName, self::CATEGORY);

return $next($request)->then(function (ResponseInterface $response) use ($eventName) {
$this->stopwatch->stop($eventName, self::CATEGORY);
$event = $this->stopwatch->stop($eventName);

return $response;
return $response->withHeader(self::HEADER, $event->getDuration());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if there is a more elegant way than modifying the response with an additional header.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK the stopwatch is supposed to be accessed from the outside as well so you can directly get event information from it. Isn't it what we do in HttplugBundle?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, same here, but i didn't find a better one. i think getting the information out of stopwatch in collector is not good option. i could introduce some kind of request registry which is used by stopwatch plugin and collector, but i think it would harden the coupling, which might not be the best idea as well. so i decided to go with a header and i think if a plugin manipulates the response with an additional header that's not such a weird behavior, but i clearly get your point. did i miss a good option to avoid the header?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, so the problem is that we cannot map a request to a specific stopwatch event, right?

Because my idea was to use the stopwatch class and get the event based on something: http://api.symfony.com/3.2/Symfony/Component/Stopwatch/Stopwatch.html#method_getEvent

Maybe we could introduce an internal Request/Response ID header and use that in the event name, but that still feels hacky.

}, function (Exception $exception) use ($eventName) {
$this->stopwatch->stop($eventName, self::CATEGORY);
$this->stopwatch->stop($eventName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, the category parameter seems unneeded according to https://github.com/symfony/stopwatch/blob/master/Stopwatch.php


throw $exception;
});
Expand Down