Skip to content

Commit c92e9fe

Browse files
Add connect exceptions on HTTP client metrics (#9)
* feat identified connect exception on http promise rejection * use interface instead exception * abstract catch of exception * add specific message on errors
1 parent 6c2980c commit c92e9fe

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Aspect/HttpClientMetricAspect.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use GuzzleHttp\Client;
1515
use GuzzleHttp\Exception\RequestException;
16+
use GuzzleHttp\Exception\TransferException;
1617
use GuzzleHttp\Promise\Create;
1718
use GuzzleHttp\Promise\PromiseInterface;
1819
use GuzzleHttp\Psr7\Uri;
@@ -49,6 +50,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
4950
'host' => $host,
5051
'method' => $method,
5152
'http_status_code' => '200',
53+
'message' => 'success',
5254
];
5355

5456
$timer = new Timer('http_client_requests', $labels);
@@ -74,8 +76,15 @@ private function onFullFilled(Timer $timer, array $labels): callable
7476

7577
private function onRejected(Timer $timer, array $labels): callable
7678
{
77-
return function (RequestException $exception) use ($timer, $labels) {
78-
$labels['http_status_code'] = (string) $exception->getResponse()->getStatusCode();
79+
return function (TransferException $exception) use ($timer, $labels) {
80+
$labels['http_status_code'] = '';
81+
$labels['message'] = 'connect_error';
82+
83+
if ($exception instanceof RequestException) {
84+
$labels['http_status_code'] = (string) $exception->getResponse()->getStatusCode();
85+
$labels['message'] = 'request_error';
86+
}
87+
7988
$timer->end($labels);
8089

8190
return Create::rejectionFor($exception);

0 commit comments

Comments
 (0)