Skip to content

Commit

Permalink
Merge pull request #18 from LordSimal/add-http-client-requests
Browse files Browse the repository at this point in the history
add HTTP Client tracing
  • Loading branch information
LordSimal authored Aug 21, 2024
2 parents c7e945e + f11c5cb commit aa30628
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^5.0",
"phpunit/phpunit": "^10.5"
"phpunit/phpunit": "^10.5.5"
},
"license": "MIT",
"autoload": {
Expand Down
76 changes: 76 additions & 0 deletions src/Event/HttpEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);

namespace CakeSentry\Event;

use Cake\Event\EventListenerInterface;
use Cake\Http\Client\ClientEvent;
use CakeSentry\SpanStackTrait;
use Sentry\SentrySdk;
use Sentry\Tracing\SpanContext;

class HttpEventListener implements EventListenerInterface
{
use SpanStackTrait;

/**
* @inheritDoc
*/
public function implementedEvents(): array
{
return [
'HttpClient.beforeSend' => 'handleBeforeSend',
'HttpClient.afterSend' => 'handleAfterSend',
];
}

/**
* @param \Cake\Http\Client\ClientEvent $event
* @return void
*/
public function handlebeforeSend(ClientEvent $event): void
{
/** @var \Cake\Http\Client\Request $request */
$request = $event->getRequest();
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

if ($parentSpan !== null) {
$span = SpanContext::make()
->setOp('http.client');

$uri = $request->getUri();
$fullUrl = sprintf('%s://%s%s/', $uri->getScheme(), $uri->getHost(), $uri->getPath());
$span
->setDescription(sprintf('%s %s', $request->getMethod(), $fullUrl))
->setData([
'url' => $fullUrl,
'http.query' => $uri->getQuery(),
'http.fragment' => $uri->getFragment(),
'http.request.method' => $request->getMethod(),
'http.request.body.size' => $request->getBody()->getSize(),
]);

$this->pushSpan($parentSpan->startChild($span));
}
}

/**
* @param \Cake\Http\Client\ClientEvent $event
* @return void
*/
public function handleAfterSend(ClientEvent $event): void
{
$response = $event->getResult();
$span = $this->popSpan();

if ($span !== null) {
$span
->setHttpStatus($response?->getStatusCode() ?? 0)
->setData([
'http.response.body.size' => $response?->getBody()?->getSize(),
'http.response.status_code' => $response?->getStatusCode() ?? 0,
])
->finish();
}
}
}
2 changes: 2 additions & 0 deletions src/Middleware/CakeSentryPerformanceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cake\Event\EventManager;
use Cake\Http\Server;
use CakeSentry\Database\Log\CakeSentryLog;
use CakeSentry\Event\HttpEventListener;
use CakeSentry\EventListener;
use CakeSentry\QuerySpanTrait;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -77,6 +78,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$this->addQueryData();
$listener = new EventListener();
EventManager::instance()->on($listener);
EventManager::instance()->on(new HttpEventListener());

$response = $handler->handle($request);

Expand Down
37 changes: 1 addition & 36 deletions src/QuerySpanTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@
use Cake\Database\Schema\SqlserverSchemaDialect;
use Cake\Datasource\ConnectionManager;
use Sentry\SentrySdk;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\SpanStatus;

trait QuerySpanTrait
{
/**
* @var array
*/
protected array $parentSpanStack = [];

/**
* @var array
*/
protected array $currentSpanStack = [];
use SpanStackTrait;

/**
* @param \Cake\Database\Log\LoggedQuery $query
Expand Down Expand Up @@ -81,30 +72,4 @@ public function addTransactionSpan(LoggedQuery $query, ?string $connectionName =
$spanContext->setEndTimestamp($spanContext->getStartTimestamp() + $context['took'] / 1000);
$parentSpan->startChild($spanContext);
}

/**
* @param \Sentry\Tracing\Span $span The span.
* @return void
*/
protected function pushSpan(Span $span): void
{
$this->parentSpanStack[] = SentrySdk::getCurrentHub()->getSpan();
SentrySdk::getCurrentHub()->setSpan($span);
$this->currentSpanStack[] = $span;
}

/**
* @return \Sentry\Tracing\Span|null
*/
protected function popSpan(): ?Span
{
if (count($this->currentSpanStack) === 0) {
return null;
}

$parent = array_pop($this->parentSpanStack);
SentrySdk::getCurrentHub()->setSpan($parent);

return array_pop($this->currentSpanStack);
}
}
46 changes: 46 additions & 0 deletions src/SpanStackTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace CakeSentry;

use Sentry\SentrySdk;
use Sentry\Tracing\Span;

trait SpanStackTrait
{
/**
* @var array
*/
protected array $parentSpanStack = [];

/**
* @var array
*/
protected array $currentSpanStack = [];

/**
* @param \Sentry\Tracing\Span $span The span.
* @return void
*/
protected function pushSpan(Span $span): void
{
$this->parentSpanStack[] = SentrySdk::getCurrentHub()->getSpan();
SentrySdk::getCurrentHub()->setSpan($span);
$this->currentSpanStack[] = $span;
}

/**
* @return \Sentry\Tracing\Span|null
*/
protected function popSpan(): ?Span
{
if (count($this->currentSpanStack) === 0) {
return null;
}

$parent = array_pop($this->parentSpanStack);
SentrySdk::getCurrentHub()->setSpan($parent);

return array_pop($this->currentSpanStack);
}
}

0 comments on commit aa30628

Please sign in to comment.