Skip to content

Refactor HTTP client integration feature #797

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

Merged
merged 9 commits into from
Nov 15, 2023
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"friendsofphp/php-cs-fixer": "^3.11",
"mockery/mockery": "^1.3",
"phpstan/phpstan": "^1.10",
"laravel/folio": "^1.0"
"laravel/folio": "^1.0",
"guzzlehttp/guzzle": "^7.2"
},
"autoload-dev": {
"psr-4": {
Expand Down
67 changes: 0 additions & 67 deletions src/Sentry/Laravel/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Events as DatabaseEvents;
use Illuminate\Http\Client\Events as HttpClientEvents;
use Illuminate\Http\Request;
use Illuminate\Log\Events as LogEvents;
use Illuminate\Routing\Events as RoutingEvents;
Expand All @@ -20,16 +19,13 @@
use RuntimeException;
use Sentry\Breadcrumb;
use Sentry\Laravel\Tracing\Middleware;
use Sentry\Laravel\Util\WorksWithUris;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;

class EventHandler
{
use WorksWithUris;

/**
* Map event handlers to events.
*
Expand All @@ -41,8 +37,6 @@ class EventHandler
DatabaseEvents\QueryExecuted::class => 'queryExecuted',
ConsoleEvents\CommandStarting::class => 'commandStarting',
ConsoleEvents\CommandFinished::class => 'commandFinished',
HttpClientEvents\ResponseReceived::class => 'httpClientResponseReceived',
HttpClientEvents\ConnectionFailed::class => 'httpClientConnectionFailed',
];

/**
Expand Down Expand Up @@ -123,13 +117,6 @@ class EventHandler
*/
private $recordOctaneTaskInfo;

/**
* Indicates if we should add HTTP client requests info to the breadcrumbs.
*
* @var bool
*/
private $recordHttpClientRequests;

/**
* Indicates if we pushed a scope for Octane.
*
Expand All @@ -153,7 +140,6 @@ public function __construct(Container $container, array $config)
$this->recordCommandInfo = ($config['breadcrumbs.command_info'] ?? $config['breadcrumbs']['command_info'] ?? true) === true;
$this->recordOctaneTickInfo = ($config['breadcrumbs.octane_tick_info'] ?? $config['breadcrumbs']['octane_tick_info'] ?? true) === true;
$this->recordOctaneTaskInfo = ($config['breadcrumbs.octane_task_info'] ?? $config['breadcrumbs']['octane_task_info'] ?? true) === true;
$this->recordHttpClientRequests = ($config['breadcrumbs.http_client_requests'] ?? $config['breadcrumbs']['http_client_requests'] ?? true) === true;
}

/**
Expand Down Expand Up @@ -278,59 +264,6 @@ protected function messageLoggedHandler(LogEvents\MessageLogged $logEntry): void
));
}

protected function httpClientResponseReceivedHandler(HttpClientEvents\ResponseReceived $event): void
{
if (!$this->recordHttpClientRequests) {
return;
}

$level = Breadcrumb::LEVEL_INFO;
if ($event->response->failed()) {
$level = Breadcrumb::LEVEL_ERROR;
}

$fullUri = $this->getFullUri($event->request->url());

Integration::addBreadcrumb(new Breadcrumb(
$level,
Breadcrumb::TYPE_HTTP,
'http',
null,
[
'url' => $this->getPartialUri($fullUri),
'http.request.method' => $event->request->method(),
'http.response.status_code' => $event->response->status(),
'http.query' => $fullUri->getQuery(),
'http.fragment' => $fullUri->getFragment(),
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
'http.response.body.size' => $event->response->toPsrResponse()->getBody()->getSize(),
]
));
}

protected function httpClientConnectionFailedHandler(HttpClientEvents\ConnectionFailed $event): void
{
if (!$this->recordHttpClientRequests) {
return;
}

$fullUri = $this->getFullUri($event->request->url());

Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_ERROR,
Breadcrumb::TYPE_HTTP,
'http',
null,
[
'url' => $this->getPartialUri($fullUri),
'http.request.method' => $event->request->method(),
'http.query' => $fullUri->getQuery(),
'http.fragment' => $fullUri->getFragment(),
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
]
));
}

protected function authenticatedHandler(AuthEvents\Authenticated $event): void
{
$this->configureUserScopeFromModel($event->user);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Sentry\Laravel\Features\Concerns;

use Sentry\Laravel\Integration;
use Sentry\SentrySdk;
use Sentry\Tracing\Span;

trait TracksPushedScopesAndSpans
{
/**
* Hold the number of times the scope was pushed.
*
* @var int
*/
private $pushedScopeCount = 0;

/**
* Hold the stack of parent spans that need to be put back on the scope.
*
* @var array<int, Span|null>
*/
private $parentSpanStack = [];

/**
* Hold the stack of current spans that need to be finished still.
*
* @var array<int, Span|null>
*/
private $currentSpanStack = [];

protected function pushSpan(Span $span): void
{
$hub = SentrySdk::getCurrentHub();

$this->parentSpanStack[] = $hub->getSpan();

$hub->setSpan($span);

$this->currentSpanStack[] = $span;
}

protected function pushScope(): void
{
SentrySdk::getCurrentHub()->pushScope();

++$this->pushedScopeCount;
}

protected function maybePopSpan(): ?Span
{
if (count($this->currentSpanStack) === 0) {
return null;
}

$parent = array_pop($this->parentSpanStack);

SentrySdk::getCurrentHub()->setSpan($parent);

return array_pop($this->currentSpanStack);
}

protected function maybePopScope(): void
{
Integration::flushEvents();

if ($this->pushedScopeCount === 0) {
return;
}

SentrySdk::getCurrentHub()->popScope();

--$this->pushedScopeCount;
}
}
Loading