Skip to content

Commit f132ad5

Browse files
authored
Refactor HTTP client integration feature (#797)
1 parent 9f3a0d7 commit f132ad5

File tree

9 files changed

+400
-252
lines changed

9 files changed

+400
-252
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"friendsofphp/php-cs-fixer": "^3.11",
4242
"mockery/mockery": "^1.3",
4343
"phpstan/phpstan": "^1.10",
44-
"laravel/folio": "^1.0"
44+
"laravel/folio": "^1.0",
45+
"guzzlehttp/guzzle": "^7.2"
4546
},
4647
"autoload-dev": {
4748
"psr-4": {

src/Sentry/Laravel/EventHandler.php

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\Contracts\Events\Dispatcher;
1212
use Illuminate\Database\Eloquent\Model;
1313
use Illuminate\Database\Events as DatabaseEvents;
14-
use Illuminate\Http\Client\Events as HttpClientEvents;
1514
use Illuminate\Http\Request;
1615
use Illuminate\Log\Events as LogEvents;
1716
use Illuminate\Routing\Events as RoutingEvents;
@@ -20,16 +19,13 @@
2019
use RuntimeException;
2120
use Sentry\Breadcrumb;
2221
use Sentry\Laravel\Tracing\Middleware;
23-
use Sentry\Laravel\Util\WorksWithUris;
2422
use Sentry\SentrySdk;
2523
use Sentry\State\Scope;
2624
use Symfony\Component\Console\Input\ArgvInput;
2725
use Symfony\Component\Console\Input\InputInterface;
2826

2927
class EventHandler
3028
{
31-
use WorksWithUris;
32-
3329
/**
3430
* Map event handlers to events.
3531
*
@@ -41,8 +37,6 @@ class EventHandler
4137
DatabaseEvents\QueryExecuted::class => 'queryExecuted',
4238
ConsoleEvents\CommandStarting::class => 'commandStarting',
4339
ConsoleEvents\CommandFinished::class => 'commandFinished',
44-
HttpClientEvents\ResponseReceived::class => 'httpClientResponseReceived',
45-
HttpClientEvents\ConnectionFailed::class => 'httpClientConnectionFailed',
4640
];
4741

4842
/**
@@ -123,13 +117,6 @@ class EventHandler
123117
*/
124118
private $recordOctaneTaskInfo;
125119

126-
/**
127-
* Indicates if we should add HTTP client requests info to the breadcrumbs.
128-
*
129-
* @var bool
130-
*/
131-
private $recordHttpClientRequests;
132-
133120
/**
134121
* Indicates if we pushed a scope for Octane.
135122
*
@@ -153,7 +140,6 @@ public function __construct(Container $container, array $config)
153140
$this->recordCommandInfo = ($config['breadcrumbs.command_info'] ?? $config['breadcrumbs']['command_info'] ?? true) === true;
154141
$this->recordOctaneTickInfo = ($config['breadcrumbs.octane_tick_info'] ?? $config['breadcrumbs']['octane_tick_info'] ?? true) === true;
155142
$this->recordOctaneTaskInfo = ($config['breadcrumbs.octane_task_info'] ?? $config['breadcrumbs']['octane_task_info'] ?? true) === true;
156-
$this->recordHttpClientRequests = ($config['breadcrumbs.http_client_requests'] ?? $config['breadcrumbs']['http_client_requests'] ?? true) === true;
157143
}
158144

159145
/**
@@ -278,59 +264,6 @@ protected function messageLoggedHandler(LogEvents\MessageLogged $logEntry): void
278264
));
279265
}
280266

281-
protected function httpClientResponseReceivedHandler(HttpClientEvents\ResponseReceived $event): void
282-
{
283-
if (!$this->recordHttpClientRequests) {
284-
return;
285-
}
286-
287-
$level = Breadcrumb::LEVEL_INFO;
288-
if ($event->response->failed()) {
289-
$level = Breadcrumb::LEVEL_ERROR;
290-
}
291-
292-
$fullUri = $this->getFullUri($event->request->url());
293-
294-
Integration::addBreadcrumb(new Breadcrumb(
295-
$level,
296-
Breadcrumb::TYPE_HTTP,
297-
'http',
298-
null,
299-
[
300-
'url' => $this->getPartialUri($fullUri),
301-
'http.request.method' => $event->request->method(),
302-
'http.response.status_code' => $event->response->status(),
303-
'http.query' => $fullUri->getQuery(),
304-
'http.fragment' => $fullUri->getFragment(),
305-
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
306-
'http.response.body.size' => $event->response->toPsrResponse()->getBody()->getSize(),
307-
]
308-
));
309-
}
310-
311-
protected function httpClientConnectionFailedHandler(HttpClientEvents\ConnectionFailed $event): void
312-
{
313-
if (!$this->recordHttpClientRequests) {
314-
return;
315-
}
316-
317-
$fullUri = $this->getFullUri($event->request->url());
318-
319-
Integration::addBreadcrumb(new Breadcrumb(
320-
Breadcrumb::LEVEL_ERROR,
321-
Breadcrumb::TYPE_HTTP,
322-
'http',
323-
null,
324-
[
325-
'url' => $this->getPartialUri($fullUri),
326-
'http.request.method' => $event->request->method(),
327-
'http.query' => $fullUri->getQuery(),
328-
'http.fragment' => $fullUri->getFragment(),
329-
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
330-
]
331-
));
332-
}
333-
334267
protected function authenticatedHandler(AuthEvents\Authenticated $event): void
335268
{
336269
$this->configureUserScopeFromModel($event->user);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Sentry\Laravel\Features\Concerns;
4+
5+
use Sentry\Laravel\Integration;
6+
use Sentry\SentrySdk;
7+
use Sentry\Tracing\Span;
8+
9+
trait TracksPushedScopesAndSpans
10+
{
11+
/**
12+
* Hold the number of times the scope was pushed.
13+
*
14+
* @var int
15+
*/
16+
private $pushedScopeCount = 0;
17+
18+
/**
19+
* Hold the stack of parent spans that need to be put back on the scope.
20+
*
21+
* @var array<int, Span|null>
22+
*/
23+
private $parentSpanStack = [];
24+
25+
/**
26+
* Hold the stack of current spans that need to be finished still.
27+
*
28+
* @var array<int, Span|null>
29+
*/
30+
private $currentSpanStack = [];
31+
32+
protected function pushSpan(Span $span): void
33+
{
34+
$hub = SentrySdk::getCurrentHub();
35+
36+
$this->parentSpanStack[] = $hub->getSpan();
37+
38+
$hub->setSpan($span);
39+
40+
$this->currentSpanStack[] = $span;
41+
}
42+
43+
protected function pushScope(): void
44+
{
45+
SentrySdk::getCurrentHub()->pushScope();
46+
47+
++$this->pushedScopeCount;
48+
}
49+
50+
protected function maybePopSpan(): ?Span
51+
{
52+
if (count($this->currentSpanStack) === 0) {
53+
return null;
54+
}
55+
56+
$parent = array_pop($this->parentSpanStack);
57+
58+
SentrySdk::getCurrentHub()->setSpan($parent);
59+
60+
return array_pop($this->currentSpanStack);
61+
}
62+
63+
protected function maybePopScope(): void
64+
{
65+
Integration::flushEvents();
66+
67+
if ($this->pushedScopeCount === 0) {
68+
return;
69+
}
70+
71+
SentrySdk::getCurrentHub()->popScope();
72+
73+
--$this->pushedScopeCount;
74+
}
75+
}

0 commit comments

Comments
 (0)