Skip to content

Commit

Permalink
The timing is now by default a range
Browse files Browse the repository at this point in the history
Add compatibility with time-based filter of Kibana by adding the `@timestamp` field
  • Loading branch information
sroze committed Feb 7, 2016
1 parent a1f39c4 commit 793d093
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Tolerance\MessageProfile\Peer\MessagePeer;
use Tolerance\MessageProfile\Peer\Resolver\PeerResolver;
use Tolerance\MessageProfile\Storage\ProfileStorage;
use Tolerance\MessageProfile\Timing\MessageTiming;


class StoresRequestProfileWhenKernelTerminatesSpec extends ObjectBehavior
Expand All @@ -23,6 +24,8 @@ function let(HttpFoundationProfileFactory $httpFoundationProfileFactory, Profile

function it_stores_the_request_profile_when_the_kernel_terminates(PostResponseEvent $event, HttpFoundationProfileFactory $httpFoundationProfileFactory, ProfileStorage $profileStorage, MessageProfile $messageProfile, PeerResolver $peerResolver, MessagePeer $messagePeer)
{
$messageProfile->withTiming(Argument::type(MessageTiming::class))->willReturn($messageProfile);

$request = Request::create('/');
$response = Response::create(null, 200);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ public function create()
{
return function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$start = microtime(true);
$start = \DateTime::createFromFormat('U.u', microtime(true));

return $handler($request, $options)->then(function (ResponseInterface $response) use ($start, $request) {
$timing = SimpleMessageTiming::fromMilliseconds((microtime(true) - $start) * 1000);
$this->storeProfile($request, $response, $timing);
$end = \DateTime::createFromFormat('U.u', microtime(true));
$this->storeProfile($request, $response, SimpleMessageTiming::fromRange($start, $end));

return $response;
}, function ($reason) use ($start, $request) {
$response = $reason instanceof RequestException ? $reason->getResponse() : null;

$timing = SimpleMessageTiming::fromMilliseconds((microtime(true) - $start) * 1000);
$this->storeProfile($request, $response, $timing);
$end = \DateTime::createFromFormat('U.u', microtime(true));
$this->storeProfile($request, $response, SimpleMessageTiming::fromRange($start, $end));

throw $reason;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function configureMessageProfileStorage(ContainerBuilder $container, Loa
new Reference($config['elastica']),
]
));
} else if (false !== $config['in_memory']) {
} elseif (false !== $config['in_memory']) {
$storage = 'tolerance.message_profile.storage.in_memory';
} else {
throw new \RuntimeException('Unable to configure Request Identifier storage');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GenerateRequestIdentifierIfNotInHeaders

/**
* @param RequestIdentifierResolver $requestIdentifierResolver
* @param string $headerName
* @param string $headerName
*/
public function __construct(RequestIdentifierResolver $requestIdentifierResolver, $headerName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Tolerance\MessageProfile\HttpRequest\HttpFoundation\HttpFoundationProfileFactory;
use Tolerance\MessageProfile\Peer\Resolver\PeerResolver;
use Tolerance\MessageProfile\Storage\ProfileStorage;
use Tolerance\MessageProfile\Timing\SimpleMessageTiming;

final class StoresRequestProfileWhenKernelTerminates
{
Expand Down Expand Up @@ -53,7 +54,26 @@ public function onKernelTerminate(PostResponseEvent $event)
$receiver = $this->peerResolver->resolve();

$profile = $this->profileFactory->fromRequestAndResponse($event->getRequest(), $event->getResponse(), null, $receiver);
$profile = $profile->withTiming($this->generateTiming());

$this->profileStorage->store($profile);
}

/**
* @return SimpleMessageTiming
*/
private function generateTiming()
{
$start = array_key_exists('REQUEST_TIME_FLOAT', $_SERVER) ?
\DateTime::createFromFormat('U.u', (double) $_SERVER['REQUEST_TIME_FLOAT']) :
(array_key_exists('REQUEST_TIME', $_SERVER) ?
\DateTime::createFromFormat('U', (int) $_SERVER['REQUEST_TIME']) :
new \DateTime()
)
;

$end = \DateTime::createFromFormat('U.u', microtime(true));

return SimpleMessageTiming::fromRange($start, $end);
}
}
1 change: 0 additions & 1 deletion src/Tolerance/MessageProfile/AbstractMessageProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* This class is basically a workaround to the fact that an interface can't hold
* a discriminator map.
*
*/
abstract class AbstractMessageProfile implements MessageProfile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Tolerance\MessageProfile\HttpRequest\HttpFoundation\RequestIdentifier\RequestIdentifierResolver;
use Tolerance\MessageProfile\HttpRequest\HttpMessageProfile;
use Tolerance\MessageProfile\Peer\MessagePeer;
use Tolerance\MessageProfile\Timing\SimpleMessageTiming;

final class SimpleHttpFoundationProfileFactory implements HttpFoundationProfileFactory
{
Expand All @@ -43,30 +42,10 @@ public function fromRequestAndResponse(Request $request, Response $response = nu
$sender,
$recipient,
[],
$this->generateTiming(),
null,
$request->getMethod(),
$request->getRequestUri(),
$request->getUri(),
null !== $response ? $response->getStatusCode() : 0
);
}

/**
* @return SimpleMessageTiming
*/
private function generateTiming()
{
$start = array_key_exists('REQUEST_TIME_FLOAT', $_SERVER) ?
\DateTime::createFromFormat('U.u', (double) $_SERVER['REQUEST_TIME_FLOAT']) :
(array_key_exists('REQUEST_TIME', $_SERVER) ?
\DateTime::createFromFormat('U', (int) $_SERVER['REQUEST_TIME']) :
new \DateTime()
)
;

$end = \DateTime::createFromFormat('U.u', microtime(true));

$difference = ((double) $end->format('U.u')) - ((double) $start->format('U.u'));

return SimpleMessageTiming::fromMilliseconds($difference * 1000);
}
}
1 change: 1 addition & 0 deletions src/Tolerance/MessageProfile/Storage/ElasticaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function store(MessageProfile $profile)
{
$documentUuid = Uuid::uuid4()->toString();
$normalized = $this->normalizer->normalize($profile);
$normalized['@timestamp'] = (int) ((double) $profile->getTiming()->getStart()->format('U.u')) * 1000;

$this->type->addDocument(new Document($documentUuid, $normalized));
}
Expand Down
13 changes: 10 additions & 3 deletions src/Tolerance/MessageProfile/Timing/MessageTiming.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@
interface MessageTiming
{
/**
* Amount of time, in milliseconds.
* Beginning of the request.
*
* @return float
* @return \DateTime
*/
public function getTotal();
public function getStart();

/**
* Beginning of the request.
*
* @return \DateTime
*/
public function getEnd();
}
43 changes: 37 additions & 6 deletions src/Tolerance/MessageProfile/Timing/SimpleMessageTiming.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,63 @@
class SimpleMessageTiming implements MessageTiming
{
/**
* @var \DateTime
*/
private $start;

/**
* @var \DateTime
*/
private $end;

/**
* Duration, in seconds.
*
* @var float
*/
private $milliseconds;
private $duration;

private function __construct()
{
}

/**
* @param float $milliseconds
* @param \DateTime $start
* @param \DateTime $end
*
* @return SimpleMessageTiming
*/
public static function fromMilliseconds($milliseconds)
public static function fromRange(\DateTime $start, \DateTime $end)
{
$timing = new self();
$timing->milliseconds = $milliseconds;
$timing->start = $start;
$timing->end = $end;
$timing->duration = ((double) $end->format('U.u') - (double) $start->format('U.u')) * 1000;

return $timing;
}

/**
* {@inheritdoc}
*/
public function getTotal()
public function getStart()
{
return $this->start;
}

/**
* {@inheritdoc}
*/
public function getEnd()
{
return $this->end;
}

/**
* {@inheritdoc}
*/
public function getDuration()
{
return $this->milliseconds;
return $this->duration;
}
}

0 comments on commit 793d093

Please sign in to comment.